Answer the question
In order to leave comments, you need to log in
Why does yii eat up so much memory for large queries?
I make requests through Yii::app()->db->createCommand which end up being quite large (100-150 thousand characters). These queries do not return large data. The problem is that where does Yii store the history of requests and when I make 100 (and I need to make 20,000 of them :) ) such requests I have fatal
Fatal error: Allowed memory size.
$ids = Yii::app()->db->createCommand()
->select('user_id')
->from('user_community')
->where("community_id = :community_id AND user_id IN({$subjectsIn})", [
':community_id' => $communityId
])
->queryColumn();
subjectsIn- this is a string of 120 thousand characters.
Answer the question
In order to leave comments, you need to log in
Show the code, it will greatly increase the likelihood of finding the right solution.
There can be two problems:
1) At each iteration, new objects are created and are not cleaned up upon completion
2) Objects are cleaned up, but due to their large size and small number they are not processed by the garbage collector
To solve the first problem, it is enough to unset for unnecessary objects
To solve the second - do gc_collect_cycles. Since this operation is time-consuming, you can do it every 10/100/1000 cycles, pick it up yourself.
On the subject of garbage collection: php.net/manual/ru/features.gc.performance-consider...
In short, the garbage collector is called when the number of unused objects reaches 10000. Therefore, if the objects are large and 10000 pieces do not fit into memory, there will be an overflow.
Rewrite specifically this block with requests to PDO or mysqli and everything will be fine
Disable debugging, profiling and all that in cdbconnection.
how do you get this $subjectsIn - user_id array? it may be better not to store this variable in memory at all, but to make a query like SELECT * FROM table1 WHERE id IN (SELECT id FROM table2); those. place ... AND user_id IN({$subjectsIn}) ... rewrite to ... AND user_id IN (SELECT user_id FROM `user_community` WHERE condition) ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question