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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:42:21+00:00 2026-05-14T00:42:21+00:00

I have a tree structure that can be n-levels deep, without restriction. That means

  • 0

I have a tree structure that can be n-levels deep, without restriction. That means that each node can have another n nodes.

What is the best way to retrieve a tree like that without issuing thousands of queries to the database?

I looked at a few other models, like flat table model, Preorder Tree Traversal Algorithm, and so.

Do you guys have any tips or suggestions of how to implement a efficient tree model? My objective in the real end is to have one or two queries that would spit the whole tree for me.

With enough processing i can display the tree in dot net, but that would be in client machine, so, not much of a big deal.

Oh just dropping some more info here:

environment: Oracle or PostgreSQL, C# and Winforms.

Thanks for the attention

  • 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-14T00:42:21+00:00Added an answer on May 14, 2026 at 12:42 am

    Assuming your only concern is selects and not inserts/updates/deletes, this depends on needs, such as:

    • do you need to know what level each node is at?
    • do you need to know if each node has any children while rendering it?
    • do you need to sort siblings by name?

    However, if there really are minimal changes to the tree being done, then it almost doesn’t matter what scheme you use, as you can do all the work in the application layer and cache the output.

    Edit:

    When order matters, I usually go for the materialized path method, and add an additional column SortPath. This way you can get your results sorted by sibling, which is a denormalization that makes rendering the tree in HTML extremely easy, as you can write out the entire tree (or any portion) in exactly the order you receive the records using a single query. This is optimal for speed, and is the easiest way to sort more than one level at a time.

    E.g.,

    CREATE TABLE [dbo].[MatPath](
        [ID] [int] NULL,
        [Name] [varchar](50) NULL,
        [Path] [varchar](max) NULL,
        [SortPath] [varchar](max) NULL
    ) 
    
    insert into MatPath (ID, Name, Path, SortPath) values (1, 'Animal', '1', 'Animal-1')
    insert into MatPath (ID, Name, Path, SortPath) values (2, 'Dog', '1.2', 'Animal-1|Dog-2')
    insert into MatPath (ID, Name, Path, SortPath) values (3, 'Horse', '1.3', 'Animal-1|Horse-3')
    insert into MatPath (ID, Name, Path, SortPath) values (4, 'Beagle', '1.2.4', 'Animal-1|Dog-2|Beagle-4')
    insert into MatPath (ID, Name, Path, SortPath) values (5, 'Abyssinian', '1.3.5', 'Animal-1|Horse-3|Abyssinian-5')
    insert into MatPath (ID, Name, Path, SortPath) values (6, 'Collie', '1.2.6', 'Animal-1|Dog-2|Collie-6')
    
    select * 
    from MatPath
    order by SortPath
    

    Output:

    ID     Name            Path        SortPath
    ------ --------------- ----------- --------------------------------
    1      Animal          1           Animal-1
    2      Dog             1.2         Animal-1|Dog-2
    4      Beagle          1.2.4       Animal-1|Dog-2|Beagle-4
    6      Collie          1.2.6       Animal-1|Dog-2|Collie-6
    3      Horse           1.3         Animal-1|Horse-3
    5      Abyssinian      1.3.5       Animal-1|Horse-3|Abyssinian-5
    
    (6 row(s) affected)
    

    You can determine the level of each node by counting pipes (or periods) in the application layer, or in SQL using whatever built-int or custom functions that can count occurrences of a string.

    Also, you’ll notice I append the ID to the Name when building SortPath. This is to ensure that two sibling nodes with the same name will always get returned in the same order.

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

Sidebar

Related Questions

I am having a tree data structure that can have different types of nodes
I have a tree structure where each Node has a parent and a Set<Node>
I have category model that has a tree structure. In my database I have
For our CMS we have a site manager that defines the site's tree structure
I have a deep tree structure built with lists, containing lists and floats. I
In Lua, I have a tree relationship structure between objects where an object can
I have a category/file tree structure. Both categories and files can have parents, so
lets say i have a tree of the following structure: <div>node level1 <div>node level2
I have this python code that will traverse a tree structure. I am trying
I have a tree like structure of Categories, the leaf Nodes in the tree

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.