Thread: pg_fetch_array

pg_fetch_array

From
"Thanks"
Date:
Hello....

I would like to know the performance of pg_fetch_array.  Cosider the code:

$query = "select * from foo";
$result = pg_query( $db, $query );

while ($row = pg_fetch_array($result))
{
   $a = $row["a"];
   $b = $row["b"];
   $c = $row["c"];
   $d = $row["d"];
}

Does php need to read database everytime when pg_fetch_array is executed in
the while loop or all the rows have been in the memory after pg_query?

If read database is needed, is there any method to copy all the rows into
memory by using other command? (because I have a application which needs
large amount database update/retrieval and I wish the performance of the
overall applications run faster.)

or other method you would like to recommend in order to make the faster
response time?

Thank you in advance.



Re: pg_fetch_array

From
Rod Taylor
Date:
> Does php need to read database everytime when pg_fetch_array is executed in
> the while loop or all the rows have been in the memory after pg_query?

You may need to ask the php people about this one. The PostgreSQL
protocol would allow data to continue streaming in at the same time as
you are processing other rows (asynchronous retrieval). So, optionally
they may fetch and cache all rows in local memory at pg_query OR grab
them in sets of 1000 rows and cache that (fetching the next set when the
first set runs out) OR grab one row for each fetch.

You could run a simple select that will fetch 100M rows from a table
with no WHERE clause. See how quickly the first row come in, and how
much memory is used by the process.

I suspect they call all of the rows at pg_query execution. Otherwise
they wouldn't know how to respond to a pg_num_rows() call.


On a side note, that is a rather unique email address.


Re: pg_fetch_array

From
Richard Huxton
Date:
Thanks wrote:
> Hello....
>
> I would like to know the performance of pg_fetch_array.  Cosider the code:
>
> $query = "select * from foo";
> $result = pg_query( $db, $query );
>
> while ($row = pg_fetch_array($result))
> {
>    $a = $row["a"];
>    $b = $row["b"];
>    $c = $row["c"];
>    $d = $row["d"];
> }
>
> Does php need to read database everytime when pg_fetch_array is executed in
> the while loop or all the rows have been in the memory after pg_query?

Not knowing anything in detail about the PHP drivers, I'm almost certain
that all rows are returned to PHP and then pg_fetch_array() reads that
from memory.

> If read database is needed, is there any method to copy all the rows into
> memory by using other command? (because I have a application which needs
> large amount database update/retrieval and I wish the performance of the
> overall applications run faster.)
>
> or other method you would like to recommend in order to make the faster
> response time?

Are you sure that pg_fetch_array() is the problem? Can you give an
example of what you're trying to do with the data?

PS - there's a PHP list too.
--
   Richard Huxton
   Archonet Ltd