• +91 9723535972
  • info@interviewmaterial.com

My SQL Interview Questions and Answers

Question - How MySQL Optimizes WHERE Clauses ?

Answer - The WHERE optimizations are put in the SELECT part here because they are mostly used with SELECT, but the same optimizations apply for WHERE in DELETE and UPDATE statements. Also note that this section is incomplete. MySQL does many optimizations, and we have not had time to document them all. Some of the optimizations performed by MySQL are listed below: Removal of unnecessary parentheses: ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -> (a AND b AND c) OR (a AND b AND c AND d) Constant folding: (a b>5 AND b=c AND a=5 Constant condition removal (needed because of constant folding): (B>=5 AND B=5) OR (B=6 AND 5=50) OR (B=7 AND 5=6) -> B=5 OR B=6 Constant expressions used by indexes are evaluated only once. COUNT(*) on a single table without a WHERE is retrieved directly from the table information. This is also done for any NOT NULL expression when used with only one table. Early detection of invalid constant expressions. MySQL quickly detects that some SELECT statements are impossible and returns no rows. HAVING is merged with WHERE if you don't use GROUP BY or group functions (COUNT(), MIN()...). For each sub-join, a simpler WHERE is constructed to get a fast WHERE evaluation for each sub-join and also to skip records as soon as possible. All constant tables are read first, before any other tables in the query. A constant table is: An empty table or a table with 1 row. A table that is used with a WHERE clause on a UNIQUE index, or a PRIMARY KEY, where all index parts are used with constant expressions and the index parts are defined as NOT NULL. All the following tables are used as constant tables: mysql> SELECT * FROM t WHERE primary_key=1; mysql> SELECT * FROM t1,t2 WHERE t1.primary_key=1 AND t2.primary_key=t1.id; The best join combination to join the tables is found by trying all possibilities. If all columns in ORDER BY and in GROUP BY come from the same table, then this table is preferred first when joining. If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners