variable substitution vs. executing dynamic strings
- From: no.spam@xxxxxxxxxxxxx
- Date: Thu, 19 Oct 2006 07:41:53 -0400
First off_ pure - uneducated - hacker - novice - alert!! If you are offended by
silly questions close this posting immediately!
I'm sure this has come up 1,000K times before and I have googled the issue_
Still have not found a satisfactory explanation.
I have a table 'alarms' with several defined indexes. The index alarm_id is just
what you would expect a auto increment key field.
In the follow code returns around 1768 records. WHY is the second query so much
faster than the first? Executing strings in sprocs seems insane, not to mention
bug-ugly to write_ but for whatever reason the evaluation of the variables for
the first query seem to make it take significantly longer to run_ If I up the
returned record count to 500K or so the difference is minimal. That implies to
me that the compile and substitution time is the issue not the actual reading of
the data. Query execution plan shows a 9:1 speed difference. Trace shows a 1.5:1
difference. Not sure I beleve either one of them but in practice using my stop
watch the difference is noticable.
(This is actually an sproc where the start / end variables are passed_ The
actual query in the sproc has a number of joins etc however all that stuff was
removed as it appears to be irrelevant. The server an 8 cpu machine running
sql2K)
declare @start int
declare @end int
declare @string nvarchar(500)
select @start = 35300000
select @end = 35301768
SELECT *
FROM alarm
WHERE (alarm_id BETWEEN @start AND @end)
select @string = 'SELECT * FROM alarm WHERE (alarm_id BETWEEN '''+ cast(@start
as nvarchar(8)) + ''' AND '''+ cast(@end as nvarchar(8)) + ''')'
exec(@string)
.
- Follow-Ups:
- Re: variable substitution vs. executing dynamic strings
- From: Russ Rose
- Re: variable substitution vs. executing dynamic strings
- Prev by Date: Re: SQL 2005 encryption: OPEN SYMMETRIC KEY in view?
- Next by Date: Openquery and Oracle output parameters
- Previous by thread: Jscript msgbox
- Next by thread: Re: variable substitution vs. executing dynamic strings
- Index(es):
Relevant Pages
|
Loading