Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Seriously Quirky Performance Behavior in a...
Seriously Quirky Performance Behavior in a Query
Rate Topic
Display Mode
Topic Options
Author
Message
pat.baggett
pat.baggett
Posted Wednesday, December 19, 2012 8:55 AM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, February 14, 2013 6:59 AM
Points: 4,
Visits: 12
I have come across a strange phenomenon when querying data from one of our historical databases and was wondering if someone could please provide some sort of explanation?
I am seeing an enormous performance hit when I execute a query containing a literal date range (embedded in the query) as opposed to using variables.
As an example, I have 2 queries:
Query 1
select count(a.[counter]), sum(a.[clmtotal]) from [direct].dbo.[claims] a with (nolock)
inner join [direct].dbo.[pclaim] c with (nolock) on c.counter = a.claimlink
where a.[type] = 'P'
and c.[hubid] = 1
and c.billdate between '10/1/2012' and '11/30/2012'
Execution time varies anywhere from 20 - 30 minutes (or more)
Query 2
declare @begdate datetime, @enddate datetime
select @begdate = '10/1/2012', @enddate = '11/30/2012'
select count(a.[counter]), sum(a.[clmtotal]) from [direct].dbo.[claims] a with (nolock)
inner join [direct].dbo.[pclaim] c with (nolock) on c.counter = a.claimlink
where a.[type] = 'P'
and c.[hubid] = 1
and c.billdate between @begdate and @enddate
Execution time averages around 5 -10 seconds
Why the difference? I can easily change my code to use the "variable approach", but I would really like an explanation as to this enormous difference as it may help to increase performance in other projects.
Thanks in advance.
Post Attachments
variables.sqlplan
(
6 views,
20.38 KB
)
no variables.sqlplan
(
6 views,
18.55 KB
)
Post #1398473
anthony.green
anthony.green
Posted Wednesday, December 19, 2012 8:59 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
My first guess would be with all the implicit conversions it has to do converting the strings '10/1/2012' and '11-30-2012' to datetime.
Can you upload the execution plans in a SQLPLAN format so we can see the differences between the two?
The link in my signature on posting performance problems will help if you are unsure.
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1398476
pat.baggett
pat.baggett
Posted Wednesday, December 19, 2012 9:08 AM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, February 14, 2013 6:59 AM
Points: 4,
Visits: 12
done - see attached.
Your explanation makes perfect sense though. Sometimes the answer is staring you right in the face - but you still need someone to point it out.
Thanks for the quick reply.
Post #1398487
GSquared
GSquared
Posted Wednesday, December 19, 2012 9:13 AM
SSCoach
Group: General Forum Members
Last Login: 2 days ago @ 1:55 PM
Points: 15,442,
Visits: 9,571
On the one with the string literals, try '20121130' and '20121001' instead of the US-formatted dates and see what happens.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1398493
GilaMonster
GilaMonster
Posted Wednesday, December 19, 2012 9:21 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:38 AM
Points: 37,726,
Visits: 29,987
Parameter sniffing, or lack thereof. Probably combined with stale statistics so that SQL gets a completely incorrect row estimate when it can sniff the 'parameters' (in this case string literals) while the variables, because they can't be sniffed give a rougher estimate that's not so likely to be affected by stale stats.
http://sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/
http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-2/
Look at the two estimated plans posted (actuals would be far better), look at the row estimations. Without the actual plans there's no way to see which of those estimates are more accurate, but I'll bet that the 5000 is closer than the 1.
p.s. Watch those nolocks.
http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1398498
Dave Ballantyne
Dave Ballantyne
Posted Wednesday, December 19, 2012 9:28 AM
SSCommitted
Group: General Forum Members
Last Login: Friday, May 10, 2013 4:07 PM
Points: 1,943,
Visits: 8,227
I think your statistics are out of date on that table.
Update them , then run the queries again (using WITH RECOMPILE) and see what the plans look like.
The estimations and therefore the plans are vastly different, you would normally expect a query with literal value to be faster, but it estimates at 1 row.
Clear Sky SQL
My Blog
Kent user group
Post #1398503
GilaMonster
GilaMonster
Posted Wednesday, December 19, 2012 9:33 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:38 AM
Points: 37,726,
Visits: 29,987
Dave Ballantyne (12/19/2012)
Update them , then run the queries again (using WITH RECOMPILE) and see what the plans look like.
No need to use recompile. The stats update alone would invalidate the plan.
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1398507
Dave Ballantyne
Dave Ballantyne
Posted Wednesday, December 19, 2012 9:53 AM
SSCommitted
Group: General Forum Members
Last Login: Friday, May 10, 2013 4:07 PM
Points: 1,943,
Visits: 8,227
GilaMonster (12/19/2012)
Dave Ballantyne (12/19/2012)
Update them , then run the queries again (using WITH RECOMPILE) and see what the plans look like.
No need to use recompile. The stats update alone would invalidate the plan.
Tru dat :)
Clear Sky SQL
My Blog
Kent user group
Post #1398518
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.