sql – 使用显式create table语句和select into创建一个表

使用显式创建表语句和加载数据与选择之间是否存在任何性能差异.此示例仅显示2列,但问题是面向使用非常大的表.下面的示例也使用临时表,虽然我想知道使用常规表的效果.我认为他们将是一样的,不管表类型.

临时表情景:

--- Explicitly creating temp table first and then loading.
create table #test1 (id int,name varchar(100))
insert into #test1 (id,name) select id,name from #bigTable

--- Creating temp table by selecting into.
select id,name into #test2 from #bigTable

或普通表:

--- Explicitly creating table first and then loading.
create table test1 (id int,name varchar(100))
insert into test1 (id,name from #bigTable

--- Creating table by selecting into.
select id,name into test2 from bigTable

大家对此有何想法?我认为明确创建表和加载必须具有比选择为select的更好的性能,以便在语句中评估表达式才能创建一个表.

我们的组织通常将临时表格明确地作为标准做法,我们想知道一切都认为是最佳实践.

http://msdn.microsoft.com/en-us/library/ms188029.aspx

解决方法

在插入数据之前,CREATE TABLE可以更好地控制表的定义,例如NOT NULL,约束等,您不能使用SELECT INTO执行的操作.

SELECT INTO是一种最低限度的记录操作,但在某些情况下,INSERT..SELECT也可以进行最低限度的记录.
请参见The Data Loading Performance Guide,特别是部分:总结最小记录条件.

简而言之,如果你不在乎约束等(例如你想快速创建表的副本),SELECT..INTO的优势IMHO是一个较短的代码.否则,您应该使用另一种方式,您仍然可以将其最小化记录.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...