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,206
  • 11
  • 83
  • 119
mike
  • 285
  • 2
  • 7
  • 16

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
  • 11,823
  • 5
  • 27
  • 57
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
  • 15,874
  • 6
  • 36
  • 42
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
  • 152
  • 2
  • 2
  • 10