如何使用MySQL将内连接保存到新表

时间:2018-04-01 13:23:07

标签: mysql sql inner-join

我试图将内部联接保存到新表中但是它无休止地运行并最终超时。内连接本身在大约15秒后工作,无需创建表格。

示例:

create table newtable as(
SELECT thing1, thing2
FROM (container
INNER JOIN staff ON container.Staff = staff.name)
);

无需创建表格即可运行的示例:

SELECT thing1, thing2
    FROM (container
    INNER JOIN staff ON container.Staff = staff.name);

2 个答案:

答案 0 :(得分:0)

你的意思是你已经尝试过这样做但它没有用?

create table newtable as  
SELECT thing1, thing2 
FROM  container 
INNER JOIN staff  
ON container.Staff = staff.name

桌子有多大?当您从客户端程序运行查询时,通常只会提取前几行,因此您不需要等待下载所有内容,但是当您执行create table时,必须完整地创建它,如果你有数百万行...

答案 1 :(得分:0)

执行join时,它会在所有结果生成之前开始返回结果。

当您执行create table时,您必须等到 all 生成结果。

你可能有很多结果。您可以通过以下方式估算出的数量:

select count(*), count(distinct staff)
from container;

select count(*), count(distinct name)
from staff;

然后:

select sum(c.cnt * s.cnt)
from (select staff, count(*) as cnt
      from container
      group by staff
     ) c join
     (select name, count(*) as cnt
      from staff
      group by name
     ) s
     on c.staff = s.name;

这是需要生成的行数。