Solved

Is there is any alternatives for big union query in sql ?

Posted on 2010-11-13
7
794 Views
Last Modified: 2012-05-10
Is there is any alternatives for big union query in sql ?
how do i split big union querys ?
0
Comment
Question by:Varshini
7 Comments
 

Expert Comment

by:dhshah_in
ID: 34127369
The UNION query is quite efficient and works even on large tables. I have personally run it on tables as large as few hundred million records and it completes in a day or two max. But the important thing here is RAM. You need a good amount of RAM to speed up your query.

Suppose that is not an option, then below is the alternative:

The purpose of running a UNION query is to merge two tables and remove duplicates.
The fastest alternative is to simply insert the second table into the first table. I do this using the import/export wizard which is quite fast in inserting rows.

After the insert is over, we now need to remove duplicates.

For this I use the procedure provided by Microsoft in this wonderful step by step article: http://support.microsoft.com/kb/139444


0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34133148
it depends on what your union query is actually doing...

please post it....

or at least give further information on its structure...

e.g.  is it returning a result set for multiple criteria but against the same underlying tables...
  or is it returning a result set from multiple table sources?

what do you mean by big ...?
a large number of rows in the result set , or the query itself occupies many lines of code?
0
 
LVL 6

Accepted Solution

by:
subhashpunia earned 500 total points
ID: 34134907
Use a temporary table to insert all data from all queries that are in UNION one by one. This way you would be able to process queries one at a time with smaller transactions. It would also help in making the process better understandable.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Varshini
ID: 34144016
hi Lowfatspread:

  it is not the result set of same tables its from different tables.
the union is like ....

select  busineess_id,date,sales_amount from (
select  busineess_id,date,sales_amount from  sales_southzone il where date='2010-11-11'
union
select  busineess_id,date,sales_amount from  sales_northzone  where date='2010-11-11'
union
select  busineess_id,date,sales_amount from  sales_eastzone  where date='2010-11-11'
union
select  busineess_id,date,sales_amount from  sales_westzone  where date='2010-11-11'
union
select  busineess_id,date,sales_amount from  sales_northeastzone  where date='2010-11-11'
union
select  busineess_id,date,sales_amount from  sales_southeastzone  where date='2010-11-11'
) from a where a.

--- each where clause has to many conditions and final outer query also has more condition and some query has included left out join

0
 
LVL 7

Expert Comment

by:twol
ID: 34170085
If you place the queries in a stored procedure, it will allow you to insert the rows from the queries into a temp table. Then you can query the temp table with the final conditions and joins and return that as the result set. The stored procedure will allow for a lot of flexibility in how you build the temptable and could provide easier maintenance in the long run.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34179632
what is the reason for the underlying table structure?

do the tables exist in the same database?
could a partioned view be constructed over the tables?
0
 

Author Comment

by:Varshini
ID: 34205682
the db has  created 5 years back. So i do not know the reason.   The tables exists in the same db.
I have no idea of partioned  views and in the query i have so many outer joins so i do not able to create indexed views.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question