優化-mysql子查詢索引失效問題解決
問題
生產某些功能執行較慢,找出來對應的sql,看看執行計劃,入下:
sql:
SELECT
COUNT( 1 )
FROM
fich_base t1
LEFT JOIN cont_base t2 ON t1.contract_id = t2.id
LEFT JOIN orga_base t3 ON t3.id = t1.transfer_organization_id
LEFT JOIN user_base t4 ON t4.id = t2.sale_user_id
LEFT JOIN empl_base t5 ON t5.id = t4.employee_id
LEFT JOIN vehi_brand t6 ON t6.id = t2.vehicle_brand_id
LEFT JOIN vehi_base t7 ON t2.vehicle_id = t7.id
LEFT JOIN deal_base t8 ON t8.id = t2.dealer_id
LEFT JOIN fich_review_node t9 ON t1.id = t9.final_check_id
LEFT JOIN fich_review_process t10 ON t9.id = t10.review_node_id
WHERE
t1.obj_status = 1
AND t2.factory_end_date IS NOT NULL
AND t2.obj_status = 1
AND t2.number LIKE CONCAT( '%', CONCAT( 123, '%' ) )
AND (
t2.personal_name LIKE CONCAT( '%', CONCAT( 1234, '%' ) )
OR t2.enterprise_name LIKE CONCAT( '%', CONCAT( 1234, '%' ) )
)
AND t2.vin_number LIKE CONCAT( '%', CONCAT( 123, '%' ) )
AND t1.transfer_organization_id IN ( SELECT u.organization_id FROM user_rel_organization_view u WHERE u.user_id = 1 AND u.obj_status = 1 )
AND t1.review_status = 2
AND t10.review_status = 0
AND t9.review_count = 1
AND t9.review_status = 0
AND timestampdiff( DAY, t2.factory_end_date, NOW( ) ) <= 30
AND t2.`status` != 3
執行計劃:
有說in 接子查詢會導致索引失效。
解決
調整子查詢為關聯查詢:
EXPLAIN
SELECT
COUNT( 1 )
FROM
fich_base t1
LEFT JOIN cont_base t2 ON t1.contract_id = t2.id
LEFT JOIN orga_base t3 ON t3.id = t1.transfer_organization_id
LEFT JOIN user_base t4 ON t4.id = t2.sale_user_id
LEFT JOIN empl_base t5 ON t5.id = t4.employee_id
LEFT JOIN vehi_brand t6 ON t6.id = t2.vehicle_brand_id
LEFT JOIN vehi_base t7 ON t2.vehicle_id = t7.id
LEFT JOIN deal_base t8 ON t8.id = t2.dealer_id
LEFT JOIN fich_review_node t9 ON t1.id = t9.final_check_id
LEFT JOIN fich_review_process t10 ON t9.id = t10.review_node_id
# 這裡調整了
left join user_rel_organization_view t11 on t1.transfer_organization_id=t11.organization_id
WHERE
t1.obj_status = 1
AND t2.factory_end_date IS NOT NULL
AND t2.obj_status = 1
AND t2.number LIKE CONCAT( '%', CONCAT( 123, '%' ) )
AND (
t2.personal_name LIKE CONCAT( '%', CONCAT( 1234, '%' ) )
OR t2.enterprise_name LIKE CONCAT( '%', CONCAT( 1234, '%' ) )
)
AND t2.vin_number LIKE CONCAT( '%', CONCAT( 123, '%' ) )
# 這裡調整了
AND t11.user_id = 1 AND t11.obj_status = 1
AND t1.review_status = 2
AND t10.review_status = 0
AND t9.review_count = 1
AND t9.review_status = 0
AND timestampdiff( DAY, t2.factory_end_date, NOW( ) ) <= 30
AND t2.`status` != 3
執行計劃都走索引了
總結
對於sql,在編寫完成後,要關注執行計劃檢查,另外對於in,exist,要關注資料量的變化對索引的影響。
相關文章
- MySQL索引與查詢優化MySql索引優化
- MySQL-效能優化-索引和查詢優化MySql優化索引
- MySQL 優化五(關聯查詢子查詢以及 in 的效率問題)(高階篇)MySql優化
- MySQL子查詢的優化薦MySql優化
- MySQL 索引及查詢優化總結MySql索引優化
- MySQL索引原理及慢查詢優化MySql索引優化
- mysql效能優化-慢查詢分析、優化索引和配置MySql優化索引
- 《MySQL慢查詢優化》之SQL語句及索引優化MySql優化索引
- 【最佳化】模糊查詢索引問題索引
- MySQL查詢優化MySql優化
- mysql子查詢的缺陷以及5.6的優化MySql優化
- 聊一聊MySQL索引失效的問題MySql索引
- 查詢效率提升10倍!3種優化方案,幫你解決MySQL深分頁問題優化MySql
- Mysql優化系列之——優化器對子查詢的處理MySql優化
- exists與in子查詢優化優化
- Mysql中的巢狀子查詢問題QSBSMySql巢狀
- mysql千萬級資料量根據索引優化查詢速度MySql索引優化
- MySQL 的查詢優化MySql優化
- MySQL 慢查詢優化MySql優化
- MySQL優化COUNT()查詢MySql優化
- 三高Mysql - Mysql索引和查詢優化(偏理論部分)MySql索引優化
- 三高Mysql - Mysql索引和查詢優化(偏實戰部分)MySql索引優化
- mysql innodb 索引失效問題引起表級鎖MySql索引
- MySQL第六篇:索引與子查詢MySql索引
- MySql中limit子查詢的解決辦法MySqlMIT
- mysql查詢優化檢查 explainMySql優化AI
- MySQL調優之查詢優化MySql優化
- MySQL進階【五】—— MySQL查詢優化器是如何選擇索引的MySql優化索引
- MySQL索引的最左字首原理與查詢的相關優化MySql索引優化
- 【oracle 效能優化】組合索引查詢。Oracle優化索引
- MongoDB範圍查詢的索引優化MongoDB索引優化
- PostgreSQL 原始碼解讀(29)- 查詢語句#14(查詢優化-上拉子查詢)SQL原始碼優化
- 提高mysql千萬級大資料SQL查詢優化30條經驗(Mysql索引優化注意)MySql大資料優化索引
- plsql查詢亂碼問題解決SQL
- MySQL查詢優化利刃-EXPLAINMySql優化AI
- MySQL分頁查詢優化MySql優化
- mysql關聯查詢優化MySql優化
- .gitignore 失效問題解決Git