I would like to know what precisely is happening since php script runs the query to the moment when database returns data and php script starts to fetch them.
In theory, I have a Postgre database with huge table, talking about 10/20 million records with 20+ columns and I have a php script which is requesting all that rows from database to be placed in, lets say, some file.
My knowledge is:
I am trying to accomplish best possible optimization of php code which is doing described work above.
The questions are:
Thanks in advance.
PostgreSQL does (by default) return all rows in one go. This has the advantage of freeing resources on the server side at the cost of possibly large result sets at the client.
The alternative is to use a cursor which can return a single row at a time. Some drivers support this directly (maybe PDO?) or you can use DECLARE and FETCH
The other thing to be aware of is PostgreSQL's COPY command which can dump a table or query to a file directly (assuming you don't need much processing/formatting). Check if your datbase library offers direct access.