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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:12:41+00:00 2026-06-12T00:12:41+00:00

I have a stored procedure that opens a CURSOR on a select statement that

  • 0

I have a stored procedure that opens a CURSOR on a select statement that iterates over a table of 15M rows (This table is a simpl import of a large CSV).

I need to normalize that data by inserting various pieces of each row into 3 different tables (capture auto-update ID’s, use them in forein key constraints, and such).

So I wrote a simple stored procedure, open CURSOR, FETCH the fields into varialbes and do the 3 insert statements.

I’m on a small DB server, default mysql installation (1 cpu, 1.7GB ram), I had hoped for a few hours for this task. I’m at 24 hours+ and top shows 85% wasted CPU.

I think I have some kind of terrible inefficiency. Any ideas on improving the efficiency of the task? Or just determining where the bottleneck is?


root@devapp1:/mnt/david_tmp# vmstat 10
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  1    256  13992  36888 1466584    0    0     9    61    1    1  0  0 98  1
 1  2    256  15216  35800 1466312    0    0    57  7282  416  847  2  1 12 85
 0  1    256  14720  35984 1466768    0    0    42  6154  387  811  2  1 10 87
 0  1    256  13736  36160 1467344    0    0    51  6979  439  934  2  1  9 89

DROP PROCEDURE IF EXISTS InsertItemData;

DELIMITER $$
CREATE PROCEDURE InsertItemData() BEGIN 
    DECLARE spd TEXT;
    DECLARE lpd TEXT;
    DECLARE pid INT;
    DECLARE iurl TEXT;

    DECLARE last_id INT UNSIGNED;
    DECLARE done INT DEFAULT FALSE;

    DECLARE raw CURSOR FOR select t.shortProductDescription, t.longProductDescription, t.productID, t.productImageURL 
                           from frugg.temp_input t;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    OPEN raw;

    read_loop: LOOP
        FETCH raw INTO spd, lpd, pid, iurl;

        IF done THEN
            LEAVE read_loop;
        END IF;

        INSERT INTO item (short_description, long_description) VALUES (spd, lpd);
        SET last_id = LAST_INSERT_ID();
        INSERT INTO item_catalog_map (catalog_id, catalog_unique_item_id, item_id) VALUES (1, CAST(pid AS CHAR), last_id);
        INSERT INTO item_images (item_id, original_url) VALUES (last_id, iurl);
    END LOOP;

    CLOSE raw;
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-06-12T00:12:42+00:00Added an answer on June 12, 2026 at 12:12 am

    MySQL will almost always perform better executing straight SQL statements, than looping inside a stored procudure.

    That said, if you are using InnoDB tables, your procedure will run faster inside a START TRANSACTION / COMMIT block.

    Even better would be to add an AUTO_INCREMENT to the records in frugg.temp_input, and querying against that table:

    DROP TABLE IF EXISTS temp_input2;
    
    CREATE TABLE temp_input2 (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        shortProductDescription TEXT, 
        longProductDescription TEXT,
        productID INT,
        productImageURL TEXT,
        PRIMARY KEY (id)
    );
    
    START TRANSACTION;
    
    INSERT INTO 
        temp_input2
    SELECT
        NULL AS id,
        shortProductDescription, 
        longProductDescription,
        productID,
        productImageURL
    FROM
        frugg.temp_input;
    
    INSERT 
        INTO item 
    (
        id, 
        short_description, 
        long_description
    ) 
    SELECT 
        id,
        shortProductDescription AS short_description, 
        longProductDescription AS long_description
    FROM
        temp_input2
    ORDER BY
        id;
    
    INSERT INTO 
        item_catalog_map
    (
        catalog_id, 
        catalog_unique_item_id, 
        item_id
    )
    SELECT 
        1 AS catalog_id,
        CAST(productID AS CHAR) AS catalog_unique_item_id,
        id AS item_id
    FROM
        temp_input2
    ORDER BY
        id;
    
    INSERT INTO 
        item_images 
    (
        item_id, 
        original_url
    ) 
    SELECT 
        id AS item_id,
        productImageURL AS original_url
    FROM
        temp_input2
    ORDER BY
        id;
    
    COMMIT;
    

    Even better than the above, is before loading the .CSV file into frugg.temp_input, you add an AUTO_INCREMENT field to it, saving you the extra step of creating/loading temp_input2 shown above.

    • 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 that has this line: SET @SQL = 'SELECT path,title,tags
I have a stored procedure that uses a FAST_FORWARD cursor to chronologically loop over
I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure that logs some data, how can I call this
I have a SQL Server 2008 stored procedure that updates values in a table.
I have a stored procedure call that goes like this: using (OracleConnection con =
I have created a Stored Procedure, that looks like this: DELIMITER €€ CREATE PROCEDURE
I have a stored procedure, which contains a simple select statement: ALTER PROCEDURE [dbo].[TransferAuditRecords]
I have a stored procedure made in Oracle 9g that returns a cursor with
I have created a stored procedure for selecting value from the table if that

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.