2013-06-24 67 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