mysql 官方文档优化之SELECT语句优化

时间:2022-07-29 06:00:39

Optimizing SQL Statements(优化SQL语句)

        The core logic of a database application is performed through SQL statements, whether issued directly through an interpreter or submitted behind the scenes through an API. The tuning guidelines in this section help to speed up all kinds of MySQL applications. The guidelines cover SQL operations that read and write data, the behind-the-scenes overhead for SQL operations in general, and operations used in specific scenarios such as database monitoring.

        数据库应用程序的核心逻辑是通过SQL语句执行的,无论是通过解释器直接发布,还是通过API在后台提交。本节中的调优指南有助于加快各种MySQL应用程序的速度。该指导方针涵盖了读取和写入数据的SQL操作、一般SQL操作的幕后开销,以及在特定场景中使用的操作,如数据库监控。

Optimizing SELECT Statements(优化SELECT语句)

        Queries, in the form of SELECT statements, perform all the lookup operations in the database. 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.
           查询,以SELECT语句的形式,执行数据库中的所有查找操作。 对这些语句进行调优是一项首要任务,无论是实现动态web页面的次秒响应时间,还是为了生成大量的夜间报告而缩短工作时间。
        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. Those statements have additional performance considerations because they combine write operations with the read-oriented query operations.
             除了SELECT语句之外,查询的调优技术也适用于诸如CREATE TABLE ..... AS SELECT、INSERT INTO......SELECT、和WHERE条件在DELETE语句中之类的构造 这些语句有额外的性能考虑,因为它们将写操作与面向读的查询操作结合起来。
            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语句在查询速度上更快,首先要检查的是是否可以添加索引。 在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.

            索引对于引用不同表的查询特别重要,利用诸如联结和外键之类的特性。您可以使用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。

            通过定期使用分析表语句来保持表统计信息,这样优化器就有了构建高效执行计划所需的信息。

           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都有一组用于支持和维持查询的高性能指导方针。

            You can optimize single-query transactions for InnoDB tables。

            您可以为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 计划和调整您的索引、WHERER子句、连接子句等等来研究特定查询的内部细节。(当你达到一定的专业水平时,阅读EXPLAIN 计划可能是你的第一步。)

            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.

           调整MySQL用于缓存的内存区域的大小和属性。通过有效地使用InnoDB缓冲池、MyISAM key cache和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.

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

           

            希望读者能够纠正错误;非常感谢!

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

            下一篇:https://blog.csdn.net/QWERDF10010/article/details/80514301