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 6714727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:31:46+00:00 2026-05-26T08:31:46+00:00

I have a table discussion of which structure is following: Table name: discussion, Id

  • 0

I have a table “discussion” of which structure is following:

Table name: discussion,

Id      name       parent_id   root_id  ...other columns
 1   discussion1      0            0
 2   discussion2      1            1
 3   discussion3      2            1
 4   discussion4      3            1
 5   discussion5      4            1

Obviously id was defined as PK, parent_id and root_id has been indexed.

these rows of table was built as a hierarchical relationship(sort of parent->child), As well as please note column root_id is used to describe those rows are in the same tree.

I written two SQLs to get the thread tree:

SQL 1

  SELECT * 
    from discussion 
   start with (parent_Id=0 AND id=?) 
 connect by prior Id=parent_Id

SQL 2

 SELECT * FROM (
           SELECT * FROM discussion WHERE root_id = ? or id = ?
          )START WITH (parent_Id=0 AND id=?) connect by prior Id=parent_Id

After my test, if the data sets are around 4000, SQL 1 did a little bit better than SQL2.
But if table discussion has very large data, SQL 2 did much better than SQL1.

When the table has 300 thousands rows, query plan reported the cost of SQL 1 is 22126, The cost of SQL 2 is 6. SQL 1 get involved full table scan or SQL 2 get range index scan.

Is anybody can help me to explain why the two SQLs show different result with different data number set?

More important is I want your guys suggestions to improve the performance or another solution?

Here is query plan for SQL 1 when we have 300 thousands rows.

GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name   : staName52149
Tuning Task Owner  : CISCO
Tuning Task ID     : 54
Workload Type      : Single SQL Statement
Execution Count    : 1
Current Execution  : EXEC_44
Execution Type     : TUNE SQL
Scope              : COMPREHENSIVE
Time Limit(seconds): 1800
Completion Status  : COMPLETED
Started at         : 10/23/2011 23:23:31
Completed at       : 10/23/2011 23:23:59

-------------------------------------------------------------------------------
Schema Name: CISCO
SQL ID     : davhv6p4x6bu2
SQL Text   : SELECT * from discussion start with (parent_Id=0 AND id=6587)
             connect by prior Id=parent_Id

-------------------------------------------------------------------------------
FINDINGS SECTION (1 finding)
-------------------------------------------------------------------------------

1- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
  A potentially better execution plan was found for this statement.

  Recommendation (estimated benefit: 99.99%)
  ------------------------------------------
  - Consider accepting the recommended SQL profile.
    execute dbms_sqltune.accept_sql_profile(task_name => 'staName52149',
            task_owner => 'CISCO', replace => TRUE);

  Validation results
  ------------------
  The SQL profile was tested by executing both its plan and the original plan
  and measuring their respective execution statistics. A plan may have been
  only partially executed if the other could be run to completion in less time.

                           Original Plan  With SQL Profile  % Improved
                           -------------  ----------------  ----------
  Completion Status:            COMPLETE          COMPLETE
  Elapsed Time(us):            14251990               121      99.99 %
  CPU Time(us):                 3463222                 0        100 %
  User I/O Time(us):           10821745                 0        100 %
  Buffer Gets:                   678361                 6      99.99 %
  Physical Read Requests:          1013                 0        100 %
  Physical Write Requests:         1081                 0        100 %
  Physical Read Bytes:        234168320                 0        100 %
  Physical Write Bytes:       223756288                 0        100 %
  Rows Processed:                     1                 1 
  Fetches:                            1                 1 
  Executions:                         1                 1 

  Notes
  -----
  1. The SQL profile plan was first executed to warm the buffer cache.
  2. Statistics for the SQL profile plan were averaged over next 9 executions.

-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------

1- Original
-----------
Plan hash value: 2811036397


