我有6张桌子
我希望通过过滤订单日期和订单状态来获取订单数据, 所以,我有这样的查询:
SELECT orders_id, item_id, qty, status, order_date FROM ( (SELECT bo.orders_id AS orders_id, boi.orders_items_id AS item_id, boi.order_quantity AS qty, boish.status AS status, bo.import_date AS order_date FROM b_orders bo LEFT JOIN b_orders_items boi ON boi.orders_id = bo.orders_id LEFT JOIN b_orders_items_history boih ON boih.orders_items_id = boi.orders_items_id WHERE bo.partner_id IN (2,10) AND DATE(boish.status_date) >= DATE('2015-10-27') AND DATE(boish.status_date) <= DATE('2016-04-26') AND boish.status = 10 GROUP BY boish.status_date, boish.status) UNION ALL (SELECT ao.orders_id AS orders_id, aoi.orders_products_id AS item_id, aoi.products_quantity AS qty, opsh.status AS status, ao.date_purchased AS order_date FROM a_orders ao LEFT JOIN a_orders_items aoi ON aoi.orders_id = ao.orders_id LEFT JOIN a_orders_items_history aoih ON aoih.orders_products_id = aoi.orders_products_id WHERE DATE(aoih.status_date) >= DATE('2015-10-27') AND DATE(aoih.status_date) <= DATE('2016-04-26') AND aoih.status = 10 GROUP BY aoih.status_date, aoih.status) ) temp_table GROUP BY item_id, status;
完成需要5秒钟,并且还记录了mysql慢查询。 有什么办法可以让它更快吗? 我已经尝试过使用UNION,并将它放在WHERE过滤器的子查询之外,但结果是一样的。
编辑: 我将在此处显示EXPLAIN结果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra
1|PRIMARY|<derived2>|ALL|null|null|null|null|3385|Using temporary; Using filesort
2|DERIVED|bo|ALL|PRIMARY,customer_billing_id,customer_shipping_id|null|null|null|743032|Using where; Using temporary; Using filesort
2|DERIVED|boi|ref|PRIMARY,Index_Orders|Index_Orders|4|bo.bo_orders_id|1|Using where
2|DERIVED|boih|ref|orders_items_id|orders_items_id|5|boi.orders_items_id|1|Using where
3|UNION|aoi|ALL|PRIMARY,orders_id|null|null|null|77765|Using temporary; Using filesort
3|UNION|ao|eq_ref|PRIMARY|PRIMARY|4|aoi.orders_id|1|empty
3|UNION|aoih|ref|orders_products_id|orders_products_id|5|aoi.orders_products_id|1|Using where
null|UNION RESULT|<union2,3>|ALL|null|null|null|null|null|empty
谢谢,任何帮助表示赞赏。
答案 0 :(得分:0)
您可以尝试解释您的查询,它将为您提供有关查询的索引使用情况的信息......您可以在MySQL文档中使用它。