mysql 官方文檔優化之SELECT語句優化

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章