8.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子句。
Constant expressions used by indexes are evaluated only once.
索引使用的常量表达式只计算一次。
COUNT(*) on a single table without a WHERE is retrieved directly from the table information for MyISAM and MEMORY tables. This is also done for any NOT NULL expression when used with only one table.
不带WHERE的单个表的COUNT(*)直接从MyISAM和MEMORY表的表信息中检索。当只与一个表一起使用时,也可以对任何NOT NULL表达式执行此操作。
Early detection of invalid constant expressions. MySQL quickly detects that some SELECT
早期检测无效常量表达式。MySQL很快检测到一些SELECT语句是不可能的,并且不返回任何行。
HAVING is merged with WHERE if you do not use GROUP BY or aggregate functions (COUNT(), MIN(), and so on).
如果不使用GROUP BY或聚合函数(COUNT()、MIN()等),HAVING将与WHERE合并。
For each table in a join, a simpler WHERE is constructed to get a fast WHERE evaluation 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 WHERE clause on a PRIMARY KEY or a UNIQUE index, where all index parts are compared to constant expressions and are defined as NOT NULL.
空表或只有一行的表。
与主键或唯一索引上的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 BY and GROUP BY clauses come from the same table, that 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 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_RESULT modifier, 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 HAVING clause 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:
假设索引列为数值,MySQL仅使用索引
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, ... ;