0

I was wondering is there a performance difference between:

query 1: CREATE TEMPORARY TABLE temp_table1 AS SELECT * FROM lookup_table JOIN ...

then

INSERT INTO dest_table SELECT * FROM temp_table1

vs

query 2: INSERT INTO dest_table SELECT * FROM lookup_table JOIN ... 

My concern was, the lookup_table is accessed very often by different users and when I run query 2, most of the users need to wait longer to be able to retrieve their result. What I was thinking was to write the data into a temporary table then write it to dest_table afterwards . Im just not sure if writing into a temp table with give a difference performance compared to writing it directly to the destination table. Im using mysql 5.6.

The reason why I need to write data from lookup_table to dest_table is because I need to create a report from it. Seeing how complex the query from lookup_table is makes it very difficult to create a report so I decided to move those data to a single table then just make a report from it.

4

1 回答 1

0

您担心填充此临时表的 SELECT 查询所占用的锁定时间。

这些表的实现方式相同,因此在任何一种情况下,创建成本都将非常接近。

您可以通过在 MEMORY 访问方法中创建临时表来加快速度,但我怀疑差异会很小;这里涉及的工作是 SELECT / JOIN 的东西。

通过在创建目标表时确保目标表没有索引,您可能会使其运行得更快。 CREATE ... AS SELECT会这样做。

您将能够通过摆脱SELECT *(无论如何都会在 JOIN 上产生冗余列)来降低创建成本,而是指定您真正需要的列。

但是,最好的办法是弄清楚为什么要创建此表,并查看是否可以通过编写针对源表的查询来满足这些要求。如果您使这些查询操作高效,您就可以为自己节省大量数据混洗。

于 2014-04-15T00:54:27.663 回答