WHERE 子句优化

1 WHERE Clause Optimization (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 语句,但同样适用于 DELETEUPDATE 语句中的 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
    

    This takes place during preparation rather than during the optimization phase, which helps in simplification of joins. See Section 8.2.1.9, “Outer Join Optimization”, for further information and examples.

    这发生在准备阶段而不是优化阶段,这有助于简化连接。有关更多信息和示例,请参阅第 8.2.1.9 节“外连接优化”。

  • Constant expressions used by indexes are evaluated only once(索引使用的常量表达式仅计算一次).

  • 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 8.2.1.14, “Constant-Folding Optimization”, for more information.

    有关详细信息,请参阅第 8.2.1.14 节“常量合并优化”。

  • 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 statements are impossible and returns no rows.

    及早检测无效常量表达式。 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 使用主键或唯一性索引的表,其中所有索引部分都与常量表达式进行比较并定义为非空。

    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.

    通过尝试所有可能性找到连接表的最佳连接组合。如果 ORDER BYGROUP BY 子句中的所有列都来自同一个表,则在连接时首选该表。

  • 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 BYGROUP BY 子句不同,或者 ORDER BYGROUP 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 可以从索引中读取行,而无需读取数据文件。如果索引中使用的所有列都是数字,则仅使用索引树来解析查询。

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 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, ... ;
posted @ 2023-07-21 20:47  MartinGarrix  阅读(45)  评论(0)    收藏  举报