MySQL配置參數log_queries_not_using_indexes


MySQL慢日志相關配置參數

mysql數據庫的配置參數log_queries_not_using_indexes的作用是控制未使用索引的查詢是否寫入慢日志。

背景

在一次項目上線后,監控平台馬上收到大量報警。報警內容均是業務存在大量慢sql。將報警sql取出進行分析,發現這些sql運行效率並不低。經過排查因為這些sql在執行過程中未使用到索引,被記錄到slow log中。該行為由MySQL參數 log_queries_not_using_indexes 控制

參數解釋

在啟用 log_queries_not_using_indexes 時,未使用到索引的sql會被記錄到slow log中。但是此選項不一定意味不使用索引,我們先來看一下官方文檔給出的解釋:

If you enable this variable with the slow query log enabled, queries that are expected to retrieve all rows are logged. See Section 5.4.5, “The Slow Query Log”. This option does not necessarily mean that no index is used. For example, a query that uses a full index scan uses an index but would be logged because the index would not limit the number of rows

如果在啟用慢速查詢日志的情況下啟用此變量,則會記錄預期檢索所有行的查詢

此選項不一定意味着不使用索引。例如,使用完整索引掃描的查詢使用索引,但會被記錄,因為索引不會限制行數

示例:

創建測試表並插入數據,表結構如下:

CREATE TABLE `tab_test` (
`id` int(11) NOT NULL,
`name` varchar(10) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into tab_test values(1,'aaa','xxxxxxxxxxxxxxx');
insert into tab_test values(2,'bbb','yyyyyyyyyyyyyyy');
insert into tab_test values(3,'ccc','zzzzzzzzzzzzzzz');

分別執行如下查詢語句,跟綜慢sql日志:

select * from tab_test;
select name from tab_test;
select * from tab_test where name='aaa';
select * from tab_test where name in('aaa','bbb','ccc');
select * from tab_test where name in('aaa');

慢日志記錄如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM