2013-06-24 102 views
0

以下查詢需要永久完成。慢速查詢和複合索引

有每個表示WHERE & JOIN陳述了田野的指標,但我沒有在兩個ship_to_idbill_to id綜合指數爲第一JOIN

當第一次連接僅在單個字段上時,查詢會按預期立即完成。

IN聲明是瓶頸。

在這種情況下,組合索引是否合理?還是必須更改查詢的邏輯?謝謝。

這裏的EXPLAIN此查詢(與格式掙扎):

1 SIMPLE orders   ALL or4          1973557 Using where; Using temporary; Using filesort 
1 SIMPLE taxonomy eq_ref PRIMARY    PRIMARY 62 datamart.orders.Item 1 Using where 
1 SIMPLE universe index mtot1    mtot1 4   856128 Using where; Using index; Using join buffer (Block Nested Loop) 
1 SIMPLE customer eq_ref PRIMARY,cu4   PRIMARY 4 datamart.universe.customer_id 1 


CREATE TABLE ff_atl AS 
SELECT 

universe.customer_id  as ID, 

orders.order_date   as orddt, 
orders.order_sequence  as ordnum, 
taxonomy.age    as prodage, 
taxonomy.category   as prodcat, 
taxonomy.source    as prodsrc, 
orders.order_category  as channel, 
orders.quantity    as quantity, 
orders.price_after_discount as pad, 
orders.number_of_issues_left as nIssuesLeft, 
orders.number_of_times_renewed as nTimesRenewed, 
orders.number_of_invoice_effort as nInvoiceEfforts, 
case when orders.order_status in (1,2,3,4) then 1 else 0 end as cancelled, 
customer.zip    as zipcode, 
customer.create_date  as fordt, 
orders.item     as item, 
orders.subscription_id  as subid 
FROM 
paid_cat_ATL universe INNER JOIN orders_raw orders  ON universe.customer_ID IN (orders.BILL_to_id,orders.SHIP_to_id) 
         INNER JOIN customers_raw customer ON customer.customer_id = universe.customer_ID 
         LEFT JOIN products taxonomy  ON taxonomy.order_class = orders.item 
WHERE orders.order_date <= STR_TO_DATE('2012-08-10' , '%Y-%m-%d') 

ORDER BY universe.customer_id , orders.order_sequence 
+0

任何原因,你不能只是'WHERE orders.order_date <'2012-08-10''? mysql可以按照原樣接受該日期,而不必將其強制爲本地日期。 –

+1

複合索引不會起作用,因爲它仍需要在索引的第二部分匹配,而第一部分沒有匹配(所以索引無用)。 MySQL只會在查詢中的表的一個實例上使用一個索引,因此2列上的單獨索引不起作用。解決方案(直到MySQL索引變得更聰明)是Joe Frambach(將查詢拆分爲2,其中每個索引都可以使用索引)的建議。但是,這可能不是唯一的瓶頸,所以如果您發佈了對您的查詢的解釋,那將是最好的。 – Kickstart

回答

2

你可以做兩個查詢和UNION他們。

CREATE TABLE ff_atl AS 
SELECT 

universe.customer_id  as ID, 

orders.order_date   as orddt, 
orders.order_sequence  as ordnum, 
taxonomy.age    as prodage, 
taxonomy.category   as prodcat, 
taxonomy.source    as prodsrc, 
orders.order_category  as channel, 
orders.quantity    as quantity, 
orders.price_after_discount as pad, 
orders.number_of_issues_left as nIssuesLeft, 
orders.number_of_times_renewed as nTimesRenewed, 
orders.number_of_invoice_effort as nInvoiceEfforts, 
case when orders.order_status in (1,2,3,4) then 1 else 0 end as cancelled, 
customer.zip    as zipcode, 
customer.create_date  as fordt, 
orders.item     as item, 
orders.subscription_id  as subid 
FROM 
paid_cat_ATL universe INNER JOIN orders_raw orders  ON universe.customer_ID = orders.BILL_to_id 
         INNER JOIN customers_raw customer ON customer.customer_id = universe.customer_ID 
         LEFT JOIN products taxonomy  ON taxonomy.order_class = orders.item 
WHERE orders.order_date <= STR_TO_DATE('2012-08-10' , '%Y-%m-%d') 

UNION 

SELECT 

universe.customer_id  as ID, 

orders.order_date   as orddt, 
orders.order_sequence  as ordnum, 
taxonomy.age    as prodage, 
taxonomy.category   as prodcat, 
taxonomy.source    as prodsrc, 
orders.order_category  as channel, 
orders.quantity    as quantity, 
orders.price_after_discount as pad, 
orders.number_of_issues_left as nIssuesLeft, 
orders.number_of_times_renewed as nTimesRenewed, 
orders.number_of_invoice_effort as nInvoiceEfforts, 
case when orders.order_status in (1,2,3,4) then 1 else 0 end as cancelled, 
customer.zip    as zipcode, 
customer.create_date  as fordt, 
orders.item     as item, 
orders.subscription_id  as subid 
FROM 
paid_cat_ATL universe INNER JOIN orders_raw orders  ON universe.customer_ID = orders.SHIP_to_id 
         INNER JOIN customers_raw customer ON customer.customer_id = universe.customer_ID 
         LEFT JOIN products taxonomy  ON taxonomy.order_class = orders.item 
WHERE orders.order_date <= STR_TO_DATE('2012-08-10' , '%Y-%m-%d') 

ORDER BY universe.customer_id , orders.order_sequence 
+0

不會UNION重複bill_to_id和ship_to_id相同的交易記錄嗎?擁有SORT的聯盟也會影響我認爲的查詢速度。 – user2105469

+0

你是對的,會的。 '然後,將some_column = ID和other_column <> ID'加入other_table。 –

+0

這會排除這些記錄(ship_to = bill_to)。 – user2105469