搜尋
首頁資料庫mysql教程一般程式設計師必須掌握的SQL最佳化技巧

不管是工作中,還是面試中,基本上都需要搞定一些SQL最佳化技巧,比如說使用explain查看SQL的執行計劃,然後,針對執行計劃對SQL進行最佳化。

關於explain的使用以及相關欄位的分析,現在基本上已經是程式設計師標配了。

不會的,請認真看完。

1、什麼是MySQL執行計劃

要對執行計劃有個比較好的理解,需要先對MySQL的基礎結構及查詢基本原理有簡單的了解。

MySQL本身的功能架構分為三個部分,分別是 應用層、邏輯層、物理層,不只是MySQL ,其他大多數資料庫產品都是依照這個架構來劃分的。

  • 應用層,主要負責與客戶端進行交互,建立鏈接,記住鏈接狀態,返回數據,響應請求,這一層是和客戶端打交道的。
  • 邏輯層,主要負責查詢處理、事務管理等其他資料庫功能處理,以查詢為例。

先接收到查詢SQL之後,資料庫會立即指派一個執行緒來處理,第一步查詢處理器會對SQL查詢進行最佳化,最佳化後會產生執行計劃,然後交由計劃執行器來執行。

計劃執行器需要訪問更底層的事務管理器,存儲管理器來操作數據,他們各自的分工各有不同,最終通過調用物理層的文件獲取到查詢結構信息,將最終結果響應給應用層。

  • 物理層,實際實體磁碟上儲存的文件,主要有分文資料文件,日誌檔案。

透過上面的描述,生成執行計劃是執行一條SQL不可或缺的步驟,一個SQL效能的好壞,可以透過檢視執行計畫很直觀的看出來,執行計劃提供了各種查詢類型與級別,方面我們進行查看以及作為性能分析的依據。

2、如何分析執行計畫

MySQL為我們提供了 explain 關鍵字來直覺的檢視一條SQL的執行計畫。

explain顯示了MySQL如何使用索引來處理select語句以及連接表,可以幫助選擇更好的索引和寫出更優化的查詢語句。

下面我們使用explain 做一個查詢,如下:

mysql> explain select * from payment;
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
|  1 | SIMPLE      | payment | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 16086 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
1 row in set, 1 warning (0.01 sec)

查詢結構中有12列,理解每一列的含義,對理解執行計劃至關重要,下面用一個表格的形式進行說明。

