如何使用 MySQL 慢查詢日誌進行性能優化 - Profiling、mysqldumpslow 實例詳解

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們開始關注數據庫整體性能優化時,我們需要一套 MySQL 查詢分析工具。特別是在開發中大型項目時,往往有數百個查詢分佈在代碼庫中的各個角落,並實時對數據庫進行大量訪問和查詢。如果沒有一套趁手的分析方法和工具,就很難發現在執行過程中代碼的效率瓶頸,我們需要通過這套工具去定位 SQL 語句在執行中緩慢的問題和原因。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本教程帶領大家學習和實踐 MySQL Server 內置的查詢分析工具 —— 慢查詢日誌、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Profiling","attrs":{}}],"attrs":{}},{"type":"text","text":",詳細講解如何使用他們提升代碼執行效率。如果你想根據自己的工作流開發一套數據庫查詢管理工具,推薦使用卡拉雲。只要你會寫 SQL,無需會前端也可以輕鬆搭建屬於自己的後臺查詢工具,詳見本文文末。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一. 有關 MySQL 慢查詢日誌","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.慢查詢日誌是什麼?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL 慢查詢日誌是用來記錄 MySQL 在執行命令中,響應時間超過預設閾值的 SQL 語句。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"記錄這些執行緩慢的 SQL 語句是優化 MySQL 數據庫效率的第一步。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認情況下,慢查詢日誌功能是關閉的,需要我們手動打開。當然,如果不是調優需求的話,一般也不建議長期啓動這個功能,因爲開啓慢查詢多少會對數據庫的性能帶來一些影響。慢查詢日誌支持將記錄寫入文件,當然也可以直接寫入數據庫的表中。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.配置並打開慢查詢日誌","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(1)在 MySQL Server 中臨時開啓慢查詢功能","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 MySQL Server 中,默認情況慢查詢功能是關閉的,我們可以通過查看此功能的狀態","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"show variables like 'slow_query_log'; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/75/758308c7952bb537f2ce5245660cd809.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,慢查詢日誌(slow_query_log )的狀態爲關閉。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以使用以下命令開啓並配置慢查詢日誌功能,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在 mysql 中執行以下命令","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SET GLOBAL slow_query_log = 'ON';\nSET GLOBAL slow_query_log_file = '/var/log/mysql/kalacloud-slow.log';\nSET GLOBAL log_queries_not_using_indexes = 'ON';\nSET SESSION long_query_time = 1;\nSET SESSION min_examined_row_limit = 100;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"SET GLOBAL slow_query_log","attrs":{}}],"attrs":{}},{"type":"text","text":" :全局開啓慢查詢功能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"SET GLOBAL slow_query_log_file","attrs":{}}],"attrs":{}},{"type":"text","text":" :指定慢查詢日誌存儲文件的地址和文件名。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"SET GLOBAL log_queries_not_using_indexes","attrs":{}}],"attrs":{}},{"type":"text","text":":無論是否超時,未被索引的記錄也會記錄下來。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"SET SESSION long_query_time","attrs":{}}],"attrs":{}},{"type":"text","text":":慢查詢閾值(秒),SQL 執行超過這個閾值將被記錄在日誌中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"SET SESSION min_examined_row_limit","attrs":{}}],"attrs":{}},{"type":"text","text":":慢查詢僅記錄掃描行數大於此參數的 SQL。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"**特別注意:**在實踐中常常會碰到無論慢查詢閾值調到多小,日誌就是不被記錄。這個問題很有可能是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"min_examined_row_limit","attrs":{}}],"attrs":{}},{"type":"text","text":" 行數過大,導致沒有被記錄。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"min_examined_row_limit","attrs":{}}],"attrs":{}},{"type":"text","text":" 在配置中常被忽略,這裏要特別注意。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們來執行查詢語句,看看配置。(在 MySQL Server 中執行)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"show variables like 'slow_query_log%';\nshow variables like 'log_queries_not_using_indexes';\nshow variables like 'long_query_time';\nshow variables like 'min_examined_row_limit';\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9e/9e92c9e3c33d2c3cd16c336fe050a7b4.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上修改 MySQL 慢查詢配置的方法是用在","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"臨時監測數據庫運行狀態","attrs":{}},{"type":"text","text":"的場景下,當 MySQL Server 重啓時,以上修改全部失效並恢復原狀。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擴展閱讀:","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-manage-and-use-mysql-database-triggers/","title":"","type":null},"content":[{"type":"text","text":"六類 MySQL 觸發器使用教程及應用場景實戰案例","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(2)將慢查詢設置寫入 MySQL 配置文件,永久生效","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然我們可以在命令行中對慢查詢進行動態設置,但動態設置會隨着重啓服務而失效。如果想長期開啓慢查詢功能,需要把慢查詢的設置寫入 MySQL 配置文件中,這樣無論是重啓服務器,還是重啓 MySQL ,慢查詢的設置都會保持不變。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL conf 配置文件通常在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"/etc","attrs":{}}],"attrs":{}},{"type":"text","text":" 或 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"/usr","attrs":{}}],"attrs":{}},{"type":"text","text":" 中。我們可以使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"find","attrs":{}}],"attrs":{}},{"type":"text","text":" 命令找到配置文件具體的存放位置。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo find /etc -name my.cnf\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d6/d6e6585002430ca97819bc9f9105bd65.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"找到位置後,使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nano","attrs":{}}],"attrs":{}},{"type":"text","text":" 編輯 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"my.cnf","attrs":{}}],"attrs":{}},{"type":"text","text":" 將慢查詢設置寫入配置文件。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo nano /etc/mysql/my.cnf\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"[mysqld]\n\nslow-query-log = 1\nslow-query-log-file = /var/log/mysql/localhost-slow.log\nlong_query_time = 1\nlog-queries-not-using-indexes\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nano","attrs":{}}],"attrs":{}},{"type":"text","text":" 打開配置文件,把上面的的代碼寫在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"[mysqld]","attrs":{}}],"attrs":{}},{"type":"text","text":" 的下面即可。 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ctrl+X","attrs":{}}],"attrs":{}},{"type":"text","text":" 保存退出。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo systemctl restart mysql\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"重啓 MySQL Server 服務,使剛剛修改的配置文件生效。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"**特別注意:**直接在命令行中設置的慢查詢動態變量與直接寫入 my.cnf 配置文件的語法有所不同。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擴展閱讀:10種 ","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/blog/best-mysql-gui-tools/","title":"","type":null},"content":[{"type":"text","text":"MySQL 管理工具","attrs":{}}]},{"type":"text","text":" 橫向測評 - 免費和付費到底怎麼選?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"舉例:動態變量是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"slow_query_log","attrs":{}}],"attrs":{}},{"type":"text","text":",寫入配置文件是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"slow-query-log","attrs":{}}],"attrs":{}},{"type":"text","text":"。這裏要特別注意。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更多 MySQL 8.0 動態變量語法可查看 ","attrs":{}},{"type":"link","attrs":{"href":"https://dev.mysql.com/doc/refman/8.0/en/dynamic-system-variables.html","title":"","type":null},"content":[{"type":"text","text":"MySQL 官方文檔","attrs":{}}]},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二. 使用慢查詢功能記錄日誌","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到這裏我們已經配置好慢查詢功能所需要的一切。下面咱們寫一個示例,在這個示例中我們來一起學習如何查看和分析慢查詢日誌。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以打開兩個連接到服務器的命令行窗口,一個用來寫 MySQL 代碼,另一個用來查看日誌。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意:以下教程中,有些代碼是在命令行中執行,有些是在 MySQL Server 中執行,請注意分辨。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"登錄 MySQL Server,創建一個數據庫,寫入一組示例數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"CREATE DATABASE kalacloud_demo;\nUSE kalacloud_demo;\nCREATE TABLE users ( id TINYINT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) );\nINSERT INTO users (name) VALUES ('Jack Ma'),('Lei Jun'),('Wang Xing'),('Pony Ma'),('Zhang YiMing'),('Ding Lei'),('Robin Li'),('Xu Yong'),('Huang Zheng'),('Richard Liu');\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了保證大家與教程配置保持一致,咱們一起使用動態變量,再設置一邊慢查詢參數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 MySQL Server 中執行以下 SQL 代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SET GLOBAL slow_query_log = 1;\nSET GLOBAL slow_query_log_file = '/var/log/mysql/kalacloud-slow.log';\nSET GLOBAL log_queries_not_using_indexes = 1;\nSET long_query_time = 10;\nSET min_examined_row_limit = 0;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在我們有了一個表中有數據的示例數據庫。慢查詢功能也已經打開,我們特意把時間閾值(long_query_time)設置爲 10 並且把最小行(min_examined_row_limit)設置爲 0。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們來運行一段代碼測試一下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"USE kalacloud_demo;\nSELECT * FROM users WHERE id = 1;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用主鍵索引對錶進行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"select","attrs":{}}],"attrs":{}},{"type":"text","text":" 查詢,這種查詢速度非常快,又使用了索引。因此慢查詢日誌中不會有任何記錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們打開慢查詢日誌,驗證一下是否有記錄,在命令行中執行以下命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo cat /var/log/mysql/kalacloud-slow.log\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":"還沒有任何記錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b9/b9313cd2c306ce3a1797c8a3ff21a7be.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們在 MySQL Server 中執行以下代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SELECT * FROM users WHERE name = 'Wang Xing';\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這段查詢代碼使用非索引列(name)來進行查詢,所以慢查詢日誌在會記錄下這個查詢。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們打開日誌查看記錄變化:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo cat /var/log/mysql/kalacloud-slow.log\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ef/ef6ea84e57b67d7026f5d146a2b4d26a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以看到這個非索引查詢,已經被記錄在慢查詢日誌中了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再舉個例子。我們提高最小檢查行(min_examined_row_limit)的檢查行數設置爲 100,然後再執行查詢。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 MySQL Server 中執行以下代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SET min_examined_row_limit = 100;\nSELECT * FROM users WHERE name = 'Zhang YiMing';\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行後,再打開 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":" ,可以看到條小於 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"100","attrs":{}}],"attrs":{}},{"type":"text","text":" 行的查詢,沒有被記錄到日誌中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"特別注意","attrs":{}},{"type":"text","text":":如果慢查詢日誌中,沒有記錄任何數據,可以檢查以下內容。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(1)創建日誌的目錄權限問題,是否有對應的權限。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"cd /var/log\nmkdir mysql\nchmod 755 mysql\nchown mysql:mysql mysql\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(2)另一個可能是查詢變量配置問題,把 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"my.conf","attrs":{}}],"attrs":{}},{"type":"text","text":" 文件內有關慢查詢的配置清乾淨,然後重啓服務,重新配置。看看是不是這裏出的問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擴展閱讀:","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-save-mysql-mariadb-query-output-to-a-file/","title":"","type":null},"content":[{"type":"text","text":"如何將 MySQL 的查詢結果保存到文件","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三. 慢查詢日誌記錄參數詳解","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們來講解慢查詢日誌應該如何分析","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ef/ef6ea84e57b67d7026f5d146a2b4d26a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"日誌中信息的說明:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Time","attrs":{}}],"attrs":{}},{"type":"text","text":" :被日誌記錄的代碼在服務器上的運行時間。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"User@Host","attrs":{}}],"attrs":{}},{"type":"text","text":":誰執行的這段代碼。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Query_time","attrs":{}}],"attrs":{}},{"type":"text","text":":這段代碼運行時長。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Lock_time","attrs":{}}],"attrs":{}},{"type":"text","text":":執行這段代碼時,鎖定了多久。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Rows_sent","attrs":{}}],"attrs":{}},{"type":"text","text":":慢查詢返回的記錄。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Rows_examined","attrs":{}}],"attrs":{}},{"type":"text","text":":慢查詢掃描過的行數。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些被記錄的信息非常有意義,所有超過閾值的代碼都會被記錄在日誌中,我們可以通過這些信息找到 MySQL 查詢時效率不佳的代碼,有助於我們優化 MySQL 性能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擴展閱讀:","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/blog/find-all-tables-with-specific-column-names-in-mysql/","title":"","type":null},"content":[{"type":"text","text":"如何在 MySQL 裏查詢數據庫中帶有某個字段的所有表名","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四. 使用 mysqldumpslow 工具對慢查詢日誌進行分析","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際工作中,慢查詢日誌可不像上文描述的那樣,僅僅有幾行記錄。現實中慢查詢日誌會記錄大量慢查詢信息,寫入也非常頻繁。日誌記錄的內容會越來越長,分析數據也變的困難。 好在 MySQL 內置了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 工具,它可以把相同的 SQL 歸爲一類,並統計出歸類項的執行次數和每次執行的耗時等一系列對應的情況。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來執行幾行代碼讓慢查詢日誌記錄下來,然後再用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行分析。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上文我們把","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"min_examined_row_limit","attrs":{}}],"attrs":{}},{"type":"text","text":" 設置爲 100,在這裏,我們要將它改爲 0 ,慢查詢纔能有記錄。在 MySQL Server 中執行以下代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SET min_examined_row_limit = 0;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們執行幾條查詢命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SELECT * FROM users WHERE name = 'Wang Xing';\nSELECT * FROM users WHERE name = 'Huang Zheng';\nSELECT * FROM users WHERE name = 'Zhang YiMing';\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據前文的慢查詢設置,這三條記錄都將被記錄在日誌中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我們切換到命令行的窗口中","attrs":{}},{"type":"text","text":",執行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo mysqldumpslow -s at /var/log/mysql/kalacloud-slow.log\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回的數據:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e66c8d435201f58a4d01999508321e35.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以看到,返回的數據中,已經把三條類似的 SQL 語句記錄抽象成一條記錄","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SELECT * FROM users WHERE name = 'S'","attrs":{}}],"attrs":{}},{"type":"text","text":" 並且針對這條記錄列出了對應的總量和平均量的記錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"常見的","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"命令","attrs":{}},{"type":"text","text":" 平時大家也可以根據自己的常用需求來總結,存好這些腳本備用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow -s at -t 10 kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":":平均執行時長最長的前 10 條 SQL 代碼。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow -s al -t 10 kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":":平均鎖定時間最長的前10條 SQL 代碼。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow -s c -t 10 kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":":執行次數最多的前10條 SQL 代碼。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow -a -g 'user' kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":":顯示所有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"user","attrs":{}}],"attrs":{}},{"type":"text","text":" 表相關的 SQL 代碼的具體值","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow -a kalacloud-slow.log","attrs":{}}],"attrs":{}},{"type":"text","text":":直接顯示 SQL 代碼的情況。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 的參數命令","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]\n\nParse and summarize the MySQL slow query log. Options are\n\n --verbose verbose\n --debug debug\n --help write this text to standard output\n -v verbose\n -d debug\n -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default\n al: average lock time\n ar: average rows sent\n at: average query time\n c: count\n l: lock time\n r: rows sent\n t: query time\n -r reverse the sort order (largest last instead of first)\n -t NUM just show the top n queries\n -a don't abstract all numbers to N and strings to 'S'\n -n NUM abstract numbers with at least n digits within names\n -g PATTERN grep: only consider stmts that include this string\n -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),\n default is '*', i.e. match all\n -i NAME name of server instance (if using mysql.server startup script)\n -l don't subtract lock time from total time\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"常用的參數講解:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"-s","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"al:平均鎖定時間","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"at:平均查詢時間 [默認]","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ar:平均返回記錄時間","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"c:count 總執行次數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"l:鎖定時間","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"r:返回記錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"t:查詢時間","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"-t","attrs":{}}],"attrs":{}},{"type":"text","text":":返回前 N 條的數據","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"-g","attrs":{}}],"attrs":{}},{"type":"text","text":":可寫正則表達,類似於 grep 命令,過濾出需要的信息。如,只查詢 X 表的慢查詢記錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"-r","attrs":{}}],"attrs":{}},{"type":"text","text":":rows sent 總返回行數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 日誌查詢工具好用就好用在它特別靈活,又可以合併同類項式的分析慢查詢日誌。我們在日常工作的使用中,就能夠體會 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 的好用之處。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 的使用參數也可在 ","attrs":{}},{"type":"link","attrs":{"href":"https://dev.mysql.com/doc/refman/8.0/en/mysqldumpslow.html","title":"","type":null},"content":[{"type":"text","text":"MySQL 8.0 使用手冊","attrs":{}}]},{"type":"text","text":" 中找到。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擴展閱讀:","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-get-the-sizes-of-the-tables-of-a-mysql-database/","title":"","type":null},"content":[{"type":"text","text":"如何查看 MySQL 數據庫、表、索引容量大小","attrs":{}}]},{"type":"text","text":"?找到佔用空間最大的表","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"五. Profilling - MySQL 性能分析工具","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了更精準的定位一條 SQL 語句的性能問題,我們需要拆分這條語句運行時到底在什麼地方消耗了多少資源。 我們可以使用 Profilling 工具來進行這類細緻的分析。我們可通過 Profilling 工具獲取一條 SQL 語句在執行過程中對各種資源消耗的細節。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"進入 MySQL Server 後,執行以下代碼,啓動 Profilling","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SET SESSION profiling = 1; \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"檢查 profiling 的狀態","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SELECT @@profiling;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回數據: 0 表示未開啓,1 表示已開啓。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ae/ae5abdc4e1ce094e75dc9d2b536c481a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行需要定位問題的 SQL 語句。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"USE kalacloud_demo;\nSELECT * FROM users WHERE name = 'Jack Ma';\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看 SQL 語句狀態。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SHOW PROFILES;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打開 profiling 後,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SHOW PROFILES;","attrs":{}}],"attrs":{}},{"type":"text","text":" 會顯示一個將 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Query_ID","attrs":{}}],"attrs":{}},{"type":"text","text":" 鏈接到 SQL 語句的表。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4f/4f771bdda2f733c660eb4a263a027840.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Query_ID","attrs":{}}],"attrs":{}},{"type":"text","text":":SQL 語句的 ID 編號。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Duration","attrs":{}}],"attrs":{}},{"type":"text","text":":SQL 語句執行時長。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Query","attrs":{}}],"attrs":{}},{"type":"text","text":":具體的 SQL 語句。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行以下 SQL 代碼,將 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"[# Query_ID]","attrs":{}}],"attrs":{}},{"type":"text","text":" 替換爲我們要分析的 SQL 代碼","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Query_ID","attrs":{}}],"attrs":{}},{"type":"text","text":"的編號。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SHOW PROFILE CPU, BLOCK IO FOR QUERY [# Query_ID];\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SHOW PROFILE CPU, BLOCK IO FOR QUERY 4;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b4/b4e3b259d7d04c581cb996210fa1c761.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Status","attrs":{}}],"attrs":{}},{"type":"text","text":" 是執行查詢過程中的具體步驟,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Duration","attrs":{}}],"attrs":{}},{"type":"text","text":" 是完成該步驟所需的時間(以秒爲單位)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以根據這些細節來具體分析,如何優化對應的 SQL 代碼。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"六. 慢查詢教程總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"慢查詢是讓我們看到數據庫真實運行狀態的工具,對服務器和數據庫性能優化有着指導性的意義。無論是生產環境、開發、QA,都可以謹慎的打開慢查詢來記錄性能日誌。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以先把動態變量","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"long_query_time","attrs":{}}],"attrs":{}},{"type":"text","text":" 設置的大一些,觀察一下,然後在進行微調。有了慢查詢日誌,我們就有了優化性能的方向和目標,再使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqldumpslow","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"profiling","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行宏觀和微觀的日誌分析。找到低效 SQL 語句的細節,進行微調,最終使我們的系統可以獲得最佳執行性能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至此,MySQL 慢查詢日誌我們就講解完了,如果你週期性的查看 log 日誌,可以使用卡拉雲搭一個日誌看板,自己不僅查看、分析數據方便,還可以一鍵分享給組內的小夥伴共享數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"卡拉雲是新一代低代碼開發工具,免安裝部署,可一鍵接入包括 MySQL 在內的常見數據庫及 API。不僅可以像命令行一樣靈活,還可根據自己的工作流,定製開發。無需繁瑣的前端開發,只需要簡單拖拽,即可快速搭建企業內部工具。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數月的開發工作量,使用卡拉雲後可縮減至數天","attrs":{}},{"type":"text","text":",歡迎使用我開發的","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/?utm_medium=register","title":"","type":null},"content":[{"type":"text","text":"卡拉雲","attrs":{}}]},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8b/8bcdd52d6492ff4ada86801282362bbe.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"卡拉雲可快速接入的常見數據庫及 API","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"卡拉雲可根據公司工作流需求,輕鬆搭建數據看板,並且可分享給組內的小夥伴共享數據","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/5400a60956e16d655e0297c5d6e5a8d2.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"僅需拖拽一鍵生成前端代碼,簡單一行代碼即可映射數據到指定組件中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/abab46cb6a2c70c2b3e2e944c5d4610e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"卡拉雲可直接添加導出按鈕,導出適用於各類分析軟件的數據格式,方便快捷。立即開通","attrs":{}},{"type":"link","attrs":{"href":"https://kalacloud.com/?utm_medium=register","title":"","type":null},"content":[{"type":"text","text":"卡拉雲","attrs":{}}]},{"type":"text","text":",快速搭建屬於你自己的後臺管理系統。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有關 MySQL 教程,可繼續拓展學習:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-allow-remote-access-to-mysql/","title":"","type":null},"content":[{"type":"text","text":"如何遠程連接 MySQL 數據庫,阿里雲騰訊雲外網連接教程","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-import-and-export-databases-excel-csv-in-mysql-or-mariadb-from-terminal/","title":"","type":null},"content":[{"type":"text","text":"如何在 MySQL / MariaDB 中導入導出數據,導入導出數據庫文件、Excel、CSV","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-migrate-a-mysql-database-between-two-servers-aliyun-tencentyun/","title":"","type":null},"content":[{"type":"text","text":"如何在兩臺服務器之間遷移 MySQL 數據庫 阿里雲騰訊雲遷移案例","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kalacloud.com/blog/how-to-use-the-mysql-blob-data-type-to-store-images-with-php-or-kalacloud/","title":"","type":null},"content":[{"type":"text","text":"MySQL 中如何實現 BLOB 數據類型的存取,BLOB 有哪些應用場景?","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kalacloud.com/blog/mysql-workbench-tutorial/","title":"","type":null},"content":[{"type":"text","text":"如何使用 MySQL Workbench 操作 MySQL / MariaDB 數據庫中文指南","attrs":{}}]}]}]}],"attrs":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章