我正在开发一个数据仓库。我们每晚刷新的一个临时表有大约 1000 万行。我们正在使用自定义构建的 ETL 工具,我无法对其进行太多更改。该工具像这样加载这个临时表:
truncate stage_table;
insert into stage_table with (tablockx) (column1, column2, etc...)
exec load_stage_table @batch_id = @batch_input
Run Code Online (Sandbox Code Playgroud)
的内容load_stage_table
有一些设置和选择语句。我无法分享确切的代码,但这是一个基本示例。
create table load_stage_table
(
@batch_id varchar(max) = null
)
as
-- <update batch_id in batch_table>
-- collect data
select
column1 = table1.column1,
column2 = table2.column2,
...
from table1
join table2
on table2.id = table1.table2_id
-- many more similar joins
Run Code Online (Sandbox Code Playgroud)
问题是,当我按照 ETL 工具运行的方式运行存储过程时,运行时间几乎是 30 分钟。但是,如果我修改存储过程以在内部包含插入语句,则只需 1 分钟。
create table load_stage_table
(
@batch_id varchar(max) = null
)
as
-- …
Run Code Online (Sandbox Code Playgroud) performance sql-server insert sql-server-2016 exec query-performance