A
A
Albert Tobacco2015-10-26 11:47:54
Yii
Albert Tobacco, 2015-10-26 11:47:54

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
.
Request I am making
$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.
I'm going to use TEMP TABLE, but I would like to get around this Yii limitation of course
. Actually, how to get around this?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexey Ostin, 2015-10-26
@nitso

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.

D
Dmitry Donkovtsev, 2015-10-26
@Demetriy

Batch , this should probably help, read it.

I
Ilya Beloborodov, 2015-10-26
@kowap

<?php ini_set("memory_limit", "1000M"); ?>

O
Optimus, 2015-10-26
Pyan @marrk2

Rewrite specifically this block with requests to PDO or mysqli and everything will be fine

A
Alexander Melekhovets, 2015-10-26
@Blast

Disable debugging, profiling and all that in cdbconnection.

E
Evgeny_Shestakov, 2015-10-28
@Evgeny_Shestakov

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 question

Ask a Question

731 491 924 answers to any question