------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |            |   348K|  2770M| 22162  (82)| 00:04:26 |
|*  1 |  CONNECT BY NO FILTERING WITH START-WITH|            |       |       |            |          |
|   2 |   TABLE ACCESS FULL                     | DISCUSSION |   348K|    79M|  4108   (1)| 00:00:50 |
------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1
   2 - SEL$2 / DISCUSSION@SEL$2

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DISCUSSION"."PARENT_ID"=PRIOR NULL)
       filter("PARENT_ID"=0 AND "ID"=6587)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "DISCUSSION"."PARENT_ID"[NUMBER,22], "DISCUSSION"."ID"[NUMBER,22], STRDEF[22], 
       STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[4000], STRDEF[22], STRDEF[22], 
       STRDEF[150], STRDEF[7], STRDEF[7], STRDEF[4000], STRDEF[22], STRDEF[22], STRDEF[22], 
       STRDEF[4000], STRDEF[22], STRDEF[4000], STRDEF[7], STRDEF[22], STRDEF[32], STRDEF[22], 
       STRDEF[22], STRDEF[22], STRDEF[7], STRDEF[7], PRIOR NULL[22], LEVEL[4]
   2 - "ID"[NUMBER,22], "DISCUSSION"."CLASS"[NUMBER,22], "DISCUSSION"."SUBCLASS"[NUMBER,22], 
       "PARENT_ID"[NUMBER,22], "DISCUSSION"."ROOT_ID"[NUMBER,22], 
       "DISCUSSION"."MESSAGE"[VARCHAR2,4000], "DISCUSSION"."STATUS"[NUMBER,22], 
       "DISCUSSION"."PRIORITY"[NUMBER,22], "DISCUSSION"."AUTO_NUMBER"[VARCHAR2,150], 
       "DISCUSSION"."CREATE_DATE"[DATE,7], "DISCUSSION"."CLOSE_DATE"[DATE,7], 
       "DISCUSSION"."CLOSE_COMMENTS"[VARCHAR2,4000], "DISCUSSION"."ORGANIZATION"[NUMBER,22], 
       "DISCUSSION"."TYPE"[NUMBER,22], "DISCUSSION"."CREATOR"[NUMBER,22], 
       "DISCUSSION"."SUBJECT"[VARCHAR2,4000], "DISCUSSION"."REPLIES"[NUMBER,22], 
       "DISCUSSION"."NOTIFYLIST"[VARCHAR2,4000], "DISCUSSION"."LAST_REPLY_DATE"[DATE,7], 
       "DISCUSSION"."DELETE_FLAG"[NUMBER,22], "DISCUSSION"."FLAGS"[VARCHAR2,32], 
       "DISCUSSION"."PREVIEW_TEXT"[NUMBER,22], "DISCUSSION"."FILTER"[NUMBER,22], 
       "DISCUSSION"."OBJVERSION"[NUMBER,22], "DISCUSSION"."CREATED"[DATE,7], 
       "DISCUSSION"."LAST_UPD"[DATE,7]

2- Original With Adjusted Cost
------------------------------
Plan hash value: 2811036397


------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |            |   348K|  2770M| 22162  (82)| 00:04:26 |
|*  1 |  CONNECT BY NO FILTERING WITH START-WITH|            |       |       |            |          |
|   2 |   TABLE ACCESS FULL                     | DISCUSSION |   348K|    79M|  4108   (1)| 00:00:50 |
------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1
   2 - SEL$2 / DISCUSSION@SEL$2

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DISCUSSION"."PARENT_ID"=PRIOR NULL)
       filter("PARENT_ID"=0 AND "ID"=6587)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "DISCUSSION"."PARENT_ID"[NUMBER,22], "DISCUSSION"."ID"[NUMBER,22], STRDEF[22], 
       STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[4000], STRDEF[22], STRDEF[22], 
       STRDEF[150], STRDEF[7], STRDEF[7], STRDEF[4000], STRDEF[22], STRDEF[22], STRDEF[22], 
       STRDEF[4000], STRDEF[22], STRDEF[4000], STRDEF[7], STRDEF[22], STRDEF[32], STRDEF[22], 
       STRDEF[22], STRDEF[22], STRDEF[7], STRDEF[7], PRIOR NULL[22], LEVEL[4]
   2 - "ID"[NUMBER,22], "DISCUSSION"."CLASS"[NUMBER,22], "DISCUSSION"."SUBCLASS"[NUMBER,22], 
       "PARENT_ID"[NUMBER,22], "DISCUSSION"."ROOT_ID"[NUMBER,22], 
       "DISCUSSION"."MESSAGE"[VARCHAR2,4000], "DISCUSSION"."STATUS"[NUMBER,22], 
       "DISCUSSION"."PRIORITY"[NUMBER,22], "DISCUSSION"."AUTO_NUMBER"[VARCHAR2,150], 
       "DISCUSSION"."CREATE_DATE"[DATE,7], "DISCUSSION"."CLOSE_DATE"[DATE,7], 
       "DISCUSSION"."CLOSE_COMMENTS"[VARCHAR2,4000], "DISCUSSION"."ORGANIZATION"[NUMBER,22], 
       "DISCUSSION"."TYPE"[NUMBER,22], "DISCUSSION"."CREATOR"[NUMBER,22], 
       "DISCUSSION"."SUBJECT"[VARCHAR2,4000], "DISCUSSION"."REPLIES"[NUMBER,22], 
       "DISCUSSION"."NOTIFYLIST"[VARCHAR2,4000], "DISCUSSION"."LAST_REPLY_DATE"[DATE,7], 
       "DISCUSSION"."DELETE_FLAG"[NUMBER,22], "DISCUSSION"."FLAGS"[VARCHAR2,32], 
       "DISCUSSION"."PREVIEW_TEXT"[NUMBER,22], "DISCUSSION"."FILTER"[NUMBER,22], 
       "DISCUSSION"."OBJVERSION"[NUMBER,22], "DISCUSSION"."CREATED"[DATE,7], 
       "DISCUSSION"."LAST_UPD"[DATE,7]

