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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:47:29+00:00 2026-06-08T05:47:29+00:00

I create a Mysql procedure using cursor, but it’s run too slow… It’s get

  • 0

I create a Mysql procedure using cursor, but it’s run too slow… It’s get between 40 and 60 lines by second.. See:

DELIMITER $$
CREATE PROCEDURE sp_create(IN v_idsorteio INT,OUT afetados INT)
  BEGIN
        DECLARE done INT default 0;
        DECLARE vc_idsocio INT;
        DECLARE z INT;
        DECLARE cur1 CURSOR FOR select IdSocio from socios where Sorteio=1  and Finalizado='S' and CodClientes IS NOT NULL;
        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
        SET z=1;
        OPEN cur1;
        FETCH cur1 INTO vc_idsocio;
        WHILE done=0 DO
            -- SELECT register as t; 
            insert INTO socios_numeros_sorteio (IdSocio,IdSorteio,NumerodeSorteio) VALUES (vc_idsocio,v_idsorteio,z);
            FETCH cur1 INTO vc_idsocio;
            SET z = z+1;
        END WHILE;
        CLOSE cur1;
        Select z-1 as total INTO afetados;
  END$$
DELIMITER ;

how can I to improve that?

  • 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-08T05:47:32+00:00Added an answer on June 8, 2026 at 5:47 am

    This is slow because you are looping through a resultset, row by row, and performing individual insert statements for each row returned. That’s why it’s gonna be slow.

    Let’s briefly summarize what you are doing. First, you are running a query:

    select IdSocio
      from socios
     where Sorteio=1
       and Finalizado='S'
       and CodClientes IS NOT NULL;
    

    (Apparently the order these rows are returned in is not important.)

    Then for each row returned from that query, you want to insert a row into another table.

    insert INTO socios_numeros_sorteio
    (IdSocio
    ,IdSorteio
    ,NumerodeSorteio
    ) VALUES
    (vc_idsocio
    ,v_idsorteio
    ,z);
    

    The value for the first column is coming from a value returned by the query.
    The value for the second column is being assigned a value passed as an argument to the procedure.
    And the value for the third column is from a counter that starts at 1 and is being incremented by 1 for each row.

    MySQL is optimized to perform an operation like this. But it’s NOT optimized to do this using a stored procedure that loops through a cursor row by row.

    If you are looking to get some reasonable performance, you need to SIGNIFICANTLY REDUCE the number of individual INSERT statements you run, and instead think in terms of processing data in “sets” rather than individual rows. One approach is batch the rows up into “extended insert” statements, which can insert multiple rows at a time. (The number rows you can insert in one statement is effectively limited by max_allowed_packet.)

    That approach will significantly improve performance, but it doesn’t avoid the overhead of the cursor, fetching each row into procedure variables.

    Something like this (in the body of your procedure) is likely to perform much, much better, because it takes the result set from your select and inserts all of the rows into the destination table in one fell swoop, without bothering to mess with updating the values of variables in the procedure.

    BEGIN
    
    SET @idsorteio = v_idsorteio;
    
    INSERT INTO socios_numeros_sorteio
    ( IdSocio
    , IdSorteio
    , NumerodeSorteio
    )
    SELECT s.IdSocio   AS IdSocio
         , @idsorteio  AS IdSorteio
         , @z := @z+1  AS NumerodeSorteio
      FROM socios s
      JOIN (SELECT @z := 0) z
     WHERE s.Sorteio=1
       AND s.Finalizado='S'
       AND s.CodClientes IS NOT NULL;
    
    SELECT ROW_NUMBER() INTO afetados;
    
    END$$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a procedure which extracts data from a MySQL server (using
I'm using adodb5 to call this stored procedure but MySQL doesn't understand $sealID is
I'm using Coldfusion8 and trying to get a simple stored procedure to run a
Can I create tables using Stored Procedures? I am using MySQL. I had searched
I use the following mysql procedure, DROP PROCEDURE IF EXISTS `allied`.`GetRegistrationData`$$ CREATE DEFINER=`allied`@`%` PROCEDURE
I use the following mysql query, DELIMITER $$ DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$ CREATE
i am writing stored procedures in MySQL that return values; CREATE PROCEDURE getCustomerById (id
Is there a way to create mysql triggers using Activerecord migrations ? Has anybody
I'm trying to create a mysql table from the inner join between two other
I am using the following Code to execute the SP of MySql and get

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.