php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52127 mysl_fetch_assoc is much slower than mysql_fetch_row
Submitted: 2010-06-19 18:13 UTC Modified: 2010-07-28 02:38 UTC
From: skyeye at o2 dot pl Assigned: preinheimer (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.2 OS: CentOS
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: skyeye at o2 dot pl
New email:
PHP Version: OS:

 

 [2010-06-19 18:13 UTC] skyeye at o2 dot pl
Description:
------------
"Note: Performance
An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. "

I compared mysql_fetch_assoc() and mysql_fetch_row(). These results show that there is a huge difference between these functions.

Times for fetching 90000 records (there are 42 columns):

1. mysql_fetch_assoc() + array

2.20659708977 [s].

2. mysql_fetch_row + array

1.21593475342E-5 [s].

3. mysql_fetch_row + list()

2.40802764893E-5 [s].

4. mysql_fetch_row() + foreach()

4.19616699219E-5 [s].

5. mysql_fetch_object()

5.91278076172E-5 [s].

Times for fetching 50 records (there are 42 columns):

1. mysql_fetch_assoc() + array

0.00150012969971 [s].

2. mysql_fetch_row + array

1.09672546387E-5 [s].

3. mysql_fetch_row + list()

2.78949737549E-5 [s].

4. mysql_fetch_row() + foreach()

4.19616699219E-5 [s].

5. mysql_fetch_object()

5.50746917725E-5 [s].

Times for fetching 1 record (there are 42 columns):

1. mysql_fetch_assoc() + array

0.000169038772583 [s].

2. mysql_fetch_row + array

9.77516174316E-6 [s].

3. mysql_fetch_row + list()

3.50475311279E-5 [s].

4. mysql_fetch_row() + foreach()

4.91142272949E-5 [s].

5. mysql_fetch_object()

6.29425048828E-5 [s].

Times for fetching 90000 records (there is 1 column):

1. mysql_fetch_assoc() + array

0.10712313652 [s].

2. mysql_fetch_row + array

1.4066696167E-5 [s].

3. mysql_fetch_row + list()

4.88758087158E-5 [s].

4. mysql_fetch_row() + foreach()

6.98566436768E-5 [s].

5. mysql_fetch_object()

8.98838043213E-5 [s].

Times for fetching records (there is 1 column):

1. mysql_fetch_assoc() + array

0.000155925750732 [s].

2. mysql_fetch_row + array

1.09672546387E-5 [s].

3. mysql_fetch_row + list()

4.29153442383E-5 [s].

4. mysql_fetch_row() + foreach()

6.29425048828E-5 [s].

5. mysql_fetch_object()

8.01086425781E-5 [s].

Times for fetching 1 records (there is 1 column):

1. mysql_fetch_assoc() + array

0.000102996826172 [s].

2. mysql_fetch_row + array

1.31130218506E-5 [s].

3. mysql_fetch_row + list()

4.29153442383E-5 [s].

4. mysql_fetch_row() + foreach()

5.79357147217E-5 [s].

5. mysql_fetch_object()

7.20024108887E-5 [s].



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-21 02:13 UTC] philip@php.net
-Status: Open +Status: Feedback
 [2010-06-21 02:13 UTC] philip@php.net
Benchmarked with what code?
 [2010-06-21 22:33 UTC] skyeye at o2 dot pl
-Status: Feedback +Status: Open
 [2010-06-21 22:33 UTC] skyeye at o2 dot pl
With this code (please test it for any database that has large number of records and columns):

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

echo "<font color='green'>1. mysql_fetch_assoc() + array</b></font>";

$link = mysql_connect(HOST,USER,PASS);
mysql_select_db(DB,$link);

$result = mysql_query("SELECT ... FROM ... ORDER BY '...' DESC LIMIT 300");

mysql_close($link);

$start = microtime_float();

while($tab1 = mysql_fetch_assoc($result))
{
}

$stop = microtime_float();

echo "<BR><BR>".($stop - $start)." [s].";

echo "<BR><BR><font color='green'>2. mysql_fetch_row + array</b></font>";

$start = microtime_float();

while($tab2 = mysql_fetch_row($result))
{
}

$stop = microtime_float();

echo "<BR><BR>".($stop - $start)." [s].";

echo "<BR><BR><font color='green'>3.  mysql_fetch_row + list()</b></font>";

$start = microtime_float();

while(list([variables for all 42 columns]) = mysql_fetch_row($result))
{
}

$stop = microtime_float();

echo "<BR><BR>".($stop - $start)." [s].";

echo "<BR><BR><font color='green'>4. mysql_fetch_row() + foreach()</b></font>";

while($info = mysql_fetch_row($result))
{
	foreach($info as $field);
}

$stop = microtime_float();


echo "<BR><BR>".($stop - $start)." [s].";

echo "<BR><BR><font color='green'>5. mysql_fetch_object()</b></font>";

while($object = mysql_fetch_row($result))
{
}

$stop = microtime_float();

echo "<BR><BR>".($stop - $start)." [s].";
 [2010-06-21 22:40 UTC] skyeye at o2 dot pl
Obviusly there should be:

while($object = mysql_fetch_object($result))
{
}

instead of:

while($object = mysql_fetch_row($result))
{
}

but it is not so important as we mainly compare mysql_fetch_assoc() with mysql_fetch_row().
 [2010-07-28 01:50 UTC] preinheimer@php.net
-Assigned To: +Assigned To: preinheimer
 [2010-07-28 02:18 UTC] tocker at gmail dot com
I don't think the docs should be changed.  This is worst-case analysis, and  performance is only measurably different because the reporter is extracting too many rows.  In the typical case I would never recommend someone grab greater than 100 in one go, and in that case the round trip cost is much more visible as being a large part of the cost.

If there's any docs correction to be made - it should be that 90K rows at a time is excessive, and not a best practice.
 [2010-07-28 02:38 UTC] preinheimer@php.net
-Status: Assigned +Status: Closed
 [2010-07-28 02:38 UTC] preinheimer@php.net
Hi,

I've replicated your test case by using the World.sql file provided by MySQL.
http://dev.mysql.com/doc/world-setup/en/world-setup.html

There's an issue with the way your tests are run, in that whichever one runs 
first always runs slowest.

So I'll get:
fetch row: 0.0063321590423584
fetch asc: 4.0531158447266E-6

Then:

fetch asc: 0.0053088665008545
fetch row: 3.0994415283203E-6

When I reverse the order. This is likely the result of some processing occurring 
when the data is pulled out of the result set.



paul
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 09:00:02 2026 UTC