3- Using SQL Profile
--------------------
Plan hash value: 3458076016


---------------------------------------------------------------------------------------------------
| Id  | Operation                     | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |                   |     2 | 16686 |    11  (28)| 00:00:01 |
|*  1 |  CONNECT BY WITH FILTERING    |                   |       |       |            |          |
|*  2 |   TABLE ACCESS BY INDEX ROWID | DISCUSSION        |     1 |   240 |     3   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN          | DISCUSSION_PK     |     1 |       |     2   (0)| 00:00:01 |
|   4 |   NESTED LOOPS                |                   |     1 |   253 |     5   (0)| 00:00:01 |
|   5 |    CONNECT BY PUMP            |                   |       |       |            |          |
|   6 |    TABLE ACCESS BY INDEX ROWID| DISCUSSION        |     1 |   240 |     3   (0)| 00:00:01 |
|*  7 |     INDEX RANGE SCAN          | DISC_IDX_PARENTID |     1 |       |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1
   2 - SEL$4 / DISCUSSION@SEL$4
   3 - SEL$4 / DISCUSSION@SEL$4
   4 - SEL$3
   6 - SEL$3 / DISCUSSION@SEL$3
   7 - SEL$3 / DISCUSSION@SEL$3

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DISCUSSION"."PARENT_ID"=PRIOR NULL)
   2 - filter("PARENT_ID"=0)
   3 - access("ID"=6587)
   7 - access("connect$_by$_pump$_002"."prior Id"="PARENT_ID")

Here is query plan for SQL 2 when we have 300 thousands rows.

GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name   : staName64031
Tuning Task Owner  : CISCO
Tuning Task ID     : 55
Workload Type      : Single SQL Statement
Execution Count    : 1
Current Execution  : EXEC_45
Execution Type     : TUNE SQL
Scope              : COMPREHENSIVE
Time Limit(seconds): 1800
Completion Status  : COMPLETED
Started at         : 10/23/2011 23:30:22
Completed at       : 10/23/2011 23:30:26

-------------------------------------------------------------------------------
Schema Name: CISCO
SQL ID     : c741jfryv5m98
SQL Text   : SELECT * FROM (
                            SELECT * FROM discussion WHERE root_id = 6587 or
             id = 6587
                           )Start With (Parent_Id=0 And Id=6587) Connect By
             Prior Id=Parent_Id

-------------------------------------------------------------------------------
There are no recommendations to improve the statement.

-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------

1- Original
-----------
Plan hash value: 1202872009


