Fastest loop code/best way to get database results into array

This is a discussion on Fastest loop code/best way to get database results into array within the PHP General forums, part of the PHP Programming Forums category; I'm working on some code that deals with a LOT of results from a MySQL database and drops them ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-01-2003
Galen
 
Posts: n/a
Default Fastest loop code/best way to get database results into array

I'm working on some code that deals with a LOT of results from a MySQL
database and drops them into an array. It deals with about 17,200
results on a very fast box in about 0.5 seconds, which is not too bad,
until I realize I may need to call it on about that many results
several times in a page.

Is there any way to speed things up? Change to a different loop type?
Some other technique for pulling the data out of the database faster?
My only requirement is that the array format remain unchanged (yes,
associative values are important).

Here's the code, which seems awfully simple:

for($i=0; $i < $num_results; $i++)
{
$search_results[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
}
Reply With Quote
  #2 (permalink)  
Old 12-01-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Fastest loop code/best way to get database results intoarray

On Mon, 2003-12-01 at 03:06, Galen wrote:
> I'm working on some code that deals with a LOT of results from a MySQL
> database and drops them into an array. It deals with about 17,200
> results on a very fast box in about 0.5 seconds, which is not too bad,
> until I realize I may need to call it on about that many results
> several times in a page.
>
> Is there any way to speed things up? Change to a different loop type?
> Some other technique for pulling the data out of the database faster?
> My only requirement is that the array format remain unchanged (yes,
> associative values are important).
>
> Here's the code, which seems awfully simple:
>
> for($i=0; $i < $num_results; $i++)
> {
> $search_results[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
> }


You might squeeze a little more speed by using the following:

while( ($search_results[] = mysql_fetch_assoc( $result )) !== false );
array_pop( $search_results );

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #3 (permalink)  
Old 12-01-2003
Eugene Lee
 
Posts: n/a
Default Re: [PHP] Fastest loop code/best way to get database results into array

On Mon, Dec 01, 2003 at 03:24:35AM -0500, Robert Cummings wrote:
:
: On Mon, 2003-12-01 at 03:06, Galen wrote:
: >
: > I'm working on some code that deals with a LOT of results from a MySQL
: > database and drops them into an array. It deals with about 17,200
: > results on a very fast box in about 0.5 seconds, which is not too bad,
: > until I realize I may need to call it on about that many results
: > several times in a page.
: >
: > Is there any way to speed things up? Change to a different loop type?
: > Some other technique for pulling the data out of the database faster?
: > My only requirement is that the array format remain unchanged (yes,
: > associative values are important).
: >
: > Here's the code, which seems awfully simple:
: >
: > for($i=0; $i < $num_results; $i++)
: > {
: > $search_results[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
: > }
:
: You might squeeze a little more speed by using the following:
:
: while( ($search_results[] = mysql_fetch_assoc( $result )) !== false );
: array_pop( $search_results );

For large query results, you might consider using mysql_unbuffered_query()
and the while-loop instead of mysql_query() and the for-loop.

Otherwise, if the PHP code is optimal enough, the other obvious place is
to maybe optimize the query itself or that particular MySQL table.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0