我試圖優化在電子商務網站上運行的報表查詢。我敢肯定,我正在做一些愚蠢的事情,因爲這個查詢不應該花費盡可能長的時間來運行。 有問題的查詢:MYSQL查詢優化
SELECT inventories_name, inventories_code, SUM(shop_orders_inventories_qty) AS qty,
SUM(shop_orders_inventories_price) AS tot_price, inventories_categories_name,
inventories_price_list, inventories_id
FROM shop_orders
LEFT JOIN shop_orders_inventories ON (shop_orders_id = join_shop_orders_id)
LEFT JOIN inventories ON (join_inventories_id = inventories_id)
WHERE {$date_type} BETWEEN '{$start_date}' AND '{$end_date}'
AND shop_orders_x_response_code = 1
GROUP BY join_inventories_id, join_shop_categories_id
{$order}
{$limit}
它基本上是試圖在一段時間來獲得每個項目的總銷售額;大括號中的值通過表單填寫。它可以在幾天內正常工作,但是查詢一週或更長的時間間隔可能需要30秒+。
我覺得爲了計算聚合值並吸取大量內存,我覺得它加入了太多的行,但我不知道如何限制它。
注 - 我意識到我選擇的字段不在羣組中,但它們與羣組中的庫存ID相對應。
有什麼建議嗎?
- 編輯 - 目前的指標是:
庫存:
join_categories - BTREE
inventories_name,inventories_code,inventories_description - FULLTEXT
shop_orders_inventories:
shop_orders_inventories_id - BTREE
shop_orders:
shop_orders_id - BTREE
這些表有哪些索引? – 2011-03-22 23:00:57