10.2.1.1 WHERE Clause Optimization
This section discusses optimizations that can be made for processing WHERE clauses. The examples use SELECT statements, but the same optimizations apply for WHERE clauses in DELETE and UPDATE statements.
本节讨论可以对处理WHERE子句进行的优化。这些示例使用SELECT语句,但同样的优化也适用于DELETE和UPDATE语句中的WHERE子句。
Note
Because work on the MySQL optimizer is ongoing, not all of the optimizations that MySQL performs are documented here.
因为MySQL优化器的工作正在进行中,所以这里并没有记录MySQL执行的所有优化。
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:
您可能会试图重写查询以使算术运算更快,同时牺牲可读性。由于MySQL会自动执行类似的优化,因此您通常可以避免这项工作,并将查询保持在更易于理解和维护的形式。MySQL执行的一些优化如下:
-
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 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=6In 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 10.2.1.9, “Outer Join Optimization”, for further information and examples.
在MySQL 8.0.14及更高版本中,这发生在准备阶段,而不是优化阶段,这有助于简化连接。更多信息和示例请参见第10.2.1.9节“外部连接优化”。
-
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:
从MySQL 8.0.16开始,检查数值类型的列与常量值的比较,并折叠或删除无效或超出范围的值:
# CREATE TABLE t (c TINYINT UNSIGNED NOT NULL); SELECT * FROM t WHERE c < 256; -≫ SELECT * FROM t WHERE 1;See Section 10.2.1.14, “Constant-Folding Optimization”, for more information.
-
COUNT(*)on a single table without aWHEREis retrieved directly from the table information forMyISAMandMEMORYtables. This is also done for anyNOT NULLexpression when used with only one table.不带WHERE的单个表上的COUNT(*)直接从MyISAM和MEMORY表的表信息中检索。当仅与一个表一起使用时,也可以对任何NOT NULL表达式执行此操作。
-
Early detection of invalid constant expressions. MySQL quickly detects that some
SELECTstatements are impossible and returns no rows.早期检测无效的常量表达式。MySQL很快检测到某些SELECT语句是不可能的,并且不返回任何行。
-
HAVINGis merged withWHEREif you do not useGROUP BYor aggregate functions (COUNT(),MIN(), and so on).如果不使用GROUP BY或聚合函数(COUNT()、MIN()等),HAVING将与WHERE合并。
-
For each table in a join, a simpler
WHEREis constructed to get a fastWHEREevaluation for the table and also to skip rows as soon as possible.对于联接中的每个表,构造一个更简单的WHERE,以快速计算表的WHERE,并尽快跳过行。
-
All constant tables are read first before any other tables in the query. A constant table is any of the following:
- An empty table or a table with one row.
- A table that is used with a
WHEREclause on aPRIMARY KEYor aUNIQUEindex, where all index parts are compared to constant expressions and are defined asNOT NULL.
所有常量表都会在查询中的任何其他表之前首先读取。常数表可以是以下任何一种:
- 空表或只有一行的表。
- 与PRIMARY KEY或UNIQUE索引上的WHERE子句一起使用的表,其中所有索引部分都与常量表达式进行比较,并定义为NOT NULL。
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
ORDER BYandGROUP BYclauses come from the same table, that table is preferred first when joining.通过尝试所有可能性,找到连接表的最佳连接组合。如果ORDER BY和GROUP BY子句中的所有列都来自同一个表,则连接时首选该表。
-
If there is an
ORDER BYclause and a differentGROUP BYclause, or if theORDER BYorGROUP BYcontains columns from tables other than the first table in the join queue, a temporary table is created.如果存在ORDER BY子句和不同的GROUP BY子句,或者ORDER BY或GROUP BY包含联接队列中第一个表以外的表中的列,则会创建一个临时表。
-
If you use the
SQL_SMALL_RESULTmodifier, MySQL uses an in-memory temporary table.如果您使用了 SQL_SMALL_RESULT 修饰符,MySQL将使用内存中的临时表。
-
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.
查询每个表索引,并使用最佳索引,除非优化器认为使用表扫描更有效。曾经,扫描是基于最佳索引是否覆盖了表的30%以上,但固定的百分比不再决定使用索引还是扫描。优化器现在更加复杂,其估计基于其他因素,如表大小、行数和I/O块大小。
-
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.
在某些情况下,MySQL甚至可以在不查阅数据文件的情况下从索引中读取行。如果索引中使用的所有列都是数字,则只有索引树用于解析查询。
-
Before each row is output, those that do not match the
HAVINGclause are skipped.在输出每一行之前,跳过与HAVING子句不匹配的行。
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 MAX(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, ... ;