0

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

Kermit
  • 33,827
  • 13
  • 85
  • 121
mike
  • 345
  • 2
  • 8
  • 19

3 Answers3

0

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.

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
0

Try the following query :

@sql = SELECT a, b, c, d 
FROM table t (NOLOCK) LEFT JOIN table2 t2 (NOLOCK) ON t.id = t2.id
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
Dev
  • 1
  • 1
-2

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.

user1880670
  • 162
  • 2
  • 2
  • 10