[英]Any way to make UNION ALL run faster?
I have a lot of exactly same tables. 我有很多完全相同的表。 TableA,TableB,TableC,TableD etc. which I want to create views from.
我想从中创建视图的TableA,TableB,TableC,TableD等。 Doing
select * from TableA
takes 20ms, doing select * from tableB
takes 20ms, but doing (select * from TableA) union all (select * from TableB)
takes over 20 minutes. select * from TableA
执行select * from TableA
花费20毫秒, select * from tableB
执行select * from tableB
花费20毫秒,但是(select * from TableA) union all (select * from TableB)
需要20分钟以上。 Those tables have exactly same columns. 这些表具有完全相同的列。 Is there any settings in my.cnf that I need to change, or a way to create a view that would run faster?
my.cnf中是否需要更改任何设置,或者是否可以创建运行速度更快的视图? All tables have 1.5m to about 10m rows.
所有表都有1.5m至约10m行。
Results of explain 解释结果
PRIMARY TableA ALL 28808685
UNION TableB ALL 15316215
UNION RESULT <union1,2> ALL Using temporary
Table structure: 10 varchar(20)'s, 5 unsigned INTs. 表结构:10个varchar(20),5个无符号INT。
My guess is that select * from TableA
does not take 20 ms. 我的猜测是,
select * from TableA
中select * from TableA
不需要20毫秒。 It takes 20 ms to start returning results. 开始返回结果需要20毫秒。
Although I am going to answer your question, you should revisit your data structure. 尽管我将回答您的问题,但是您应该重新访问数据结构。 Having multiple tables with the same layout is usually a really bad idea.
具有相同布局的多个表通常是一个糟糕的主意。 Instead, you should have a single table with all the rows.
相反,您应该有一个包含所有行的表。
But, you don't seem to have that. 但是,您似乎没有。
Try running the union all
without parentheses: 尝试运行
union all
不带括号的union all
:
select * from TableA union all
select * from TableB;
MySQL has a habit of materializing subqueries. MySQL有实现子查询的习惯。 I'm not sure if it does this with
union all
subqueries, but given your description of the problem, that sees likely. 我不确定是否通过合并
union all
子查询来执行此操作,但是鉴于您对问题的描述,这似乎是可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.