8.2.1.1 WHERE 子句优化
8.2.1.1 WHERE Clause Optimization 8.2.1.1 WHERE 子句优化
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 子句。
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=6 -
Constant expressions used by indexes are evaluated only once.
索引使用的常量表达式只计算一次。
-
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.直接从 MyISAM 和 MEMORY 表的表信息中检索没有 WHERE 的单个表的 COUNT (*)。当只对一个表使用时,对于任何 notnull 表达式也是如此。
-
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).如果不使用 groupby 或聚合函数(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.与 primarykey 上的 WHERE 子句一起使用的表或 UNIQUE 索引,其中所有索引部分都与常量表达式进行比较,并定义为 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.通过尝试所有可能性可以找到连接表的最佳连接组合。如果 orderby 和 groupby 子句中的所有列都来自同一个表,则首选在连接时先连接该表。
-
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, ... ;
-----------------------------------------
原文链接:https://dev.mysql.com/doc/refman/5.7/en/where-optimization.html

浙公网安备 33010602011771号