以下查询需要永久完成。慢速查询和复合索引
有每个表示WHERE
& JOIN
陈述了田野的指标,但我没有在两个ship_to_id
和bill_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
任何原因,你不能只是'WHERE orders.order_date <'2012-08-10''? mysql可以按照原样接受该日期,而不必将其强制为本地日期。 –
复合索引不会起作用,因为它仍需要在索引的第二部分匹配,而第一部分没有匹配(所以索引无用)。 MySQL只会在查询中的表的一个实例上使用一个索引,因此2列上的单独索引不起作用。解决方案(直到MySQL索引变得更聪明)是Joe Frambach(将查询拆分为2,其中每个索引都可以使用索引)的建议。但是,这可能不是唯一的瓶颈,所以如果您发布了对您的查询的解释,那将是最好的。 – Kickstart