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

  • Home
  • SEARCH
  • 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 8962789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:12:42+00:00 2026-06-15T16:12:42+00:00

I am using Oracle 11g XE. Simple question: which is better? Query A, B

  • 0

I am using Oracle 11g XE.

“Simple” question: which is better? Query A, B or C? I am trying to achieve the fastest response time of my query for resultset pagination. My pages will contain at most 20 results.

A:

SELECT /*+ FIRST_ROWS(20) */ *
FROM
  ((SELECT SCORE(1) RANK, F.ID, 1 AS "IsFile"
    FROM "File" F
    WHERE CONTAINS("ContentCLOB", 'April 2009', 1) > 0)
  UNION ALL
    (SELECT SCORE(2) RANK, C.ID, 0 AS "IsFile"
    FROM "Claim" C
    WHERE CONTAINS(C."IdClaim", 'jjkl', 2) > 0))
ORDER BY RANK DESC

B:

  ((SELECT /*+ FIRST_ROWS(20) */ SCORE(1) RANK, F.ID, 1 AS "IsFile"
    FROM "File" F
    WHERE CONTAINS("ContentCLOB", 'April 2009', 1) > 0)
  UNION ALL
    (SELECT /*+ FIRST_ROWS(20) */ SCORE(2) RANK, C.ID, 0 AS "IsFile"
    FROM "Claim" C
    WHERE CONTAINS(C."IdClaim", 'jjkl', 2) > 0))
ORDER BY RANK DESC

C:

 Like B but without the hints.

