mysql slow log慢日志精講與profile工具分析原因

slow-log

前提:拿存儲過程做實(shí)驗?zāi)M慢語句

下面的存儲過程定義了向testlog表插入十萬行記錄瞎疼,不顯式開啟一個事務(wù)放進(jìn)存儲過程导犹,而直接執(zhí)行存儲過程,會被認(rèn)為是存儲過程內(nèi)定義的十萬次insert的DML操作溺森,事務(wù)日志刷寫級別innodb_flush_log_at_trx_commit無論是 0|1|2 都很慢癞己,模擬慢語句,超過慢語句設(shè)置的閾值時間10S 達(dá)標(biāo)似将。

(root@localhost) [hellodb]> create table testlog(id int auto_increment primary key,name char(10),age int default 20);
Query OK, 0 rows affected (0.03 sec)
(root@localhost) [hellodb]> delimiter //
(root@localhost) [hellodb]> create procedure sp_testlog()
begin
declare i int;
set i=1;
while i <= 100000
do insert into testlog(name,age) values(concat("wang",i),i);
set i=i+1;
end while;
end//
Query OK, 0 rows affected (0.03 sec)

(root@localhost) [hellodb]> delimiter ;

1.查看事務(wù)日志刷寫級別和慢日志閾值時間

(root@localhost) [hellodb]> select @@innodb_flush_log_at_trx_commit;
+----------------------------------+
| @@innodb_flush_log_at_trx_commit |
+----------------------------------+
|                                2 |
+----------------------------------+
1 row in set (0.01 sec)
#即便此時事務(wù)日志刷寫級別是2 但是存儲過程定義的十萬條DML語句被認(rèn)為十萬次事務(wù)获黔,2級別每次事務(wù)提交后不會立即刷寫到磁盤 而是將已提交的事務(wù)從log-buffer放到os-buffer中,每秒執(zhí)行一次os-buffer內(nèi)所有事務(wù)的落盤操作在验。

(root@localhost) [hellodb]> select @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
|         10.000000 |
+-------------------+
1 row in set (0.00 sec)
#閾值是十秒 夠了

2.執(zhí)行存儲過程

執(zhí)行存儲過程 模擬慢語句 此時該語句會被記錄在慢日志中

(root@localhost) [hellodb]> call sp_testlog;
Query OK, 1 row affected (31.81 sec)
#我們看到該語句執(zhí)行時間31秒 慢日志當(dāng)然也是31秒 生產(chǎn)中都是代碼生成的查詢語句 我們不可能交互式看時間玷氏。而是從日志看慢語句
(root@localhost) [hellodb]> select * from testlog;

3.分析慢日志

a.直接查看日志文件

root@17  log]# cat slow-log
/usr/local/mysql/bin/mysqld, Version: 8.0.19 (MySQL Community Server - GPL). started with:
Tcp port: 3306  Unix socket: /mysql/3306/sock/mysql.sock
Time                 Id Command    Argument
# Time: 2021-01-16T14:28:56.102633Z
# User@Host: root[root] @ localhost []  Id:     8
# Query_time: 31.805307 #這里就是超過閾值10s的慢語句執(zhí)行時間31S Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
use hellodb;
SET timestamp=1610807336;
call sp_testlog; #這個是超過閾值的慢語句

b. mysqldumpslow分析日志文件
這兩個例子里面我因為設(shè)置了不走索引的語句記錄慢日志,所以select. * from testlog即使時間沒有超過閾值10S 也會被記錄下

b.1 以慢查詢語句出現(xiàn)的次數(shù)排序mysqldumpslow -s c -t 10 slow-log

root@17 log]# mysqldumpslow -s c -t 10 slow-log

Reading mysql slow query log from slow-log
Count: 2 Time=0.06s (0s) Lock=0.00s (0s) Rows=150000.0 (300000), root[root]@localhost
select * from testlog

Count: 1 Time=24.52s (24s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
call sp_testlog

Died at /usr/local/mysql/bin/mysqldumpslow line 162, <> chunk 3.

b.2 以慢查詢語句的平均時間排序mysqldumpslow -s t -t 10 slow-log

root@17  log]# mysqldumpslow -s t -t 10 slow-log

Reading mysql slow query log from slow-log
Count: 1  Time=24.52s (24s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  call sp_testlog

Count: 2  Time=0.06s (0s)  Lock=0.00s (0s)  Rows=150000.0 (300000), root[root]@localhost
  select * from testlog

Died at /usr/local/mysql/bin/mysqldumpslow line 162, <> chunk 3.

c. 使用profile工具
當(dāng)我們通過慢日志記錄慢語句有兩種情況:
第一種:大于慢日志設(shè)置時間閾值

(root@localhost) [(none)]> select @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
|         10.000000 |
+-------------------+
1 row in set (0.00 sec)

第二種:不使用索引或使用全索引掃描腋舌,不論是否達(dá)到慢日志查詢時間閾值 都會記錄日志盏触。默認(rèn)log_queries_not_using_indexes=0不記錄

(root@localhost) [(none)]> select @@log_queries_not_using_indexes;
+---------------------------------+
| @@log_queries_not_using_indexes |
+---------------------------------+
|                               1 |
+---------------------------------+
1 row in set (0.00 sec)

但是 我們通過上面介紹的mysqldumpslow工具分析出慢日志語句有哪些,通過執(zhí)行計劃只能看到該語句走沒走索引 或者走索引了 走的是輔助索引還是主鍵索引,回表次數(shù)赞辩。不清楚具體執(zhí)行階段哪里慢雌芽。通過profile工具可以查詢出語句執(zhí)行具體步驟

下面開始介紹profile工具的使用

profile工具

profiling只是系統(tǒng)變量 不是命令選項和配置選項。所以只能系統(tǒng)內(nèi)部set profiling使用
c.1 使用流程

(root@localhost) [hellodb]> show variables like '%profi%';
+------------------------+
-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+

#打開后诗宣,會顯示語句執(zhí)行詳細(xì)的過程
set profiling = ON

#查看語句,注意結(jié)果中的query_id值
show profiles

#顯示語句的詳細(xì)執(zhí)行步驟和時長
Show profile for query N    # N為show profiles查詢出來的query_ID 

#profile工具歷史記錄15條
set profiling_history_size=15

c.2 制造慢語句 使用profile工具查詢?yōu)樯堵?/strong>

