Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Poor performance while using Union/Intersect on bulk data Expand / Collapse
Author
Message
Posted Thursday, January 21, 2010 8:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Posted Thursday, January 21, 2010 11:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 6,849, Visits: 13,378
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
Posted Thursday, January 21, 2010 11:58 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Posted Friday, January 22, 2010 12:02 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #851770
Posted Friday, January 22, 2010 12:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Posted Friday, January 22, 2010 11:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #852473
Posted Saturday, January 23, 2010 12:24 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:20 PM
Points: 18,064, Visits: 16,099
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
Post #852482
Posted Monday, January 25, 2010 6:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Posted Monday, January 25, 2010 10:54 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:20 PM
Points: 18,064, Visits: 16,099
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
Post #853175
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse