Queries,intheformof SELECT statements,performallthelookupoperationsinthedatabase.查询以SELECT
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…DELETE语句中的SELECT和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. For more information about this optimization, see Conditions for NDB pushdown joins.
NDB集群支持一个连接下推优化,通过这个优化,一个合格的连接被完整地发送到NDB集群的数据节点,在那里它可以分布在它们之间并并行执行。有关此优化的更多信息,请参见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. Set up indexes on columns used in the WHERE
clause, to speed up evaluation, filtering, and the final retrieval of results. To avoid wasted disk space, construct a small set of indexes that speed up many related queries used in your application.
要做一个缓慢的SELECT…在查询速度更快的地方,首先要检查的是是否可以添加索引。在WHERE子句中使用的列上设置索引,以加快对结果的计算、筛选和最终检索。为了避免浪费磁盘空间,可以构建一小组索引,以加速应用程序中使用的许多相关查询。
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. See Section 8.3.1, “How MySQL Uses Indexes” and Section 8.8.1, “Optimizing Queries with EXPLAIN”.
您可以使用EXPLAIN语句来确定用于SELECT的索引。
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. For details, see Section 8.5.6, “Optimizing InnoDB Queries” and Section 8.6.1, “Optimizing MyISAM Queries”.
InnoDB和MyISAM都有一套在查询中启用和保持高性能的指导原则。
You can optimize single-query transactions for 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. (When you reach a certain level of expertise, reading the EXPLAIN plan might be your first step for every query.)
如果性能问题不容易通过基本准则解决,可以通过阅读EXPLAIN计划并调整索引、WHERE子句、连接子句等来研究特定查询的内部细节。(当您达到一定的专业水平时,阅读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.
处理锁定问题,其中查询的速度可能会受到同时访问表的其他会话的影响。