(root@localhost) [hellodb]> set profiling = ON
(root@localhost) [hellodb]> select * from testlog where id>1000;
(root@localhost) [hellodb]> select sleep(1) from teachers;
+----------+
| sleep(1) |
+----------+
|        0 |
|        0 |
|        0 |
|        0 |
+----------+
4 rows in set (4.01 sec)

(root@localhost) [hellodb]> show profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration   | Query                               |
+----------+------------+-------------------------------------+
|        1 | 0.10465625 | select * from testlog where id>1000 |
|        2 | 0.00019875 | show prifiling                      |
|        3 | 2.17644475 | select sleep(1) from teachers       |
|        4 | 4.00434700 | select sleep(1) from teachers       |
|        5 | 0.09275425 | select * from testlog where id>1000 |
+----------+------------+-------------------------------------+
5 rows in set, 1 warning (0.00 sec)

# 我們使用show profile for query 4 查詢 第四條select sleep(1) from teachers 為啥這么慢
(root@localhost) [hellodb]> show profile for query 4;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000078 |
| Executing hook on transaction  | 0.000005 |
| starting                       | 0.000008 |
| checking permissions           | 0.000006 |
| Opening tables                 | 0.000027 |
| init                           | 0.000007 |
| System lock                    | 0.000008 |
| optimizing                     | 0.000004 |
| statistics                     | 0.000014 |
| preparing                      | 0.000013 |
| executing                      | 0.000027 |
| User sleep #sleep 函數(shù)休息了一秒  | 1.000131 |
| User sleep                     | 1.000784 |
| User sleep                     | 1.002001 |
| User sleep                     | 1.001088 |
| end                            | 0.000024 |
| query end                      | 0.000006 |
| waiting for handler commit     | 0.000064 |
| closing tables                 | 0.000017 |
| freeing items                  | 0.000024 |
| cleaning up                    | 0.000013 |
+--------------------------------+----------+
21 rows in set, 1 warning (0.00 sec)
#查詢得出原來中間用了四次sleep函數(shù)休息了一秒膘怕,此時可以診斷出問題了


(root@localhost) [hellodb]> show profiles;
+----------+------------+-----------------------+
| Query_ID | Duration   | Query                 |
+----------+------------+-----------------------+
|        1 | 0.19421100 | select * from testlog |
+----------+------------+-----------------------+
1 row in set, 1 warning (0.00 sec)

(root@localhost) [hellodb]> show profile for query 1;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000302 |
| Executing hook on transaction  | 0.000022 |
| starting                       | 0.000073 |
| checking permissions           | 0.000022 |
| Opening tables                 | 0.000117 |
| init                           | 0.000018 |
| System lock                    | 0.000075 |
| optimizing                     | 0.000018 |
| statistics                     | 0.000084 |
| preparing                      | 0.000117 |
| executing                      | 0.193194 |
| end                            | 0.000025 |
| query end                      | 0.000006 |
| waiting for handler commit     | 0.000012 |
| closing tables                 | 0.000013 |
| freeing items                  | 0.000012 |
| logging slow query             | 0.000083 |
| cleaning up                    | 0.000022 |
+--------------------------------+----------+
18 rows in set, 1 warning (0.01 sec)
#這個就是純屬于select * from testlog沒走索引 executing執(zhí)行時間太長了
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市召庞,隨后出現(xiàn)的幾起案子岛心,更是在濱河造成了極大的恐慌,老刑警劉巖篮灼,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件忘古,死亡現(xiàn)場離奇詭異,居然都是意外死亡诅诱,警方通過查閱死者的電腦和手機(jī)髓堪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來娘荡,“玉大人干旁,你說我怎么就攤上這事∨阢澹” “怎么了争群?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長大年。 經(jīng)常有香客問我换薄,道長,這世上最難降的妖魔是什么翔试? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任轻要,我火速辦了婚禮,結(jié)果婚禮上垦缅,老公的妹妹穿的比我還像新娘冲泥。我一直安慰自己,他們只是感情好壁涎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布柏蘑。 她就那樣靜靜地躺著,像睡著了一般粹庞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上洽损,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天庞溜,我揣著相機(jī)與錄音,去河邊找鬼。 笑死流码,一個胖子當(dāng)著我的面吹牛又官,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播漫试,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼六敬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了驾荣?” 一聲冷哼從身側(cè)響起外构,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎播掷,沒想到半個月后审编,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡歧匈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年垒酬,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片件炉。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡勘究,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出斟冕,到底是詐尸還是另有隱情口糕,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布宫静,位于F島的核電站走净,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏孤里。R本人自食惡果不足惜伏伯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望捌袜。 院中可真熱鬧说搅,春花似錦、人聲如沸虏等。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽霍衫。三九已至候引,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間敦跌,已是汗流浹背澄干。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人麸俘。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓辩稽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親从媚。 傳聞我的和親對象是個殘疾皇子逞泄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355