简体   繁体   中英

How to break query into smaller queries

So, I have a PHP that goes through a large table in DB, then when I get the results I put them into CSV. But the table keeps growing and now I got error that allowed memory size is exhausted.

 SELECT column1, column2, column3 FROM table

And due to the size of table I can not put it into csv. How could I reduce the size of this query? Should I use LIMIT and then just put it into 2 halves?

If you are writing to CSV, use fopen/fwrite to keep a stream instead of trying to concat to a string (which will cause out of memory fast if it's large), for the database query itself, using the _fetch_array() you can keep the result active on the server whilst only retrieving row by row from PHP.

This should keep the memory usage quite low.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM