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語句)
The main considerations for optimizing queries are:
索引對於引用不同表的查詢特別重要,利用諸如聯結和外鍵之類的特性。您可以使用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