The only difference in Explain Plans is that query A has an extra View before ordering.

  • 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-15T16:12:44+00:00Added an answer on June 15, 2026 at 4:12 pm

    adding first rows to the sub queries is usually a good idea (ignore the VIEW step, its there because on the first query you have “select .. from ()” which is the extra step).

    I think the following will be better for you as I will guess that your SORT ORDER BY occurs after the table access. Also how are you paginating as I don’t see any rownum stuff in there (did you just simplify it for this case?).

    select r,
           case "IsFile" when 1 then (select id from File f where f.rowid = a.rid)
             when 0 then (select id from Claim c where c.rowid = a.rid)
           end id,
           "IsFile"
      from (select /*+ first_rows(20) */ score(100) r, rowid rid,  1 AS "IsFile"
               from File s
              where contains(ContentCLOB, 'z', 100) > 0
             union all
             select /*+ first_rows(20) */ score(1) r, rowid rid, 0 AS "IsFile"
               from Claim s
              where contains(IdClaim, 'z', 1) > 0
              order by r desc) a
     where rownum <= 20;
    

    the case at the end is only needed if you want ID and rowid isnt good enough. eg heres a small test:

    SQL> set autotrace on
    SQL> select rank, id, "IsFile"
      2    from (select rank, id, "IsFile", rownum rn
      3             from (select /*+ FIRST_ROWS(20) */
      4                     *
      5                      from ((select score(1) rank, f.id, 1 as "IsFile"
      6                                from File f
      7                               where contains(ContentCLOB, 'z', 1) > 0) union all
      8                             (select score(2) rank, c.id, 0 as "IsFile"
      9                                from Claim c
     10                               where contains(c.IdClaim, 'z', 2) > 0))
     11                     order by rank desc))
     12   where rn >= 1
     13     and rownum <= 20
     14  /
    
          RANK         ID IsFile
    ---------- ---------- ----------
            25      16373          0
            21       1192          1
            21      13477          0
            21       5394          0
            21       2870          0
            17        113          1
            17      19874          0
            17       1939          1
            17       1765          1
            17       2322          1
            17       3195          1
    
          RANK         ID IsFile
    ---------- ---------- ----------
            17       4248          1
            17       4346          1
            17       4183          1
            17       8444          1
            17       9040          1
            17       9395          1
            17      10502          1
            17      10131          1
            17      11027          1
    
    20 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1202090801
    
    --------------------------------------------------------------------------------------------------
    | Id  | Operation                          | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                   |             |    20 |   840 |     9  (12)| 00:00:01 |
    |*  1 |  COUNT STOPKEY                     |             |       |       |            |          |
    |*  2 |   VIEW                             |             |    24 |  1008 |     9  (12)| 00:00:01 |
    |   3 |    COUNT                           |             |       |       |            |          |
    |   4 |     VIEW                           |             |    24 |   696 |     9  (12)| 00:00:01 |
    |   5 |      SORT ORDER BY                 |             |    24 |   696 |     9  (12)| 00:00:01 |
    |   6 |       VIEW                         |             |    24 |   696 |     8   (0)| 00:00:01 |
    |   7 |        UNION-ALL                   |             |       |       |            |          |
    |   8 |         TABLE ACCESS BY INDEX ROWID| FILE    |    10 | 20270 |     4   (0)| 00:00:01 |
    |*  9 |          DOMAIN INDEX              | CTXIDX1     |       |       |     4   (0)| 00:00:01 |
    |  10 |         TABLE ACCESS BY INDEX ROWID| CLAIM |    14 | 28378 |     4   (0)| 00:00:01 |
    |* 11 |          DOMAIN INDEX              | CTXIDX2     |       |       |     4   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter(ROWNUM<=20)
       2 - filter("RN">=1)
       9 - access("CTXSYS"."CONTAINS"("CONTENTCLOB",'z',1)>0)
      11 - access("CTXSYS"."CONTAINS"("R"."IDCLAIM",'z',2)>0)
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    
    Statistics
    ----------------------------------------------------------
             51  recursive calls
              0  db block gets
           6506  consistent gets
              0  physical reads
              0  redo size
            760  bytes sent via SQL*Net to client
            375  bytes received via SQL*Net from client
              3  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
             20  rows processed
    

    versus

    SQL> select rank, id, "IsFile"
      2    from (select rank,
      3                  case "IsFile"
      4                     when 1 then
      5                      (select id from File f where f.rowid = a.rid)
      6                     when 0 then
      7                      (select id from Claim c where c.rowid = a.rid)
      8                   end id, "IsFile", rownum r
      9             from (select /*+ first_rows(20) */
     10                     score(100) rank, rowid rid, 1 as "IsFile"
     11                      from File s
     12                     where contains(ContentCLOB, 'z', 100) > 0
     13                    union all
     14                    select /*+ first_rows(20) */
     15                     score(1) rank, rowid rid, 0 as "IsFile"
     16                      from Claim s
     17                     where contains(IdClaim, 'z', 1) > 0
     18                     order by rank desc) a)
     19   where r >= 1
     20     and rownum <= 20
     21  /
    
          RANK         ID IsFile
    ---------- ---------- ----------
            25      16373          0
            21       1192          1
            21      13477          0
            21       5394          0
            21       2870          0
            17        113          1
            17      19874          0
            17       1939          1
            17       1765          1
            17       2322          1
            17       3195          1
    
          RANK         ID IsFile
    ---------- ---------- ----------
            17       4248          1
            17       4346          1
            17       4183          1
            17       8444          1
            17       9040          1
            17       9395          1
            17      10502          1
            17      10131          1
            17      11027          1
    
    20 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1724352232
    
    -------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |             |    20 |   840 |     9  (12)| 00:00:01 |
    |   1 |  TABLE ACCESS BY USER ROWID | FILE    |     1 |    25 |     1   (0)| 00:00:01 |
    |   2 |   TABLE ACCESS BY USER ROWID| CLAIM |     1 |    25 |     1   (0)| 00:00:01 |
    |*  3 |  COUNT STOPKEY              |             |       |       |            |          |
    |*  4 |   VIEW                      |             |    24 |  1008 |     9  (12)| 00:00:01 |
    |   5 |    COUNT                    |             |       |       |            |          |
    |   6 |     VIEW                    |             |    24 |   672 |     9  (12)| 00:00:01 |
    |   7 |      SORT ORDER BY          |             |    24 | 48336 |     8  (50)| 00:00:01 |
    |   8 |       UNION-ALL             |             |       |       |            |          |
    |*  9 |        DOMAIN INDEX         | CTXIDX1     |    10 | 20140 |     4   (0)| 00:00:01 |
    |* 10 |        DOMAIN INDEX         | CTXIDX2     |    14 | 28196 |     4   (0)| 00:00:01 |
    -------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       3 - filter(ROWNUM<=20)
       4 - filter("R">=1)
       9 - access("CTXSYS"."CONTAINS"("CONTENTCLOB",'z',100)>0)
      10 - access("CTXSYS"."CONTAINS"("IDCLAIM",'z',1)>0)
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    
    Statistics
    ----------------------------------------------------------
             51  recursive calls
              0  db block gets
            216  consistent gets
              0  physical reads
              0  redo size
            760  bytes sent via SQL*Net to client
            375  bytes received via SQL*Net from client
              3  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
             20  rows processed
    

    same result, but note the IO:
    original:

       6506  consistent gets
    

    delaying the table access till after the sort:

        216  consistent gets
    

    depending on your index/table size, the savings could be significantly more.

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

Sidebar

Related Questions

I'm using oracle 11g and trying to optimize a query. The basic structure of
I am using Oracle 11g client, with ODP.NET. I am trying to add conditional
I am using REGEXP_SUBSTR in Oracle 11g and I am having difficulty trying to
Using Oracle 11g release 2, the following query gives an ORA-01790: expression must have
I'm using oracle 11g. I'm inserting date and time using function SYSDATE into column
Teaching myself SQL, and using an employee info db, on oracle 11g. I'm trying
I am using oracle 11g and have written a stored procedure which stores values
I am using oracle 11g . I am trying to run a procedure inside
I'm trying to dequeue using Oracle 11g queue using standlone java. Here is the
Using Oracle 11g, I have the following LDAP string which is only a subset

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.