【发布时间】:2011-07-26 15:04:20
【问题描述】:
使用显式创建表语句和加载数据与选择进入之间是否存在性能差异。此示例仅显示 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) select id, name from #bigTable
--- Creating table by selecting into.
select id,name into test2 from bigTable
大家对此有何看法?我认为显式创建表和加载必须具有比 select into 更好的性能,因为 select into 必须评估语句中的表达式才能创建表。
我们的组织通常明确地创建临时表作为标准做法,我们想知道什么才是真正的最佳做法。
【问题讨论】:
-
我没有时间检查,但您可能会发现这是重新编译的原因。这种潜在的开销是否大于 Adam Houldsworth 所暗示的其他好处,我会留给你或其他人去了解:)
-
你检查过这两个变种的执行计划了吗?你真的给他们计时,看看是否有可测量的差异吗?
-
我之前都试过了,但不是在大量数据上。目前我并不想解决任何性能问题,我很好奇每种插入方法的优缺点...
标签: sql sql-server database sql-server-2008