September 4, 2007 at 2:05 am
Hi Experts,
Will Dynamic sql affect performance or not ?
------------------------------------------------------
select name into #t1
from sysobjects
where type ='U'
and name not in ('ProductCommitmentWorkflow',
'Tranche',
'CompanyTrans',
'ProductFeatureSelection',
'ProductFeature',
'ProductSubType',
'ProductType'
)
create unique index id1 on #t1(name)
declare p1 cursor
for select name from #t1
for read only
open p1
declare @name varchar(40)
fetch p1 into @name
while @@fetch_status = 0
begin
declare @sql varchar(255)
select @sql = 'Truncate table '+ @name
exec (@sql)
fetch p1 into @name
end
close p1
deallocate p1
--------------------------------------------------------------------
Regards
Karthik
karthik
September 4, 2007 at 2:29 am
Karthik,
It depends on how log it takes to execute the above query. If it takes more than a few seconds, then the answer is likely that it does not matter that you are using dynamic SQL.
SQL Server uses caching for query plans. This cache is based on the text of the query, so in case of dynamic SQL the plan will not be reused if the generated string is different. It costs time to generate the query plan. So if your query is running fast (less than a few seconds) and is executed frequently, I would explore a solution that is not using dynamic SQL.
Regards,
Andras
September 4, 2007 at 2:32 am
Karthik,
also, instead of using cursors to iterate through the tables you may want to consider sp_msforeachtable . (this is not in books online, but searching these forums or google helps )
Andras
September 4, 2007 at 6:11 am
To be fair, sp_msforeachtable uses a cursor as well, but at least it's already written for you.
September 4, 2007 at 4:35 pm
to perform DDL operations like the ones you posted (truncate tables) dynamic sql does *not* matter in terms of performance because it won't be dominated by the query but by the I/O instead.
* Noel
September 4, 2007 at 5:36 pm
Karthik,
You didn't believe me?
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=395872#bm396072
--Jeff Moden
Change is inevitable... Change for the better is not.
September 5, 2007 at 10:11 am
Jeff,
I am believing you !
Are you Happy now ?
I thought this is different question, then only i posted this question.
Regards
Karthik
karthik
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply