mysql聯表查詢優化技巧


執行以下sql:

explain select gc.*, g.title from  t_goods g , t_goods_cover gc where g.goods_id = gc.goods_id and g.category_id = 44 ;

 

explain 默認第一行出現的表就是驅動表,由查詢優化器自動選擇,影響的rows是9462行,查詢效率很低

 

 

 

#關聯查詢優化要點
#1. 外鍵上加索引
create index idx_goods_id on t_goods_cover(goods_id);
#2. 查詢條件上加索引
create index idx_category_id on t_goods(category_id);

優化后同樣的sql查詢結果,驅動表的影響行數rows下降到74,查詢效率大大提高了

 


免責聲明!

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



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