Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:39:16+00:00 2026-06-03T13:39:16+00:00

UNION and UNION ALL queries can outperform equivalent queries using OR -connected predicates under

  • 0

UNION and UNION ALL queries can outperform equivalent queries using OR-connected predicates under certain circumstances. To my knowledge, this is partially because UNION subselects can be executed in parallel and they can thus have their own “sub-plan” specific to each part of the OR-connected predicate, which is probably far more optimal due to simpler applicable query transformations.

But writing OR-connected predicates is usually much more readable and concise, even if subquery factoring were applied to a UNION ALL solution. My question is: Is there a way to indicate to Oracle, that a single, costly OR-connected predicate should be transformed into a UNION ALL operation? If there is such a hint/method, under what circumstances can it be applied (e.g. do any constraints need to be present on the columns involved in the predicates, etc)? An example:

CREATE TABLE a AS
  SELECT 1 x, 2 y FROM DUAL UNION ALL
  SELECT 2 x, 1 y FROM DUAL;

-- This query...
SELECT * FROM a
WHERE x = 1 OR y = 1

-- Is sometimes outperformed by this one, for more complex table sources...
-- Note: in my case, I can safely apply UNION ALL. I know the two predicates to
-- be mutually exclusive.
SELECT * FROM a
WHERE x = 1
UNION ALL
SELECT * FROM a
WHERE y = 1

Note, I’m aware of the /*+ USE_CONCAT */ hint:

SELECT /*+ USE_CONCAT */ * FROM a
WHERE x = 1 OR y = 1

But it doesn’t seem to produce what I need (no forced UNION ALL operation in the execution plan):

-------------------------------------------
| Id  | Operation         | Name | E-Rows |
-------------------------------------------
|   0 | SELECT STATEMENT  |      |        |
|*  1 |  TABLE ACCESS FULL| A    |      2 |
-------------------------------------------

Maybe, there is some restriction to this hint? I have Oracle 11g2 available for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:39:16+00:00Added an answer on June 3, 2026 at 1:39 pm

    I believe this may have something to do with indexes existing on the columns you use in the OR predicate.

    I tested using the following in 11gR2.

    create table scott.test as 
    select level l, 
           decode(mod(level,2), 1, 1, 2) x, 
           decode(mod(level,2), 1, 2, 1) y, 
           dbms_random.value(1, 3) z from dual 
    connect by level < 1000;
    /
    
    begin
       dbms_stats.gather_table_stats('scott', 'test');
    end;
    /
    

    I then explained the following queries in TOAD, (EXPLAIN PLAN FOR)

    select x, y, z from scott.test
        where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
        ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
      TABLE ACCESS FULL COS_DM.TEST 10      280     4   
    
    select /*+ USE_CONCAT */ x, y, z from scott.test
    where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
    ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
      TABLE ACCESS FULL COS_DM.TEST 10      280     4                                
    
    
    select x, y, z from test where (floor(z) = 1 and x = 1)
    union all
    select x, y, z from test where (floor(z) = 2 and y = 1)
    ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
      UNION-ALL                                              
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    

    So it appears the hint’s not working. I then added an index to the x & y columns:

    create index test_x on test (x, y);
    
    begin
       dbms_stats.gather_table_stats('scott', 'test');
    end;
    /
    

    Rerunning the queries now:

    select x, y, z from scott.test
        where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
        ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
      TABLE ACCESS FULL COS_DM.TEST 10      280     4   
    
    select /*+ USE_CONCAT */ x, y, z from scott.test
    where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
    ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
      CONCATENATION                                              
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    
    select x, y, z from test where (floor(z) = 1 and x = 1)
    union all
    select x, y, z from test where (floor(z) = 2 and y = 1)
    ;
    
    SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
      UNION-ALL                                              
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
        TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    

    It appears that after adding the index (even though it’s not being used) the optimizer decided to use the hint after all!

    Perhaps you could try this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Firebird, I want to combine the results of two queries using UNION ALL,
I have this queries, but I need to make a Union for all. But
I came across a scenario where I had to use Union all, how can
(SELECT field1 FROM Table1) UNION (SELECT field1 FROM Table2); This gets all rows from
I'm trying to get a UNION ALL working with a CTE which I'm using
Oracle SQL can do hierarchical queries since v2, using their proprietary CONNECT BY syntax.
I have a sql statement that is a union of several queries which all
INSERT INTO files (fileUID, filename) WITH fileUIDS(fileUID) AS ( VALUES(1) UNION ALL SELECT fileUID+1
I want to union the result of two sub-queries (say SUB1 and SUB2). The
Suppose I define a union like this: #include <stdio.h> int main() { union u

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.