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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:11:44+00:00 2026-05-25T11:11:44+00:00

I have a table which is basically a tree structure with a column parent_id

  • 0

I have a table which is basically a tree structure with a column parent_id and id.

parent_id is null for root nodes.

There is also a self referential foreign key, so that every parent_id has a corresponding id.

This table is mainly read-only with mostly infrequent batch updates.

One of the most common queries from the application which accesses this table is select ... where parent_id = X. I thought this might be faster if this table was index organised on parent_id.

However, I’m not sure how to index organise this table if parent_id can be null. I’d rather not fudge things so that parent_id=0 is some special id, as I’d have to add dummy values to the table to ensure the foreign key constraints are satisfied, and it also changes the application logic.

Is there any way to index organise a table by possible null value columns?

  • 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-25T11:11:44+00:00Added an answer on May 25, 2026 at 11:11 am

    Solution from question asker:

    I found I could get the same benefits from index organisation just by adding the queried columns to the end of the parent_id index, i.e. instead of:

    create index foo_idx on foo_tab(parent_id);
    

    I do:

    create index foo_idx on foo_tab(parent_id, col1, col2, col3);
    

    Where col1, col2, col3 etc are frequently accessed columns.

    I’ve only done this with indexes which are used to return multiple rows which benefit from the ordering and hence disk locality provided by the index, instead of having to jump around the table. Indexes which are generally used to return single rows I’ve left to reference the table, as there is only one row to read anyway so locality matters much less.

    Like I mentioned, this is a mainly read table, and also space is not a huge concern, so I don’t think the overhead to writes caused by these indexes is a big concern.

    (I realise this won’t index null parent_ids, but instead I’ve made another index on decode(parent_id, null, 1, null) which indexes nulls and only nulls).

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

Sidebar

Related Questions

I have a stored procedure which deals with adding nodes in a tree. Basically
Basically I have a table which is used to hold employee work wear details.
Can we have a SQL query which will basically help in viewing table and
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------
I have a MySQL table which basically serves as a file index. The primary
I have an outings table, which basically holds some information about an 'outing'. These
I have a column in a table which needs to be replaced with a
I have a table like the following which is basically used to give a
I have an Oracle table which has a name,value,time columns.Basically the table is for

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.