生產環(huán)境MySQL索引時效的排查過程
更新時間:2022年04月10日 19:59:21 作者:那海藍藍
這篇文章主要介紹了生產環(huán)境MySQL索引時效的排查過程,文章根據SQL查詢耗時特別長,看了執(zhí)行計劃發(fā)現沒有走索引的問題展開詳細介紹,需要的朋友可以參考一下
早上收到開發(fā)同學求助,有個SQL查詢耗時特別長,看了執(zhí)行計劃發(fā)現沒有走索引,但是不知道原因在哪里,我們一起來分析一下。
mysql>explain SELECT
*
FROM
artisan_income
WHERE
parent_id IN (
222645481,
222583953,
222181775,
222180931,
222081126,
221678753,
221616102,
221591783,
221219312,
221195482,
221118672,
220763129,
220654289,
220633930,
220323633,
220227641,
219825564,
219720338,
219321345,
219291958
) \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: artisan_income
partitions:
type: ALL
possible_keys: idx_parent_id
key:
key_len:
ref:
rows: 20711352
filtered: 100
Extra: Using where
確實是全表掃描,帶著疑問我們把生產環(huán)境數據同步到測試庫,方便測試,然后在測試環(huán)境進行查詢。
mysql>explain SELECT
*
FROM
artisan_income
WHERE
parent_id IN (
222645481,
222583953,
222181775,
222180931,
222081126,
221678753,
221616102,
221591783,
221219312,
221195482,
221118672,
220763129,
220654289,
220633930,
220323633,
220227641,
219825564,
219720338,
219321345,
219291958
) \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: artisan_income
partitions:
type: range
possible_keys: idx_parent_id
key: idx_parent_id
key_len: 5
ref:
rows: 1870780
filtered: 100
Extra: Using index condition
發(fā)現在測試環(huán)境就用到了parent_id字段的索引,生產庫和測試庫同樣都是5.7的版本,數據也幾乎一樣,但是執(zhí)行計劃不一樣,第一時間想到了統計信息的問題,于是進行analyze table
analyze table artisan_income;
然后查看執(zhí)行計劃:
mysql>explain SELECT
*
FROM
artisan_income
WHERE
parent_id IN (
222645481,
222583953,
222181775,
222180931,
222081126,
221678753,
221616102,
221591783,
221219312,
221195482,
221118672,
220763129,
220654289,
220633930,
220323633,
220227641,
219825564,
219720338,
219321345,
219291958
) \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: artisan_income
partitions:
type: range
possible_keys: idx_parent_id
key: idx_parent_id
key_len: 5
ref:
rows: 1901880
filtered: 100
Extra: Using index condition
發(fā)現執(zhí)行計劃已經恢復正常。
mysql>SELECT * FROM artisan_income WHERE parent_id IN ( 222645481, 222583953, 222181775, 222180931, 222081126, 221678753, 221616102, 221591783, 221219312, 221195482, 221118672, 220763129, 220654289, 220633930, 220323633, 220227641, 219825564, 219720338, 219321345, 219291958 ) \G 返回行數:[0],耗時:2 ms.
到此這篇關于生產環(huán)境MySQL索引時效的排查過程的文章就介紹到這了,更多相關MySQL索引排查內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

