简体   繁体   中英

Insert into table variable is slow

I have a dynamic sql in stored procedure like

@sql = 'SELECT a, 
       b, 
       c d 
FROM   table t 
       LEFT JOIN table2 t2 
              ON t.id = t2.id '

and then inserting into a table variable

Insert into @tblCustomer

EXECUTE sp_executesql @sql

The insert statement is taking long time to execute. Is there any way i can improve it?

I am using SQL Server 2008

Thanks

though you cannot do much(simple sql query).

try using temp table instead

CREATE TABLE #TempTable(
 ID int,
 col2 <datatype>,
 col3 <datatype>)

and insert into it

see if it works faster. read more about it here

try indexing your database. it should give you visible performance boost.

Try the following query :

@sql = SELECT a, b, c, d 
FROM table t (NOLOCK) LEFT JOIN table2 t2 (NOLOCK) ON t.id = t2.id

It seems to mee that it is impossible to optimize. The query is straight forward. Temp tables if used may slow the process even more due to memory use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM