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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:21:36+00:00 2026-06-16T02:21:36+00:00

I have nested set table (MySQL) with about 1.5 million records in it. The

  • 0

I have nested set table (MySQL) with about 1.5 million records in it.
The table structure is:

id | name | root_id | lft | rgt | level

I need to get strings, containing names of all parent records plus record name, let’s call it ‘full_name’.
For example, it’ll be

"United States, California, Los Angeles"

for “Los Angeles” record.

I may query all names for ONE record with:

SELECT parent.name
FROM location AS node,
     location AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND parent.root_id=node.root_id AND node.id=:id
ORDER BY parent.level

and to build full_name using implode()

But this request works for one record only, working too slowly for several calls.
So now I want to add ‘full_name’ to Sphinx index (or probably to MySQL directly).

QUESTIONS:

  • Is it possible to construct such sql query that will select ‘full_name’ for each record to put it to Sphinx index? I think this would be ideal solution for such situation.

  • I also tried to update MySQL table adding ‘full_name’ field, but my update requests will take several days to finish the job. Is it good idea at all? Is it possible to make this update fast enough?

  • I’m thinking, maybe I should move to PostgreSQL and use hierarchical requests (I have no experience with this database so can’t be sure) ?

Thank you

  • 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-06-16T02:21:38+00:00Added an answer on June 16, 2026 at 2:21 am

    When you say you are updating your table to add the full_name field, do you mean you are running the above query for every single record individually?

    Really it shouldnt be that slow, do you have proper indexes on the table?

    Regardless, you can make it even quicker, by only running one actual query, to get all records. The function here:
    http://www.sitepoint.com/hierarchical-data-database-2/

    Can easily be modified, to update all recrds. Just rather than printing out an indented list, each iteration of the loop should run an update statement. As well as maintaining the $right array, need to maintain a list of strings. Where add/remove to right add you another array at the same time.

    // start with an empty $right stack
    $right = array();
    $names = array();
    
    // now, retrieve whole tree
    $result = mysql_query('SELECT id, name, full_name, lft, rgt FROM location ORDER BY lft ASC');
    
    // update each row
    while ($row = mysql_fetch_array($result)) {
        // only check stack if there is one
        if (count($right)>0) {
            // check if we should remove a node from the stack
            while ($right[count($right)-1]<$row['rgt']) {
                array_pop($right);
                array_pop($names);
            }
        }
    
        // add this node to the stack
        $right[] = $row['rgt'];
        $names[] = $row['name'];
    
        if (empty($row['full_name'])) {
            mysql_query("UPDATE location SET full_name='".mysql_real_escape_string(implode(', ',$names))."' WHERE id=".intval($row['id']));
        }
    }
    

    And if that is still slow, rather than updating the original table, insert the full_name (along with the id) into a new table – that doesnt have any indexes. Alter the table to add index on the id column, then run a multi-table update, to update the main table from the temporally table. This will be quicker, because its not updating the main indexes piecemeal, its doing it on one big batch.

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

Sidebar

Related Questions

I have a large mysql table with parent-child relationships stored in the nested set
I have a mysql table (articles) with a nested index (blog_id, published), and performs
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I've got a MySQL table that acts like a nested set in order to
I have a table in my database which conains a nested set of items,
I have hierarchical data (categories) stored in a mySql database using the nested set
Is it possible to have nested set capabilities in this somewhat custom setup? Consider
I have your run of the mill nested set hierarchy type setup with the
I have a select input: f.select :category_id, nested_set_options(Category, @categories) {|i| #{'-' * i.level} #{i.name}
I'd like to have the nested containers inherit that property, but when I set

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.