Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL very slow loop

I made a very simple procedure (a while loop) in MySQL:

BEGIN

    DECLARE start INT;
    DECLARE ending INT;

    SET start = 1;
    SET ending = 4000000;

    WHILE (start <= ending) DO
        SET start = start + 1;
    END WHILE;
END

This takes ~26 seconds on a dual core machine (each core on 100%) and I don't understand why.

Could you give me an answer?

like image 963
recis Avatar asked Sep 28 '12 10:09

recis


1 Answers

Yes, Stored Procedures are very slow compared to other programming languages like C# or Java. Computational wise that is. The same code would be slow in SQL Server aswell. Probably faster than MySQL, but you'd still be waiting a few seconds untill it's fully executed. You'll be better of by doing that in a programming language, which completes such a task in under a second.

So with other words:

Slow stored procedure performance is one of the reasons why it usually isn’t wise to implement computationally expensive business logic in the database. With networks not being the bottleneck they once were it is often better to extract the data to the application layer and process it in the programming language of your choice, even if that takes a few extra round trips to the database.

like image 199
w00 Avatar answered Oct 12 '22 19:10

w00