SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query improvement


Query improvement

Author
Message
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4438 Visits: 3672
Krishna1 (6/27/2013)
But if you see my query it has got differnt where clasues for the 2 query hence can not use roll up.

I overlooked that the WHERE clause is different. In that case I don't think there is another way to write the query. But I agree with Sean Lange to replace your UNION with the UNION ALL statement if your resultset doesn't have to be GROUPED BY.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16532 Visits: 19557
Why read the table twice?
SELECT 
sumyy = sum(yy),
xx = ???('summary line') -- where ??? is an aggregate function
FROM tablea
CROSS APPLY (
SELECT SurrogateGroup = CASE
WHEN somedate_column < @startdate_parameter THEN <<constant value>>
WHEN somedate_column between @startdate_parameter and @enddate_parameter THEN <<tablea.PK>>
ELSE NULL end
) x
WHERE SurrogateGroup IS NOT NULL -- or use somedate_column <= @enddate_parameter
GROUP BY SurrogateGroup



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3936 Visits: 8472
ChrisM@Work (6/27/2013)
Why read the table twice?
SELECT 
sumyy = sum(yy),
xx = ???('summary line') -- where ??? is an aggregate function
FROM tablea
CROSS APPLY (
SELECT SurrogateGroup = CASE
WHEN somedate_column < @startdate_parameter THEN <<constant value>>
WHEN somedate_column between @startdate_parameter and @enddate_parameter THEN <<tablea.PK>>
ELSE NULL end
) x
WHERE SurrogateGroup IS NOT NULL -- or use somedate_column <= @enddate_parameter
GROUP BY SurrogateGroup



Careful with this approach Chris, the constant scan generated is often worse than just hitting the table twice depending on indexes, data size etc. I do always like to test that approach when doing something that needs a "working" column, but it often loses out in my environment.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search