近日在項目中嘗試使用如下語句返回列表
select * from dtu_log as l left join e as e on l.dlog_ele=e.id where 1=1 and e.maint_com=1 order by dlog_id desc limit 0,20
數據總量90w
查詢耗時要5.5156秒
多,無法接收
使用EXPLAIN
查詢,發現l並沒有使用dlog_id
作為索引
最終使用FORCE INDEX (PRIMARY)
強制索引解決該問題
修改后的語句為
select * from dtu_log as l FORCE INDEX (PRIMARY) left join e as e on l.dlog_ele=e.id where 1=1 and e.maint_com=1 order by dlog_id desc limit 0,20
同樣的數據,修改后查詢耗時0.0016秒
有了很好的改善,特此記錄一次