Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Oracle UNION Tips

Expert Oracle Database Tips by Donald BurlesonMarch 15, 2015

 

The UNION operator is great for merging the results of multiple queries that return similar rowsets, essentially executing each query separately and merging the results together into a single result set.  This is especially useful for merging remote tables using database links:

select * from fact_table@new_york
union
select * from fact_table@denver;

Whenever we need to address multiple tables in a single operation and we know that there will be no duplicate rows, we can use the UNION ALL statement to merge the tables together, as follows:

select * from fact_table_1_2011
union all
select * from fact_table_2_2011
union all
select * from fact_table_3_2011
order by
    order_year,
    order_month;

A UNION is highly optimized and really fast, except in cases where one query finishes long before the other, and Oracle must wait to get the whole result set before starting sorting.

 However, there are several alternatives to the union SQL operator:

  1. Use UNION ALL:  The UNION ALL may be faster when you don't mind the possibility of having duplicate rows in the result set.

  2. Execute each SQL separately and merge and sort the result sets within your program!  Sometimes, an external sort may be faster.
     
  3. Join the tables manually.  This is slow and inefficient.

  4. In versions, 10g and beyond, use the MODEL clause can simulate the behavior of the UNION clause.

  5. Use a scalar subquery.  Here we see a scalar subquery equivalent to a UNION operator:

    select * from
    (select col1, col2, col3 from Table_1) q1,
    (select col1, col2, col3 from Table_2) q2;


  6. Use an in-line view:  Starting in Oracle 11g release 2, the optimizer starts rewriting UNION ALL queries with the Join Factorization transformation (JFT) that rewrites some UNION ALL queries into in-line views.

  7.  Re-write the UNION using a FULL OUTER JOIN with the NVL function:  It is suggested that this has faster performance than the UNION operator.

    select
         empno,
         ename,
         nvl(dept.deptno,emp.deptno) deptno, dname
    from
         emp
    full outer join
    dept
    on
         (emp.deptno = dept.deptno) order by 1,2,3,4;

When tuning alternatives to UNION or UNION ALL, you always start by comparing and examining the execution plans, and tune each alternative independently, using the SQL*Plus �set timing on� to compares real run times.  See here, the ways to tune SQL queries by changing execution plans.

 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.









































 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster