Sample Header Ad - 728x90

Insert Into table Exec SP with bad performance

5 votes
2 answers
2479 views
I am working on a datawarehouse. One of our staging tables that is refreshed every night has about 10 million rows. We are using a custom built ETL tool that I can't make too many changes to. The tool loads this staging table like this: truncate stage_table; insert into stage_table with (tablockx) (column1, column2, etc...) exec load_stage_table @batch_id = @batch_input The contents of load_stage_table has some setup and a select statement. I can't share the exact code, but here is a basic example. create table load_stage_table ( @batch_id varchar(max) = null ) as -- -- collect data select column1 = table1.column1, column2 = table2.column2, ... from table1 join table2 on table2.id = table1.table2_id -- many more similar joins The problem is that when I run the stored procedure as its meant to be run with our ETL tool, the run time is almost 30 minutes. However if I modify the stored procedure to have the insert statement on the inside, then it only takes 1 minute. create table load_stage_table ( @batch_id varchar(max) = null ) as -- -- collect data insert into stage_table with (tablockx) (column1, column2, etc...) select column1 = table1.column1, column2 = table2.column2, ... from table1 join table2 on table2.id = table1.table2_id -- many more similar joins After running this a few times both ways and examining the execution plans, it seems that parallelism is not used when the insert is outside the stored procedure. Does loading the table from the return, outside of the stored procedure, prevent parallelism? Or is this an indicator that the select statement needs some query tuning?
Asked by Ryati (153 rep)
Jan 26, 2018, 10:39 PM
Last activity: Aug 30, 2021, 11:00 PM