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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:22:40+00:00 2026-05-24T11:22:40+00:00

I am having some difficulty getting a pretty simple stored procedure right. Consider the

  • 0

I am having some difficulty getting a pretty simple stored procedure right.
Consider the following article table snippet:

id    replaced_by     baseID
 1              2          0
 2              3          0
 3              0          0

A simple hierarchical table, using copy-on-write. When an article is edited, the replaced_by field of the current article is set to the id of it’s new copy.

I’ve added a baseID field, which in the future should store the baseID of an article.
In my example above, there is one article (eg id 3). It’s baseID would be 1.

To get the baseID, I have created the following stored procedure:

DELIMITER $$

CREATE FUNCTION getBaseID(articleID INT) RETURNS INT
BEGIN
    DECLARE x INT;
    DECLARE y INT;
    SET x = articleID;
    sloop:LOOP
        SELECT id INTO y FROM article WHERE replaced_by_articleID = x;
        IF y IS NOT NULL THEN
            SET x = y;
            ITERATE sloop;
        ELSE
            LEAVE sloop;
        END IF;  
    END LOOP;
    RETURN x;
END $$

DELIMITER ;

It seems simple enough, until I actually call the function using:

SELECT getBaseID(3);

I would expect, the function to return 1. I’m even willing to understand it can take a slice of a second.
Instead, the machine’s CPU goes up to 100% (mysqld).

I have even rewritten the same function using REPEAT .. UNTIL and with WHILE .. DO, with the same end result.

Can anyone explain why my CPU goes up 100% when it enters the loop?

Side note: I am trying to simply win time. I have created the exact same function in PHP, which performs okay, but our guess is that MySQL can do it slightly faster. We need to sift through about 18 million records. Any bit of time I can save is going to be worth it.

Thanks in advance for any assistance and/or pointers.


Solved SQL:

DELIMITER $$

CREATE FUNCTION getBaseID(articleID INT) RETURNS INT
BEGIN
    DECLARE x INT;
    DECLARE y INT;
    SET x = articleID;
    sloop:LOOP
        SET y = NULL;
        SELECT id INTO y FROM article WHERE replaced_by_articleID = x;
        IF y IS NULL THEN
            LEAVE sloop;
        END IF;  
        SET x = y;
        ITERATE sloop;
    END LOOP;
    RETURN x;
END $$

DELIMITER ;
  • 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-24T11:22:41+00:00Added an answer on May 24, 2026 at 11:22 am

    From mysql :

    If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged

    So you have an infinite loop when no records found with a given x (y remains unchanged)
    Try SET y = (SELECT id ....) instead or add SET y = null before your select statement (it should be the first statement in the loop)

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

Sidebar

Related Questions

I am writing a GAE application and am having some difficulty with the following
I have the following web config file. I am having some difficulty in retrieving
I'm having some difficulty getting this script to execute properly. The create_rss function does
I'm having some difficulty getting my cross domain policy to work. I have an
Am having some difficulty getting an Image from a url, and then displaying it
I'm following a tutorial on php, and am having difficulty getting this to work.
I'm having some difficulty getting my django tests to run properly; I'm using nose,
I am having some difficulty getting the values of two text inputs: <form action=search.php>
In one of my unit tests, I am having some difficulty getting a mocked
I'm having some rather extreme difficulty getting HDF5 1.8.7 to build from source in

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.