0

以下查询需要永远完成。

WHERE&语句中显示的每个字段都有索引,但我在第一个和JOIN两个上都没有复合索引。ship_to_idbill_to idJOIN

当第一个连接仅在单个字段上时,查询会按预期立即完成。

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
4

1 回答 1

2

您可以执行两个查询并将它们联合起来。

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
于 2013-06-24T16:24:53.487 回答