Documentation Home
MySQL 8.0 参考手册  / 第8章优化  / 8.2 优化SQL语句  /  8.2.1 优化 SELECT 语句

8.2.1 优化 SELECT 语句

查询以SELECT 语句的形式执行数据库中的所有查找操作。调整这些语句是重中之重,无论是实现动态网页的亚秒级响应时间,还是缩短时间以生成庞大的通宵报告。

除了SELECT语句之外,查询的调优技术也适用于语句中的 CREATE TABLE...AS SELECTINSERT INTO...SELECTWHERE子句 等结构DELETE。这些语句具有额外的性能考虑因素,因为它们将写入操作与面向读取的查询操作结合在一起。

NDB Cluster 支持连接下推优化,通过该优化,合格的连接将被完整发送到 NDB Cluster 数据节点,在那里它可以分布在它们之间并并行执行。有关此优化的更多信息,请参阅 NDB 下推连接的条件

优化查询的主要考虑因素是:

  • 要使慢SELECT ... WHERE查询更快,首先要检查是否可以添加 索引。在子句中使用的列上设置索引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”.

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

  • 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”.

  • You can optimize single-query transactions for InnoDB tables, using the technique in Section 8.5.3, “Optimizing InnoDB Read-Only Transactions”.

  • 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.)

  • Adjust the size and properties of the memory areas that MySQL uses for caching. 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.

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

  • 处理锁定问题,您的查询速度可能会受到同时访问表的其他会话的影响。