November 29, 2008

UNION and UNION ALL return different result !

Filed under: Works

UNION and UNION ALL operations are used to combine two or more queries in a table.

UNION is best used for retrieving data in a sort-unique way. Oracle will process each data retrieved from the table and compare it if there is double values. UNION operation eliminates the duplicated data from the result set. It is usefull if you want distinct value to be retrieved from both collections. This extra process could takes time.

UNION ALL is best used when you want to join two or more tables without care about data duplication. UNION ALL operation is much faster than UNION because it does not require extra process for removing duplication.

So which one provides better performance? It depends on your need. But when you look at the auto trace result for both of them. On the execution plans, you can notice that UNION operation uses UNION ALL operation in execution. Shortly, it can be said that UNION = UNION ALL + sort operations.

So the best practice for this would be to use UNION ALL instead of UNION (whenever possible).

 

2 Comments »

The URI to TrackBack this entry is: http://1124.blogsome.com/2008/11/29/union-and-union-allon-return-different-result/trackback/

  1. ra,makin jago aja nih databasenya :D

    Comment by nico — December 1, 2008 @ 2:45 am

  2. Hehehe, satu korban :p
    Gw lupa naro ini :
    *I AM A NEWBIE, so dont trust every single word that I wrote here..kekeke

    Comment by Administrator — December 1, 2008 @ 10:42 am

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Helga Cleve