MySQL 慢查詢日誌

來源:互聯網
上載者:User

標籤:www   記錄   test   多個   重啟   version   set   需要   width   

MySQL有一種日誌,叫做慢查詢日誌,主要就是用來記錄一些耗時的查詢操

 

作。通過這個日誌我們就可以分析出哪些的操作是影響效能的,我們需要對其

 

進行一些最佳化措施。

  

查看開啟狀態

 

上面的是我在 windows 下安裝的 MySQL5.7 版本,我們可以發現,這個版本是開啟了慢查詢的。我在 CentOS6.9 下採用 yum 的方式安裝的 MySQL5.7 預設沒有開啟慢查詢日誌。不管預設有沒有給我們開啟,我們是需要瞭解慢查詢日誌是如何開啟的,開啟的方式也非常簡單。找到 MySQL 的設定檔,Windows 下是 my.ini,Linux 下的是 my.cnf。進行如下配置就可以了。

slow-query-log=1


slow_query_log_file="mysql-slow.log"


long_query_time=10

第一行是指定開啟慢查詢日誌

第二行是指定慢查詢日誌的路徑

第三行是指定查詢時間大於多少的才進行記錄,但是是毫秒,也就是操作大於 10ms 的操作都會被記錄。

配置完畢之後要重啟 MySQL 生效。

下面來看看慢查詢日誌的內容

 1 C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqld.exe, Version: 5.7.16-log (MySQL 2 Community Server (GPL)). started with: 3 TCP Port: 3306, Named Pipe: (null) 4 TimeId CommandArgument 5 #Time: 2017-07-07T06:35:46.995201Z 6 #[email protected]: root[root] @ localhost [::1]  Id:10 7 #Query_time: 12.522116 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 483968 use test; 8 SET timestamp=1499409346; 9 insert into test (id,name) (select uuid() id,name from test);10 #Time: 2017-07-07T06:36:15.258316Z11 #[email protected]: root[root] @ localhost [::1]  Id:1012 #Query_time: 24.543267 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 967936 SET timestamp=1499409375;13 insert into test (id,name) (select uuid() id,name from test);14 #Time: 2017-07-07T06:37:15.021922Z15 #[email protected]: root[root] @ localhost [::1]  Id:1016 #Query_time: 56.283040 Lock_time: 0.000499 Rows_sent: 0 Rows_examined: 1935872 SET timestamp=1499409435;17 insert into test (id,name) (select uuid() id,name from test);18 #Time: 2017-07-07T06:40:07.866659Z19 #[email protected]: root[root] @ localhost [::1]  Id:1020 #Query_time: 133.866927 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 3871744 SET timestamp=1499409607;21 insert into test (id,name) (select uuid() id,name from test);

 

我們可以看到在 2017-07-07 日,有多個慢查詢產生。單獨抽取一組,如下

1 #Time: 2017-07-07T06:35:46.995201Z2 #[email protected]: root[root] @ localhost [::1]  Id:103 #Query_time: 12.522116 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 483968 use test;4 SET timestamp=1499409346;5 insert into test (id,name) (select uuid() id,name from test);

Query_time 表示的是耗時時間

下面是一些操作,這的主要操作就是一個 insert

這就是慢查詢日誌。

MySQL 慢查詢日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.