-----------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |                 |     2 | 16686 |     6  (17)| 00:00:01 |
|*  1 |  CONNECT BY NO FILTERING WITH START-WITH|                 |       |       |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID           | DISCUSSION      |     2 |   480 |     5   (0)| 00:00:01 |
|   3 |    BITMAP CONVERSION TO ROWIDS          |                 |       |       |            |          |
|   4 |     BITMAP OR                           |                 |       |       |            |          |
|   5 |      BITMAP CONVERSION FROM ROWIDS      |                 |       |       |            |          |
|*  6 |       INDEX RANGE SCAN                  | DISCUSSION_PK   |       |       |     2   (0)| 00:00:01 |
|   7 |      BITMAP CONVERSION FROM ROWIDS      |                 |       |       |            |          |
|*  8 |       INDEX RANGE SCAN                  | DISC_IDX_ROOTID |       |       |     3   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1       
   2 - SEL$E029B2FF / DISCUSSION@SEL$5

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DISCUSSION"."PARENT_ID"=PRIOR NULL)
       filter("DISCUSSION"."PARENT_ID"=0 AND "DISCUSSION"."ID"=6587)
   6 - access("ID"=6587)
   8 - access("ROOT_ID"=6587)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "DISCUSSION"."PARENT_ID"[NUMBER,22], "DISCUSSION"."ID"[NUMBER,22], STRDEF[22], STRDEF[22], 
       STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[4000], STRDEF[22], STRDEF[22], STRDEF[150], STRDEF[7], 
       STRDEF[7], STRDEF[4000], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[4000], STRDEF[22], 
       STRDEF[4000], STRDEF[7], STRDEF[22], STRDEF[32], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[7], 
       STRDEF[7], PRIOR NULL[22], LEVEL[4]
   2 - "DISCUSSION".ROWID[ROWID,10], "ID"[NUMBER,22], "DISCUSSION"."CLASS"[NUMBER,22], 
       "DISCUSSION"."SUBCLASS"[NUMBER,22], "DISCUSSION"."PARENT_ID"[NUMBER,22], "ROOT_ID"[NUMBER,22], 
       "DISCUSSION"."MESSAGE"[VARCHAR2,4000], "DISCUSSION"."STATUS"[NUMBER,22], 
       "DISCUSSION"."PRIORITY"[NUMBER,22], "DISCUSSION"."AUTO_NUMBER"[VARCHAR2,150], 
       "DISCUSSION"."CREATE_DATE"[DATE,7], "DISCUSSION"."CLOSE_DATE"[DATE,7], 
       "DISCUSSION"."CLOSE_COMMENTS"[VARCHAR2,4000], "DISCUSSION"."ORGANIZATION"[NUMBER,22], 
       "DISCUSSION"."TYPE"[NUMBER,22], "DISCUSSION"."CREATOR"[NUMBER,22], 
       "DISCUSSION"."SUBJECT"[VARCHAR2,4000], "DISCUSSION"."REPLIES"[NUMBER,22], 
       "DISCUSSION"."NOTIFYLIST"[VARCHAR2,4000], "DISCUSSION"."LAST_REPLY_DATE"[DATE,7], 
       "DISCUSSION"."DELETE_FLAG"[NUMBER,22], "DISCUSSION"."FLAGS"[VARCHAR2,32], 
       "DISCUSSION"."PREVIEW_TEXT"[NUMBER,22], "DISCUSSION"."FILTER"[NUMBER,22], 
       "DISCUSSION"."OBJVERSION"[NUMBER,22], "DISCUSSION"."CREATED"[DATE,7], 
       "DISCUSSION"."LAST_UPD"[DATE,7]
   3 - "DISCUSSION".ROWID[ROWID,10]
   4 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 16116]
   5 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 16116]
   6 - "DISCUSSION".ROWID[ROWID,10]
   7 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 16116]
   8 - "DISCUSSION".ROWID[ROWID,10]

-------------------------------------------------------------------------------
  • 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-05-26T08:31:46+00:00Added an answer on May 26, 2026 at 8:31 am

    Do you just want an explanation of what you are seeing? Or suggestions to improve on this?

    Assuming the first:

    With SQL1 the database has to find the root element, then all the children, then the children of that and so on. It always has to work with the complete table since no general filter is available. It has process the whole table (or the complete index).

    With SQL2 the first thing the database system can do is to reduce the subset of rows it is working on.

    The difference is small (or even in the opposite directions) for small tables. If you full table fits in one or two blocks there isn’t much useful filtering the database can do. It has to pull the blocks into memory anyway. Even more if it uses index access. But with a huge table with (just making up numbers) 100 blocks reducing these to 5 be consulting 2 index blocks first is a huge gain.

    Update: Some ideas to tune this:

    • I’d try to get rid of the or in SQL2. This should be possible when a root entry has it’s own id as root_id

    • Assuming you actually need the level, which I think is provided by the hierarchical query, you could precalculate the level into a column of the table. Then you could drop the connect by clause, resulting in a real simple query. Of course if this works, depends a lot on the rest of your application.

    • Do you need all rows fast? Or the first rows? You might try providing the appropriate hint (http://download.oracle.com/docs/cd/B10501_01/server.920/a96533/hintsref.htm#4924)

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

Sidebar

Related Questions

How to tell django-tables2 which columns I would like to have in table? I
I have a table which contains a column discussion_duration where I am storing certain
I have table in which I am inserting rows for employee but next time
I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid
I have a table activity_types which contains the text for activities e.g. New project
I have a MySQL table for which I do very frequent SELECT x, y,
I have a stored procedure which creates a temporary table with found indexes by
I have a ASP:DropDown list which is populated by the following: public void Fill1()
The following jQuery function filters my table columns by letter. There is an <a>
We have a process updating the database which uses the following SQL IF NOT

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.