tags:

views:

104

answers:

2

Is there a performance difference between using mysql_result and mysql_array_assoc to loop through a large SQL select result?

A: 

Yes (fetching an entire row is faster than mysql_result), and it's mentioned clearly in the mysql_result documentation:

When working on large result sets, you should consider using one of the functions that fetch an entire row (mysql_fetch_*). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result()

Nicolás
+2  A: 

From the mysql_result() manual page:

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

So it seems that mysql_array_assoc(), one of the functions that quote refers to, is going to be your best bet.

zombat
Any idea as to how long the result needs to be before the performance difference is notable?
Brian
Nope, you'd have to do some benchmarking. But if you're looping through multiple records, I would just use mysql_array_assoc() and check the fields on the code side, rather than trying to pick out individual cells. If you only need certain columns, make sure you're selecting only those and not doing `SELECT *`, as that cuts down on resources that MySQL needs too.
zombat