1. 程式人生 > 其它 >MySQL配置引數log_queries_not_using_indexes

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. SeeSection5.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');

慢日誌記錄如下: