我在MySQL DB上运行了一个查询,而且速度非常慢。
我是否可以对以下内容进行优化
SELECT mcm.merchant_name,
( ( Sum(ot.price) + Sum(ot.handling_charges)
+ Sum(ot.sales_tax_recd)
+ Sum(ot.shipping_cost) - Sum(ot.sales_tax_payable) ) -
Sum(im.break_even_cost) ) AS PL,
ot.merchant_id
FROM order_table ot,
item_master im,
merchant_master mcm
WHERE ot.item_id = im.item_id
AND ot.merchant_id = mcm.merchant_id
GROUP BY mcm.merchant_name
ORDER BY pl DESC
LIMIT 0, 10; 上面的查询需要超过200秒才能执行。
解释结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE ot ALL "merchant_id,item_id" NULL NULL NULL 507910 "Using temporary; Using filesort"
1 SIMPLE mcm eq_ref "PRIMARY,merchant_id" PRIMARY 4 stores.ot.merchant_id 1
1 SIMPLE im eq_ref "PRIMARY,item_id" PRIMARY 4 stores.ot.item_id 1 此外,当我运行EXPLAIN扩展时,我得到了错误-1003。
发布于 2013-12-06 06:27:41
使用mysql解释计划找出为什么要花这么长时间,然后可能创建一些索引或更改代码。
基于此更新,确保在merchant_id,item_id上的order_table上有一个复合索引
https://stackoverflow.com/questions/20417425
复制相似问题