mysql解释结果解释

时间:2014-01-04 00:01:32

标签: mysql

下面的查询完全符合我的预期,是直观的,不会生成中间表。缺点是需要很长时间才能完成。

在这种情况下我要做的是逐步打破查询并创建那些中间表&索引。这一次,我想更好地处理解释提供的提示,并希望得到任何指示:我在下面的查询中遗漏了哪些明显的优化步骤?

根据MySQL query optimization and EXPLAIN for a noob中的建议,我在order_number的{​​{1}},order_typeitem创建了索引。然而,目前还不清楚这些将如何带来字符处理/正则表达式。

Output of EXPLAIN

orders_raw
编辑,@ ing tran:我还没有严格计算上面的查询和你的查询(经过几次修改,下面复制后),从空闲机器开始。我确实提交了它,并没有看到运行时间明显减少。

SELECT bundle_headers.order_number , bundle_headers.title , digital_subs.subscription_id , 1 as bundle_component
from
(
  select order_number , substring( item , 1 , 3 ) as title , quantity from orders_raw
  where order_type in (4,6)                      
) bundle_headers
inner join
(
  select order_number , subscription_id , item as title , quantity from orders_raw
  where order_type = 0 and length( item ) = 4    
) digital_subs
on bundle_headers.order_number = digital_subs.order_number and
   digital_subs.title regexp concat( '.*' , bundle_headers.title , '.*' ) and
   bundle_headers.quantity = digital_subs.quantity

UNION


SELECT bundle_headers.order_number , bundle_headers.title , print_subs.subscription_id , 1 as bundle_component
from
(
  select order_number , substring( item , 1 , 3 ) as title , quantity from orders_raw
  where order_type in (4,6)                      
) bundle_headers
inner join
(
  select order_number , subscription_id , item as title , quantity from orders_raw
  where order_type = 0 and length( item ) = 3    
) print_subs
on bundle_headers.order_number = print_subs.order_number and
   print_subs.title regexp concat( '.*' , bundle_headers.title , '.*' ) and
   bundle_headers.quantity = print_subs.quantity;

1 个答案:

答案 0 :(得分:1)

请尝试此查询,看看它是否产生相同的结果。如果它更快

SELECT bundle_headers.order_number,substring(bundle_headers.title,1,3) as title,subs.subscription_id,1 as bundle_component
FROM order_type bundle_headers
INNER JOIN orders_raw subs ON (bundle_headers.order_number = subs.order_number)
WHERE (bundle_headers.order_type = 4 OR bundle_headers.order_type = 6)
  AND subs.order_type = 0
  AND bundle_headers.quantity = subs.quantity
  AND subs.title LIKE CONCAT('%',substring(bundle_headers.title,1,3),'%')
  AND (length(subs.item) = 4 OR length(subs.item) = 3)