慢查询和复合索引

时间:2013-06-24 16:21:32

标签: mysql

以下查询需要永远完成。

每个字段都有索引显示在WHERE& JOIN语句,但我对第一个ship_to_id的{​​{1}}和bill_to id都没有综合索引。

当第一次加入仅在单个字段上时,查询会按预期完成。

JOIN声明是瓶颈。

在这种情况下,复合索引是否有意义,或者我是否必须更改查询的逻辑?感谢。

以下是此查询的IN(与格式相符):

EXPLAIN

1 个答案:

答案 0 :(得分: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