SELECT identifier, this is the query sequence number of SELECT. SELECT type, which can be any of the following: The table referenced by the output rowIf the query is based on a partitioned table, the display query will access partition. Join type. The various join types are given below, sorted from best to worst:
Column name Description
##id
select_type SIMPLE: Simple SELECT (do not use UNION or Subquery)PRIMARY:Outermost SELECTUNION:The second or following SELECT statement in UNIONDEPENDENT UNION:The second or following SELECT statement in UNION The subsequent SELECT statement depends on the outer query UNION RESULT: The result of UNION SUBQUERY: The first SELECT in the subquery DEPENDENT SUBQUERY: In the subquery The first SELECT depends on the outer queryDERIVED: SELECT of the derived table (subquery of the FROM clause)
table
partitions
type
system: The table has only one row (=system table). This is a special case of the const join type.
const: The table has at most one matching row, which will be read at the beginning of the query. Because there is only one row, the column values ​​in this row can be treated as constants by the rest of the optimizer. const tables are fast because they are read only once!
eq_ref: For each combination of rows from the previous table, read one row from this table. This is probably the best join type, besides const types.
ref: For each combination of rows from the previous table, all rows with matching index values ​​will be read from this table.
ref_or_null: This join type is like ref, but adds MySQL to specifically search for rows containing NULL values.
index_merge: This join type indicates that the index merge optimization method is used.
unique_subquery: This type replaces the ref of the IN subquery in the following form: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an index lookup function that can completely replace the subquery and is more efficient. . index_subquery: This join type is similar to unique_subquery. IN subqueries can be replaced, but only for non-unique indexes in subqueries of the following form: value IN (SELECT key_column FROM single_table WHERE some_expr)
range: Retrieve only rows in a given range, use An index to select rows (recommended, at worst this level is required).
index: This join type is the same as ALL, except that only the index tree is scanned. This is usually faster than ALL because index files are usually smaller than data files.
ALL: Perform a complete table scan for each row combination from the previous table, indicating that the query needs to be optimized. Generally speaking, it is necessary to ensure that the query reaches at least the range level, and preferably reaches the ref.
The above system is the best, in descending order, ALL is the worst
possible_keys Indicate which index MySQL can use to find in the table Line
key shows the key (index) that MySQL actually decided to use. If no index is selected, the key is NULL.
key_len Displays the key length that MySQL decides to use. If the key is NULL, the length is NULL. The shorter the length the better without losing accuracy
ref Show which column or constant is used with the key from the table Select rows in .
rows Displays the number of rows that MySQL thinks it must examine when executing the query. Multiplying data across multiple rows provides an estimate of the number of rows to process.
filtered Shows the percentage estimate of the number of rows filtered by the condition.
Extra This column contains the details of how MySQL resolved the query
Distinct:MySQL found the 1st After a row is matched, stop searching for more rows for the current combination of rows.
Select tables optimized away MySQL returns data without traversing the table or index at all, indicating that it has been optimized to the point that it cannot be optimized anymore
Not exists:MySQL can perform query optimization LEFT JOIN optimization, after finding a row matching the LEFT JOIN standard, no more rows will be checked in the table for the previous row combination.
range checked for each record (index map: #):MySQL did not find a good index that can be used, but found that if the column values ​​​​from the previous table are known, some indexes may be used.
Using filesort: MySQL needs an extra pass to figure out how to retrieve the rows in sorted order, indicating that the query needs to be optimized.
Using index: Retrieve column information from the table by reading the actual rows using only the information in the index tree without further searching.
Using temporary: In order to solve the query, MySQL needs to create a temporary table to accommodate the results, which means the query needs to be optimized.
Using where: The WHERE clause is used to limit which row matches the next table or is sent to the customer.
Using sort_union(...), Using union(...), Using intersect(...): These functions illustrate how to merge index scans for the index_merge join type.
Using index for group-by: Similar to the Using index method of accessing a table, Using index for group-by means that MySQL has found an index that can be used to query all columns of GROUP BY or DISTINCT queries. , without having to additionally search the hard drive to access the actual table.

#According to the above table, it can provide good help in execution plan analysis.

Note: If it is to cope with the interview, it is best to be able to memorize it. If you cannot memorize it all, you should also be able to say 123, and then say, you can’t remember so much. , you can read the relevant documents to optimize SQL accordingly.

以上是一般程式設計師必須掌握的SQL最佳化技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:Java后端技术全栈。如有侵權,請聯絡admin@php.cn刪除
mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

mySQL:字符串數據類型和枚舉?mySQL:字符串數據類型和枚舉?May 13, 2025 am 12:05 AM

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable長度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

mysql blob:如何優化斑點請求mysql blob:如何優化斑點請求May 13, 2025 am 12:03 AM

優化MySQLBLOB請求可以通過以下策略:1.減少BLOB查詢頻率,使用獨立請求或延遲加載;2.選擇合適的BLOB類型(如TINYBLOB);3.將BLOB數據分離到單獨表中;4.在應用層壓縮BLOB數據;5.對BLOB元數據建立索引。這些方法結合實際應用中的監控、緩存和數據分片,可以有效提升性能。

將用戶添加到MySQL:完整的教程將用戶添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

掌握mySQL字符串數據類型:varchar vs.文本與char掌握mySQL字符串數據類型:varchar vs.文本與charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串數據類型和索引:最佳實踐MySQL:字符串數據類型和索引:最佳實踐May 12, 2025 am 12:11 AM

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器