mse*_*dio 16 sql database sql-server sql-server-2008
使用显式create table语句和加载数据与选择之间是否存在性能差异.此示例仅显示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
Run Code Online (Sandbox Code Playgroud)
或常规表:
--- Explicitly creating table first and then loading.
create table test1 (id int, name varchar(100))
insert into test1 (id, name) select id, name from #bigTable
--- Creating table by selecting into.
select id,name into test2 from bigTable
Run Code Online (Sandbox Code Playgroud)
每个人对此有何看法?我认为显式创建表和加载必须具有比选择select更好的性能必须在语句中评估表达式以创建表.
我们的组织通常明确地创建临时表作为标准实践,我们想知道一切都认为实际上是最佳实践.
小智 6
CREATE TABLE在插入数据之前,您可以更好地控制表的定义,例如NOT NULL,约束等您无法使用的事物SELECT INTO.
SELECT INTO是一种最低限度记录的操作,但INSERT..SELECT在某些情况下也可以进行最低限度的记录.
请参阅"数据加载性能指南",尤其是" 汇总最小日志记录条件"部分.
简而言之,如果你不关心约束等(例如你想快速创建一个表的副本),SELECT..INTO恕我直言的优点是更短的代码.
否则,您应该使用另一种方式,并且您仍然可以将其记录最少.