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


using multiple UNION in query


using multiple UNION in query

Author
Message
Abhijit More
Abhijit More
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 767
Hello!
I have one stored proc which returns data by querying multiple tables using union clause without any search criteria.

select xyz as Col1 from table1 join table2 ...(total 8 joins)
union
select abc as Col1 from table1 join table3...(total 8 joins)
union
select pqras Col1 from table1 join table4...(total 8 joins)
union
select pqras Col1 from table1 join table5...(total 8 joins)

As per database design Table1 represent supertype and Table3,4,5 represent sub types. Table1 has 1146090 rows. Not sure what to do with this. It showed up as leading in average reads at 14 million per execution and runs for 35 seconds. It is going to get a lot worse looking at the tables it's selecting from. It is basically doing a select across lots of tables, some large tables, without a select criteria. It's doing a bunch of in clustered index scans... appreciate your help here.

Abhijit - http://abhijitmore.wordpress.com
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5178 Visits: 5478
Abhijit More (5/14/2013)
Hello!
I have one stored proc which returns data by querying multiple tables using union clause without any search criteria.

select xyz as Col1 from table1 join table2 ...(total 8 joins)
union
select abc as Col1 from table1 join table3...(total 8 joins)
union
select pqras Col1 from table1 join table4...(total 8 joins)
union
select pqras Col1 from table1 join table5...(total 8 joins)

As per database design Table1 represent supertype and Table3,4,5 represent sub types. Table1 has 1146090 rows. Not sure what to do with this. It showed up as leading in average reads at 14 million per execution and runs for 35 seconds. It is going to get a lot worse looking at the tables it's selecting from. It is basically doing a select across lots of tables, some large tables, without a select criteria. It's doing a bunch of in clustered index scans... appreciate your help here.


How do you want us to help?
Can you drop this proc and never use?
If not, what are the real requirements?
It's impossible to advise on something like that - too little information.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Abhijit More
Abhijit More
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 767
How do you want us to help? - need help to optimize the query.
Can you drop this proc and never use? - not possible
If not, what are the real requirements? - requirement is to get all record that satisfy the join criteria
It's impossible to advise on something like that - too little information. - can u tell me what information do you need

Abhijit - http://abhijitmore.wordpress.com
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27002 Visits: 17557
Abhijit More (5/14/2013)
How do you want us to help? - need help to optimize the query.
Can you drop this proc and never use? - not possible
If not, what are the real requirements? - requirement is to get all record that satisfy the join criteria
It's impossible to advise on something like that - too little information. - can u tell me what information do you need


Seriously with 900 points you have surely at least one or two forum posts. We can't see your screen, we can't read your mind, we don't know your data structures, we don't know the business rules...in short we don't know anything about your system or your issue.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5178 Visits: 5478
Abhijit More (5/14/2013)
How do you want us to help? - need help to optimize the query.
Can you drop this proc and never use? - not possible
If not, what are the real requirements? - requirement is to get all record that satisfy the join criteria
It's impossible to advise on something like that - too little information. - can u tell me what information do you need


Details!

I guess you should start from here: http://www.sqlservercentral.com/articles/Best+Practices/61537/

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Abhijit More
Abhijit More
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 767
sorry friends it my bad....i should have frame the question correctly...but for now I think i found the solution for my problem...sorry for the inconvenience

Abhijit - http://abhijitmore.wordpress.com
lnardozi 61862
lnardozi 61862
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 615
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.
Phil Parkin
Phil Parkin
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19410 Visits: 20462
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14772 Visits: 15980
Phil Parkin (5/16/2013)
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?

I suppose because UNION eliminates duplicates you only need to do it once - as long as you make sure that UNION is the last one evaluated. I don't know whether the query optimizer is smart enough to do that implicitly if you use more than one UNION - so it may or may not make a difference from a performance point of view.

John
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27002 Visits: 17557
John Mitchell-245523 (5/16/2013)
Phil Parkin (5/16/2013)
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?

I suppose because UNION eliminates duplicates you only need to do it once - as long as you make sure that UNION is the last one evaluated. I don't know whether the query optimizer is smart enough to do that implicitly if you use more than one UNION - so it may or may not make a difference from a performance point of view.

John


But that is only good if you want to eliminate ALL duplicates. It is very possible that you have 3 tables and in the first 2 tables you want duplicates but if the same value shows up in the third table you want only distinct values. Other times maybe you want to include all duplicates.

The blanket statement that "you never use more than one UNION, which is the last one. All the rest should be UNION ALL." is just completely not true. There are time when that is correct but not always.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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