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


Poor performance while using Union/Intersect on bulk data


Poor performance while using Union/Intersect on bulk data

Author
Message
gaurav.kothari
gaurav.kothari
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10825 Visits: 13559
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
gaurav.kothari
gaurav.kothari
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91244 Visits: 41151
Do you have indexes on the other two columns?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
gaurav.kothari
gaurav.kothari
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91244 Visits: 41151
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 Row-By-Agonizing-Row.
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33900 Visits: 18560
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

gaurav.kothari
gaurav.kothari
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33900 Visits: 18560
You're welcome. Glad to be of assistance.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

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