Queries, in the form of 查询以SELECT
statements, perform all the lookup operations in the database. SELECT
语句的形式执行数据库中的所有查找操作。Tuning these statements is a top priority, whether to achieve sub-second response times for dynamic web pages, or to chop hours off the time to generate huge overnight reports.调整这些语句是重中之重,无论是实现动态网页的次秒响应时间,还是缩短生成大量夜间报告的时间。
Besides 除了SELECT
statements, the tuning techniques for queries also apply to constructs such as CREATE TABLE...AS SELECT
, INSERT INTO...SELECT
, and WHERE
clauses in DELETE
statements. SELECT
语句外,查询的调优技术还适用于诸如CREATE TABLE...AS SELECT
、INSERT INTO...SELECT
和DELETE
语句中的WHERE子句等构造。Those statements have additional performance considerations because they combine write operations with the read-oriented query operations.这些语句有额外的性能考虑,因为它们将写操作与面向读的查询操作结合起来。
NDB Cluster supports a join pushdown optimization whereby a qualifying join is sent in its entirety to NDB Cluster data nodes, where it can be distributed among them and executed in parallel. NDB群集支持连接下推优化,将符合条件的join
整体发送到NDB群集数据节点,在这些节点之间可以分布并并行执行。For more information about this optimization, see Conditions for NDB pushdown joins.有关此优化的详细信息,请参见NDB下推联接的条件。
The main considerations for optimizing queries are:优化查询的主要考虑因素有:
To make a slow 要使一个较慢的SELECT ... WHERE
query faster, the first thing to check is whether you can add an index. SELECT ... WHERE
查询速度更快,首先要检查是否可以添加索引。Set up indexes on columns used in the 在WHERE
clause, to speed up evaluation, filtering, and the final retrieval of results. WHERE
子句中使用的列上设置索引,以加快计算、筛选和最终检索结果的速度。To avoid wasted disk space, construct a small set of indexes that speed up many related queries used in your application.为了避免浪费磁盘空间,可以构造一小组索引来加速应用程序中使用的许多相关查询。
Indexes are especially important for queries that reference different tables, using features such as joins and foreign keys. 索引对于使用连接和外键等功能引用不同表的查询尤其重要。You can use the 您可以使用EXPLAIN
statement to determine which indexes are used for a SELECT
. EXPLAIN
语句来确定SELECT
所使用的索引。See Section 8.3.1, “How MySQL Uses Indexes” and Section 8.8.1, “Optimizing Queries with EXPLAIN”.请参阅第8.3.1节,“MySQL如何使用索引”和第8.8.1节,“使用EXPLAIN优化查询”。
Isolate and tune any part of the query, such as a function call, that takes excessive time. 隔离和调优查询中占用过多时间的任何部分,例如函数调用。Depending on how the query is structured, a function could be called once for every row in the result set, or even once for every row in the table, greatly magnifying any inefficiency.根据查询的结构,可以对结果集中的每一行调用一次函数,甚至可以对表中的每一行调用一次函数,这大大放大了效率低下的问题。
Minimize the number of full table scans in your queries, particularly for big tables.最小化查询中的全表扫描数,特别是对于大表。
Keep table statistics up to date by using the 通过定期使用ANALYZE TABLE
statement periodically, so the optimizer has the information needed to construct an efficient execution plan.ANALYZE TABLE
语句使表统计信息保持最新,这样优化器就拥有了构造高效执行计划所需的信息。
Learn the tuning techniques, indexing techniques, and configuration parameters that are specific to the storage engine for each table. 学习特定于每个表的存储引擎的调优技术、索引技术和配置参数。Both InnoDB
and MyISAM
have sets of guidelines for enabling and sustaining high performance in queries. InnoDB
和MyISAM
都有一套在查询中启用和保持高性能的指导原则。For details, see Section 8.5.6, “Optimizing InnoDB Queries” and Section 8.6.1, “Optimizing MyISAM Queries”.有关详细信息,请参阅第8.5.6节,“优化InnoDB查询”和第8.6.1节,“优化MyISAM查询”。
You can optimize single-query transactions for 您可以使用第8.5.3节“优化InnoDB只读事务”中的技术来优化InnoDB
tables, using the technique in Section 8.5.3, “Optimizing InnoDB Read-Only Transactions”.InnoDB
表的单查询事务。
Avoid transforming the query in ways that make it hard to understand, especially if the optimizer does some of the same transformations automatically.避免以难以理解的方式转换查询,特别是当优化器自动执行某些相同的转换时。
If a performance issue is not easily solved by one of the basic guidelines, investigate the internal details of the specific query by reading the 如果一个性能问题不容易用一个基本准则来解决,那么通过阅读EXPLAIN
plan and adjusting your indexes, WHERE
clauses, join clauses, and so on. EXPLAIN
计划并调整索引、WHERE
子句、join
子句等来调查特定查询的内部细节。(When you reach a certain level of expertise, reading the (当您达到一定的专业水平时,阅读EXPLAIN
plan might be your first step for every query.)EXPLAIN
计划可能是您处理每个问题的第一步。)
Adjust the size and properties of the memory areas that MySQL uses for caching. 调整MySQL用于缓存的内存区域的大小和属性。With efficient use of the 通过有效地使用InnoDB
buffer pool, MyISAM
key cache, and the MySQL query cache, repeated queries run faster because the results are retrieved from memory the second and subsequent times.InnoDB
缓冲池、MyISAM
密钥缓存和MySQL查询缓存,重复的查询运行得更快,因为结果会在第二次和随后的时间从内存中检索。
Even for a query that runs fast using the cache memory areas, you might still optimize further so that they require less cache memory, making your application more scalable. 即使对于使用缓存区域快速运行的查询,您仍然可以进一步优化,使它们需要更少的缓存,从而使应用程序更具可伸缩性。Scalability means that your application can handle more simultaneous users, larger requests, and so on without experiencing a big drop in performance.可伸缩性意味着您的应用程序可以处理更多的并发用户、更大的请求等,而不会出现性能的大幅下降。
Deal with locking issues, where the speed of your query might be affected by other sessions accessing the tables at the same time.处理锁定问题,其中查询的速度可能会受到同时访问表的其他会话的影响。