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:
- php script runs the pg_query command with sql query
- through postgres php driver/extension query is passed to database
- database does the work and return results to driver
- php postgres driver stores results in server RAM memory and returns resource id to php script which is reference to results
- pg_fetch_row reads records from RAM memory
I am trying to accomplish best possible optimization of php code which is doing described work above.
The questions are:
- Is my knowledge correct? If something is missing or I am not right about some step, please correct me.
- How can I know how much RAM memory is allocated to database result? This can be important if my server doesn't have enough memory.
- I know I can fetch single row, then store it into file and repeat these 2 steps as long there are rows, so I can minimize memory required for php script, but how can I influence the memory used by resource?
Thanks in advance.