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

  • Home
  • SEARCH
  • 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 4608854
In Process

The Archive Base Latest Questions

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

I have a stored procedure which deals with adding nodes in a tree. Basically

  • 0

I have a stored procedure which deals with adding nodes in a tree. Basically the table structure is

id INT PRIMARY
label VARCHAR(1) /* the value of the node which is a character */
parent_id INT /* id of the parent node */

Here is my stored procedure:

/* 
    takes a word, and adds every character in to the table 
    where every character is a child of the previous character
    the first character of every word is a child of the root
*/
CREATE PROCEDURE rule(IN word VARCHAR(255))
BEGIN
    /* (parent_id = 0) => child of root */
    DECLARE pid INT DEFAULT 0; /* parent id */
    DECLARE npid INT DEFAULT 0;
    DECLARE strlength INT;
    DECLARE j INT DEFAULT 1; 
    DECLARE query_count INT DEFAULT 0;
    DECLARE active_char VARCHAR(1);
    SET strlength = LENGTH(word);
    /* loop through the word */
    WHILE j <= strlength DO
        /* get a single character from word */
        SET active_char = SUBSTR(word,j,1);
        /* if the character doesn't already exist, insert it */
        SELECT COUNT(*) INTO query_count FROM tree
        WHERE parent_id = pid AND label = active_char;
        IF (query_count = 0) THEN
            INSERT INTO tree (label, parent_id)
            VALUES (active_char,pid);
        END IF;
        /* Set the new parent id */
        SELECT id INTO npid FROM tree
        WHERE label = active_char AND parent_id = pid;
        SET pid = npid;
        SET j = j + 1;
    END WHILE;
END //

I’m sure there are a few tweaks I can make to make the procedure a bit more efficient but I can’t think of anything that would significantly reduce the time needed.

I’m dealing with a lot of words which means this procedure is run a couple 100,000 times which in turn means a lot of inserts and a lot of queries. It takes hours perhaps days(not sure because I gave up waiting and stopped the process).

The thing is, I don’t think I can do a bulk insert because every insert depends on a previous insert.

I was wondering if there’s some way to create a virtual table that’s stored in main memory to do all these operations fast and then just save the result in to the actual table.

At the moment, the only possible solution I can think of is to build the tree in PHP and then do a bulk insert. I think this should be faster but I’m not sure up to what degree.

Any help would be really appreciated.

Thanks.

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

    Whilst I’m afraid I can’t claim to have used it myself, passing the data down to the stored procedure as XML and processing it as described here would seem a reasonable approach. N.B. MySQL 5.1 or higher.

    • 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 looks like CREATE PROCEDURE Update-all ( @RowID INT,
I have a stored procedure which returns a Dataset(Table) . How can I use
I have created one stored procedure which runs on 5000 users in tbluser table
I have an Informix stored procedure which takes an int and a smallint as
I have a Stored procedure which schedules a job. This Job takes a lot
Hai Techies, I have some stored procedure which was written in SQL server.Now i
I have a Stored Procedure called spGetOrders which accepts a few parameters: @startdate and
I have an MSSQL2005 stored procedure here, which is supposed to take an XML
Which command will executed first,If a stored procedure have individual multiple select commands;
I have a problem with an application which uses the same stored procedure over

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.