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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:05:16+00:00 2026-05-22T03:05:16+00:00

I need to have a 5 levels hierarchy for the users registered to a

  • 0

I need to have a 5 levels hierarchy for the users registered to a website. Every user is invited by another, and I need to know all descendants for a user. And also ancestors for a user.

I have in mind 2 solution.

  1. Keeping a table with relationships this way. A closure table:

    ancestor_id  descendant_id  distance
    1            1              0
    2            2              0
    3            3              0
    4            4              0
    5            5              0
    6            6              0
    2            3              1
  1. Having this table for relationships. Keeping in a table 5 levels ancestors. A “ancestors” table:

   user_id ancestor_level1_id ancestor_level2_id ancestor_level3_id ancestor_level4_id ancestor_level5_id
   10      9                  7                  4                  3                  2
   9       7                  4                  3                  2                  1

Are these good ideas?

I know about “the adjacency list model” and “the modified preorder tree traversal algorithm”, but are these good solutions for a “referral” system?

The queries that I need to perform on this tree are:

  • frequently adding a new users
  • when a user buys something, their referrers get a percentage commission
  • every user should be able to find out how many people they’ve referred (and how many people were referred by people who they referred….) at each level
  • 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-22T03:05:17+00:00Added an answer on May 22, 2026 at 3:05 am

    Closure Table

    ancestor_id  descendant_id  distance
        1            1              0
        2            2              0
        3            3              0
        4            4              0
        5            5              0
        6            6              0
        2            3              1
    

    To add user 10, referred by user 3. (I don’t think you need to lock the table between these two insertions):

    insert into ancestor_table
    select ancestor_id, 10, distance+1
    from ancestor_table
    where descendant_id=3;
    
    insert into ancestor_table values (10,10,0);
    

    To find all users referred by user 3.

    select descendant_id from ancestor_table where ancestor_id=3;
    

    To count those users by depth:

    select distance, count(*) from ancestor_table where ancestor_id=3 group by distance;
    

    To find the ancestors of user 10.

    select ancestor_id, distance from ancestor_table where descendant_id=10;
    

    The drawback to this method is amount of storage space this table will take.

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

Sidebar

Related Questions

I have list of users. There is hierarchy defined by field ParentId for each
I need to manage hierarchy data storing in my database. But I have a
I have a 2 level nesting objects that i need help with My routes
Developing a project of mine I realize I have a need for some level
I have no experience with low level programing and I need this piece of
I have need to produce LINE, BAR, and PIE charts in Rails. I have
I have need to return multiple results from a subquery and have been unable
I have need to pack four signed bytes into 32-bit integral type. this is
I have need for a function pointer that takes two arguments and returns a
I have need for AutoCompliteTextView in PreferenceActivity so I extended DialogPreference. My auto-complite is

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.