mysql官方文档之WHERE子句优化

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语句,但同样的优化适用于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.

        您可能会尝试以牺牲可读性来重写查询,以便可以更快地进行算术运算。因为MySQL会自动进行类似的优化,所以通常可以避免这种工作,并将查询置于更易于理解和可维护的形式中。(意思是你自认为的可以加快查询速度的转换优化其实是在多此一举,mysql本身就已经为我们做了这一份工作,而且你的多此一举还让查询语句的的可读性降低了,并且没有产生任何效果)

        Some of the optimizations performed by MySQL follow:

        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 (needed because of constant folding):(持续条件移除;不断的折叠需要)

(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 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.

        COUNT(*)在单个表上,没有一个WHER条件,直接从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()等等),则与WHERE条件合并。

for example:

select * from student where id>1 having age>16;
和
select * from student where id>1 and age>16;

这两个是一个效果吧

        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条件,以便快速地对表的条件进行评估,并尽可能快地跳过行。(连接的时候join on 的列简单)

        All constant tables are read first before any other tables in the query. A constant table is any of the following:

        在查询任何其他表之前,首先读取的是常量表;常量表可以是下面的任何一种:

        (1)An empty table or a table with one row.空的表或者只有一行的表

        (2)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.

        通过尝试所有的可能性,可以找到连接表的最佳连接组合。如果按ORDER BY和GROUP 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 BY子句和一个不同的GROUP BY子句,或者如果ORDER BY或GROUP BY包含来自join队列中的第一张表以外的表的列,那么就会创建一个临时表。(让GROUP BY和ORDER BY的列所在的表放在join队列的第一个位置)

        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%的表为基础,但是一个固定的百分比不再决定是否使用索引或表扫描之间的选择。优化器现在更加复杂,并将其估计建立在其他因素上,如表大小、行数和输入/输出块大小。

        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, ... ;

        非常感谢查看文章的同学指正错误,提供意见。

        上一篇:https://blog.csdn.net/qwerdf10010/article/details/80491722

        下一篇:https://blog.csdn.net/qwerdf10010/article/details/80515872

猜你喜欢

转载自blog.csdn.net/qwerdf10010/article/details/80514301