Amazon Interview Question
Software Engineer / DevelopersAt each stage , use Explain or Explain Plan ( as it is the case depending on the various databases and interfaces ) , so that you can see the cost of running the query , and keep optimizing using the above mentioned highlighted points , and see if the cost of the query can be minimized.
Other things that can be checked to see if the query is optimized , is to check for proper indexes (that should be excluded as per the question , I guess) , and usage of table spaces while creating tables , not using many temporary tables while performing secondary queries on them , instead creating views/materialized views on behalf of those temporary tables would enhance the query cost a lot.
One simple tips for tuning up query is to first filter on most common dataset and then least common dataset. This make sure query execution plan is optimized. Some of the advance databases already takes care of this regardless of what is in where clause.
For example, to find details of city in world we can first filter based on continents which is most common, followed by country then state then county or district and then city. So query would look like
Select * from city where continent = NA and country = USA and state = CA and county = OC and city = Irvine
Next step is to make sure to have index on frequently used columns in where clause. But having more indices on table also reduces performance so care has to be taken.
SQL Tuning/SQL Optimization Techniques:
1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
}
2) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use HAVING clause for any other purposes.
For Example: Write the query as
}
Instead of:
}
3) Sometimes you may have more than one subqueries in your main query. Try to minimize the number of subquery block in your query.
For Example: Write the query as
}
Instead of:
}
4) Use operator EXISTS, IN and table joins appropriately in your query.
a) Usually IN has the slowest performance.
b) IN is efficient when most of the filter criteria is in the sub-query.
c) EXISTS is efficient when most of the filter criteria is in the main query.
For Example: Write the query as
}
Instead of:
}
5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as
}
Instead of:
}
6) Try to use UNION ALL in place of UNION.
For Example: Write the query as
}
Instead of:
}
7) Be careful while using conditions in WHERE clause.
For Example: Write the query as
}
Instead of:
}
Write the query as
}
Instead of:
}
Write the query as
}
Instead of:
}
Write the query as
}
Instead of:
}
Write the query as
}
Instead of:
}
Use non-column expression on one side of the query because it will be processed earlier.
Write the query as
}
Instead of:
}
Write the query as
}
Instead of:
}
- ajit.it.engg January 07, 20118) To store large binary objects, first place them in the file system and add the file path in the database.
9) To write queries which provide efficient performance follow the general SQL standard rules.
a) Use single case for all SQL verbs
b) Begin all SQL verbs on a new line
c) Separate all words with a single space
d) Right or left aligning verbs within the initial SQL verb