Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
5
of 5 vote

In SQL? You index on columns you're likely to query on. If a SELECT query has a WHERE clause checking for column X = a value, and if furthermore only a small portion of all the rows in the table have X equal to that value, then using an index on X can substantially improve the performance of the query. The benefit is then reaped everywhere you have a SELECT with a "WHERE X = some value" clause.


The reason that having an index helps is that when you have an index, something like a hashmap is used, mapping from values of the indexed column to data rows. When you want to find records with a particular value in the indexed column, you use the value as a key to locate a list of records matching that value. However, using an index means extra time spent maintaining the index when data is added to or removed from the database. The indexes could also use a substantial amount of space. So it's a tradeoff. That's why implementations of SQL make you ask for an index instead of putting one in automatically.

- eugene.yarovoi April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice answer!

- siva.sai.2020 April 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The mention of a hashmap was just for conceptual explanation -- some SQL implementations use B-trees and similar data structures since those are much more space-efficient.

- eugene.yarovoi November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Partitioning of big tables should be done in such a way that most of queries get data from single partition. For example, if most of the time your queries recent data then it makes sense to partition on creation_date column but most of the times it is subjective. If you are talking about MySQL there are restrictions on creating partition which are not part of PK if the table has PK, so beware of that.

- Shashank April 19, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More