This section discusses optimizations that can be made for
WHERE clauses. The examples use
SELECT statements, but the same
optimizations apply for
WHERE clauses in
Because work on the MySQL optimizer is ongoing, not all of the optimizations that MySQL performs are documented here.
You might be tempted to rewrite your queries to make arithmetic operations faster, while sacrificing readability. Because MySQL does similar optimizations automatically, you can often avoid this work, and leave the query in a more understandable and maintainable form. Some of the optimizations performed by MySQL follow:
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)
(a<b AND b=c) AND a=5 -> b>5 AND b=c AND a=5
Constant condition removal:
(b>=5 AND b=5) OR (b=6 AND 5=5) OR (b=7 AND 5=6) -> b=5 OR b=6
In MySQL 8.0.14 and later, this takes place during preparation rather than during the optimization phase, which helps in simplification of joins. See Section 188.8.131.52, “Outer Join Optimization”, for further information and examples.
Constant expressions used by indexes are evaluated only once.
Beginning with MySQL 8.0.16, comparisons of columns of numeric types with constant values are checked and folded or removed for invalid or out-of-rage values:
# CREATE TABLE t (c TINYINT UNSIGNED NOT NULL); SELECT * FROM t WHERE c ≪ 256; -≫ SELECT * FROM t WHERE 1;
See Section 184.108.40.206, “Constant-Folding Optimization”, for more information.
COUNT(*)on a single table without a
WHEREis retrieved directly from the table information for
MEMORYtables. This is also done for any
NOT NULLexpression when used with only one table.
Early detection of invalid constant expressions. MySQL quickly detects that some
SELECTstatements are impossible and returns no rows.
For each table in a join, a simpler
WHEREis constructed to get a fast
WHEREevaluation for the table and also to skip rows as soon as possible.
An empty table or a table with one row.
A table that is used with a
WHEREclause on a
PRIMARY KEYor a
UNIQUEindex, where all index parts are compared to constant expressions and are defined as
All of the following tables are used as constant tables:
SELECT * FROM t WHERE primary_key=1; SELECT * FROM t1,t2 WHERE t1.primary_key=1 AND t2.primary_key=t1.id;
The best join combination for joining the tables is found by trying all possibilities. If all columns in
GROUP BYclauses come from the same table, that table is preferred first when joining.
If there is an
ORDER BYclause and a different
GROUP BYclause, or if the
GROUP BYcontains columns from tables other than the first table in the join queue, a temporary table is created.
If you use the
SQL_SMALL_RESULTmodifier, MySQL uses an in-memory temporary table.
Each table index is queried, and the best index is used unless the optimizer believes that it is more efficient to use a table scan. At one time, a scan was used based on whether the best index spanned more than 30% of the table, but a fixed percentage no longer determines the choice between using an index or a scan. The optimizer now is more complex and bases its estimate on additional factors such as table size, number of rows, and I/O block size.
In some cases, MySQL can read rows from the index without even consulting the data file. If all columns used from the index are numeric, only the index tree is used to resolve the query.
Before each row is output, those that do not match the
HAVINGclause are skipped.
Some examples of queries that are very fast:
SELECT COUNT(*) FROM tbl_name; SELECT MIN(key_part1),MAX(key_part1) FROM tbl_name; SELECT MAX(key_part2) FROM tbl_name WHERE key_part1=constant; SELECT ... FROM tbl_name ORDER BY key_part1,key_part2,... LIMIT 10; SELECT ... FROM tbl_name ORDER BY key_part1 DESC, key_part2 DESC, ... LIMIT 10;
MySQL resolves the following queries using only the index tree, assuming that the indexed columns are numeric:
SELECT key_part1,key_part2 FROM tbl_name WHERE key_part1=val; SELECT COUNT(*) FROM tbl_name WHERE key_part1=val1 AND key_part2=val2; SELECT key_part2 FROM tbl_name GROUP BY key_part1;
The following queries use indexing to retrieve the rows in sorted order without a separate sorting pass:
SELECT ... FROM tbl_name ORDER BY key_part1,key_part2,... ; SELECT ... FROM tbl_name ORDER BY key_part1 DESC, key_part2 DESC, ... ;