While working on a script (in Perl), I discovered that MySQL performs very differently depending on the method used to retrieve a data row. What I need to do is walk through a set of 5000+ rows of a table with 110 columns. I have tried four different versions, and instead of elaborating and discussing, I will simply write down the method used and the execution time:
- Using a for(my $i = 0; $i < NUM; $i++)-loop and selecting every row individually with: SELECT * FROM table WHERE id = '$i'
85 seconds. Constantly 64 queries per second.
- Using a for(my $i = 0; $i < NUM; $i++)-loop and selecting every row individually with: SELECT * FROM table LIMIT $i, 1
45 seconds. During the first 5 seconds, MySQL performed at 313 queries per second, the last 5 seconds averaged at 69 queries per second after a logarithmic decrease.
- Using a for(my $i = 0; $i < NUM; $i++)-loop after creating a temporary table with CREATE TEMPORARY TABLE temp SELECT * FROM table and selecting every row individually with: SELECT * FROM temp LIMIT 1. After that select, the first row was deleted using DELETE QUICK FROM temp LIMIT 1
17 seconds.
- Using a simple SELECT * FROM table and loop-fetching each row using while(my $sample = $qry->fetchrow_hashref())
5 seconds.
There's not much to be said about this but: Use the simplest method MySQL provides. :)
Rafael: Thank you very much, I was having a huge headache to solve the very same problem!