mysql解释结果解释
下面的查询与我期望的完全一样,直观且不生成中间表。缺点是需要很长时间才能完成 在这种情况下,我要做的是分步分解查询并创建那些中间表和索引。这一次,我想更好地处理explain提供的提示,并希望得到任何提示:我在下面的查询中遗漏了哪些明显的优化步骤 按照中的建议,我在mysql解释结果解释,mysql,Mysql,下面的查询与我期望的完全一样,直观且不生成中间表。缺点是需要很长时间才能完成 在这种情况下,我要做的是分步分解查询并创建那些中间表和索引。这一次,我想更好地处理explain提供的提示,并希望得到任何提示:我在下面的查询中遗漏了哪些明显的优化步骤 按照中的建议,我在订单原始中创建了关于订单编号、订单类型和项目的索引。然而,尚不清楚这些将如何进行字符处理/正则表达式 SELECT bundle_headers.order_number , bundle_headers.title , digita
订单原始
中创建了关于订单编号
、订单类型
和项目
的索引。然而,尚不清楚这些将如何进行字符处理/正则表达式
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;
编辑,@tin tran:我还没有严格地对上面的查询和您的查询(经过几次更正后,复制到下面)在空闲机器上开始计时。我确实提交了它,并且没有看到运行时间的明显减少
SELECT bundle_headers.order_number,
substring(bundle_headers.item,1,3) as title,
subs.subscription_id,
1 as bundle_component
FROM orders_raw 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.item LIKE CONCAT('%',substring(bundle_headers.item,1,3),'%')
AND (length(subs.item) = 4 OR length(subs.item) = 3)
请尝试此查询,看看是否产生相同的结果。如果再快一点呢
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)
虽然您的问题是关于
解释,但我建议您创建临时表并使用它们而不是子查询。。。我用过这种方法,它加快了速度。当然,您需要为临时表创建适当的索引:drop table if exists temp_myTable;创建临时表temp_myTable select;alter table temp_myTable添加索引idx_anIndex(外地)代码>@Barranka-临时表而不是子查询?更快??我第一次听到这个…@Tomas信不信由你。。。我已经多次面临这种任务,在大多数情况下,创建中间临时表比子查询更快。当然,必须小心地创建它们,并且需要对它们进行适当的索引。很普通,但它完成了任务。