order表字段:order_id,product_id查询购买产品A的订单同时会购买哪些产品SELECTprodcut_id,COUNT(*)ASnumsFROMorderLEFTJOIN(SELECTorder_idFROMorderWHEREproduct_id=100)ASorder_bONorder_b.order_id=order.order_idWHEREorder_b.order_idISNOTNULLGROUPBYproduct_idORDERBYnumsDESCoder_id,prodcut_id已做索引请问如何优化以上查询语句?
2 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
用JOIN替换掉LEFTJOIN:SELECTprodcut_id,COUNT(*)ASnumsFROMorderJOIN(SELECTorder_idFROMorderWHEREproduct_id=100)ASorder_bONorder_b.order_id=order.order_idGROUPBYproduct_idORDERBYnumsDESC
月关宝盒
TA贡献1772条经验 获得超5个赞
oder_id,prodcut_id已做索引索引是两索引分别在order_id,prodcut_id上?还是一个索引在(order_id,prodcut_id)?Query1:selectproduct_id,count(product_id)fromorderWHEREorder_idin(Selectorder_idFROMorderWHEREproduct_id=100)groupbyproduct_id;Query2:selecta.product_id,count(a.product_id)fromorderainnerjoinorderbona.order_id=b.order_idandb.product_id=100groupbyproduct_id;Query2isbetter.
添加回答
举报
0/150
提交
取消