- 8.2.1.1 WHERE子句优化
- 8.2.1.2 范围优化
- 8.2.1.3 索引合并优化
- 8.2.1.4 发动机工况下推优化
- 8.2.1.5 索引条件下推优化
- 8.2.1.6 嵌套循环连接算法
- 8.2.1.7 嵌套连接优化
- 8.2.1.8 外连接优化
- 8.2.1.9 外部连接简化
- 8.2.1.10 多范围读取优化
- 8.2.1.11 阻止嵌套循环和批量密钥访问连接
- 8.2.1.12 条件过滤
- 8.2.1.13 IS NULL 优化
- 8.2.1.14 ORDER BY 优化
- 8.2.1.15 GROUP BY优化
- 8.2.1.16 DISTINCT 优化
- 8.2.1.17 LIMIT 查询优化
- 8.2.1.18 函数调用优化
- 8.2.1.19 行构造函数表达式优化
- 8.2.1.20 避免全表扫描
查询以SELECT
语句的形式执行数据库中的所有查找操作。调整这些语句是重中之重,无论是实现动态网页的亚秒级响应时间,还是缩短时间以生成庞大的通宵报告。
除了SELECT
语句之外,查询的调优技术也适用于语句中的
CREATE
TABLE...AS SELECT
、
INSERT
INTO...SELECT
和WHERE
子句
等结构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 aSELECT
. 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
andMyISAM
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 theEXPLAIN
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.
处理锁定问题,您的查询速度可能会受到同时访问表的其他会话的影响。