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
»
Programming
»
General
»
Poor performance while using Union/Intersect...
Poor performance while using Union/Intersect on bulk data
Rate Topic
Display Mode
Topic Options
Author
Message
gaurav.kothari
gaurav.kothari
Posted Thursday, January 21, 2010 8:18 AM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, June 17, 2010 3:00 AM
Points: 7,
Visits: 33
Hi sql freaks,
I am having a query related to union and intersect operator when applying to bulk data.
i am having query like
select xID from table where SomeXColumn = 3 and SomeYColumn = 4
Union
select xID from table where SomeXColumn = 5 and SomeYColumn = 6
Union
select xID from table where SomeXColumn = 7and SomeYColumn = 8
Union
select xID from table where SomeXColumn = 9 and SomeYColumn = 10
---------------------------------------------------------------
Here is the details when each select statement is run separately.
Select Stmnt--->Record Return--->Time
Stmt 1--->10118--->00.00s
Stmt 2--->17134--->00.00s
Stmt 3--->16418--->00.00s
Stmt 4--->11114--->00.00s
------------------------------------------------------------------
when i run the above quey as a whole it is taking a tremendous time nearly 1 minute and 17 sec.
CAN ANY ONE help me out .
Thanks in advance,
Gaurav Kothari
Post #851291
LutzM
LutzM
Posted Thursday, January 21, 2010 11:10 AM
SSCertifiable
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 12:04 PM
Points: 6,739,
Visits: 12,167
One reason could be that you're using UNION instead of UNION ALL.
When combining two queries using UNION you'd force an (internal) DISTINCT to be applied to the result set, usually taking quite some time.
When using UNION ALL you'd get the results of each query, including dups.
It depends on the statements you use: If you can guarantee that there will be no duplicates by the nature of the query or if you can accept dups then use UNION ALL instead of UNION.
You can verify how much the sort operation will consume you could compare the actual execution plan of each statement.
Lutz
A pessimist is an optimist with experience.
How to get fast answers to your question
How to post performance related questions
Links for
Tally Table
,
Cross Tabs
and
Dynamic Cross Tabs
,
Delimited Split Function
Post #851448
gaurav.kothari
gaurav.kothari
Posted Thursday, January 21, 2010 11:58 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, June 17, 2010 3:00 AM
Points: 7,
Visits: 33
First of all thanks Lutz for the suggestion.
The reason why i used union is that i dont want to have duplicates.
Also, i am having smillar query with 8 intersect operators between different Select statemnts.
i checked the Query plan and found that clustered index scanning is taking 27% time.
A Quick help and good suggestion is appreciated,
Thanks again.
Gaurav Kothari
Post #851768
Jeff Moden
Jeff Moden
Posted Friday, January 22, 2010 12:02 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
Do you have indexes on the other two columns?
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #851770
gaurav.kothari
gaurav.kothari
Posted Friday, January 22, 2010 12:26 AM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, June 17, 2010 3:00 AM
Points: 7,
Visits: 33
Jeff,
i have clustered index on the tables primary key column.
and among all this three columns there is no primary key column.
--Gaurav
Post #851776
Jeff Moden
Jeff Moden
Posted Friday, January 22, 2010 11:18 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
If I understand correctly, that may be the problem... no indexes on the columns you're working with.
If you could gen and capture an actual execution plan, I'm sure someone here would be able to make a better recommendation as to what your plight may be.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #852473
SQLRNNR
SQLRNNR
Posted Saturday, January 23, 2010 12:24 AM
SSCoach
Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 18,857,
Visits: 12,441
Jeff Moden (1/22/2010)
If I understand correctly, that may be the problem... no indexes on the columns you're working with.
If you could gen and capture an actual execution plan, I'm sure someone here would be able to make a better recommendation as to what your plight may be.
Many people here are very good with execution plans. It is likely due to the indexes as Jeff has said.
Jason
AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server 2008
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #852482
gaurav.kothari
gaurav.kothari
Posted Monday, January 25, 2010 6:48 AM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, June 17, 2010 3:00 AM
Points: 7,
Visits: 33
Hi Jeff/Jason,
First of all thank to you guys, for propmptly replying to the Query.
I checked out the Execution plan on your suggestion (i generally dont do it, but from now onwards i will firstly look at it always) and found that the index scan was taking too much time.
i created a table with proper indexes and now the Query time has been reduced to 9 seconds from a magnanimous 2 mins 45 sec.
i am still trying on certain more areas to make it more effective.
Thanks a lot again for all your suggestion and replies.
--Gaurav
Post #852970
SQLRNNR
SQLRNNR
Posted Monday, January 25, 2010 10:54 AM
SSCoach
Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 18,857,
Visits: 12,441
You're welcome. Glad to be of assistance.
Jason
AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server 2008
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #853175
« 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.