PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hmm, that is helpful but not exactly working. It may be something I have to do on the database level.
I have a database query that could possibly return over a million records. Even if I check the memory size before or after, it's still bombing DURING the query. So if a query is trying to return over a million records or over a certain memory size, I'd like to throw an exception and terminate the query if that's possible.
dimxasnewfrozen wrote:Hmm, that is helpful but not exactly working. It may be something I have to do on the database level.
I have a database query that could possibly return over a million records. Even if I check the memory size before or after, it's still bombing DURING the query. So if a query is trying to return over a million records or over a certain memory size, I'd like to throw an exception and terminate the query if that's possible.
If your returning a million rows, chances are your design is terribly innefficient.
Well, most likely. I can definitely make sure the query doesn't get executed incorrectly. However, for my own stubbornness, is it possible terminate a query if it exceeds the memory size?
I'm not sure what you mean, if your memory limit is reached then then the script execution is terminated. You probably just want to increase the memory size as much as your machine permits.
However, there is no reason why you should ever query 1million+ rows in a single query. You'd be far better off doing 100 queries fetching 10,000 rows (or some variant). This way, you can recycle the memory used instead of storing the entire result set.
To spell it out: no, you can't "catch" an out-of-memory error. If it happens, your script dies.
a) Set the memory limit really high. Almost always a bad idea.
b) Adjust your code so that it doesn't need so much memory. Best idea, but can't fix everything.
c) Check memory usage during runtime and increase it as needed. Not such a good idea but has its uses.