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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:35:38+00:00 2026-05-27T21:35:38+00:00

I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),

  • 0

I have a table with the following fields:

id      VARCHAR(32) PRIMARY KEY,
parent  VARCHAR(32),
name    VARCHAR(32)

parent is a foreign key referencing the same table. This structure generates a tree. This tree is supposed to replicate a filesystem tree. The problem is that looking up an id from a path is slooow. Therefore I want to build an index. What is the best way of doing this?

Example Data:

   id          parent        name
--------    -----------    ----------
   1            NULL          root
   2             1            foo
   3             1            bar
   4             3            baz
   5             4            aii

Would Index To:

   id          parent        name
--------    -----------    ----------
   1            NULL          root
   2             1            root/foo
   3             1            root/bar
   4             3            root/bar/baz
   5             4            root/bar/baz/aii

I am currently thinking about using a temporary table and in the code manually running a series of insert from’s to build the index. (The reason that I make it temporary is that if this db is accessed from a windows system, the path needs backslashes whereas from *nix it needs forward slashes). Is there an alternative to this?

  • 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-27T21:35:39+00:00Added an answer on May 27, 2026 at 9:35 pm

    so, you have a function that does something like this (pseudocode):

    int getId(char **path) {
        int id = 0;
        int i;
        char query[1000];
    
        for (i = 0; path[i] != NULL; i++) {
            sprintf(query, "select id from table where parent = %d and name = '%s'", id, name);
            id = QuerySimple(query);
            if (id < 0)
               break;
        }
        return id;
    }
    

    looking at the query, you need a (non unique) index on columns (parent, name), but maybe you already have it.

    a (temporary) table can be used like you said, note that you can change the path separator in your program, avoiding the needed of different tables for windows and unix. you also need to keep the additional table in sync with the master. if updates/deletes/inserts are rare, instead of a table, you can simply keep an in-memory cache of already looked up data, and clear the cache when an update happens (you can also do partial deletes on the cache if you want). In this case you can also read more data (e.g. given a parent read all the children) to fill up the cache faster. On the extreme, you can read the entire table in memory and work there! it depends on how many data you have, your typical access patterns (how many reads, how many writes), and the deployment environment (do you have RAM to spare?).

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

Sidebar

Related Questions

I have a table (session_comments) with the following fields structure: student_id (foreign key to
Let's say I have a table that's like the following: name VARCHAR(50) PRIMARY KEY
I have a table with following structure : id, - Primary key minlatitude -
I have an SQL table with basically the following structure: PK (int, primary key),
I have a table with following fields id (int) name (varchar) dob (datetime) Now
I have a table called Employee with the following fields: EmpID Salary Name I
I have a table with a primary key that is a varchar(255). Some cases
I have a simple mysql table called messages with the following fields: ID(int) MESSAGE(varchar)
I have a simple table in mysql with the following fields: id -- Primary
I have a table with the following structure, FIELD TYPE EXTRA faciltiy_id int auto_increment(Primary

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.