0
All right guys,
I’m using Laravel for a project and I’m kind of worried about using memory in some classes. So I decided to do a basic test and discover that a method was consuming a lot of memory and decided to change some things...between them replace the use of Elouquent with Querybuilder.
Behold:
Before:
$grade_blocos = \App\Blocos::where('grades_id', '=', $grades_id)
->with(['produtos' => function($query) {
$query->select('id');
}])->orderBy('bloco', 'asc')->orderBy('dia_semana', 'asc')
->get()->toArray();
Afterward:
$select =
[
'blocos.grades_id',
'blocos.bloco',
'blocos.dia_semana',
'blocos.duracao',
'blocos_produtos.blocos_id',
'blocos_produtos.produtos_id',
];
$grade_blocos = DB::table('blocos')
->join('blocos_produtos', 'blocos.id', '=', 'blocos_produtos.blocos_id')
->select($select)
->where('blocos.grades_id', $grades_id)
->get();
With that I won a few megas and a few milliseconds.
But from here down there was no change in the method.
Strange that what was saved up did not reflect at the end spending further down.
I did some testing on interesting parts of the code see result: Note the tasks "Query Blocks" and "Commercial Query"
Really I did not understand why the query to the commercial table bank began to consume more memory after I changed left the blocks query lighter, taking into account that I did not change the commercial query.
Unchanged:
Laço While ----------------------------------------------------- Tempo de execução: 0.00536 s. Memoria inicial: 9.25 MB. Memoria Final: 9.5 MB. Memoria Utilizada: 0.25 MB. ----------------------------------------------------- **Consulta banco de dados tabela "Blocos" ----------------------------------------------------- Tempo de execução: 0.57876 s. Memoria inicial: 9.5 MB. Memoria Final: 39.25 MB. Memoria Utilizada: 29.75 MB. -----------------------------------------------------** Consulta banco de dados tabela "Comerciais" ----------------------------------------------------- Tempo de execução: 0.53618 s. Memoria inicial: 39.25 MB. Memoria Final: 50.5 MB. Memoria Utilizada: 11.25 MB. ----------------------------------------------------- Laço Foreach Final ----------------------------------------------------- Tempo de execução: 2.60831 s. Memoria inicial: 50.5 MB. Memoria Final: 52 MB. Memoria Utilizada: 1.5 MB. ----------------------------------------------------- TEMPO TOTAL DE EXECUÇÃO DO SCRIPT ----------------------------------------------------- Tempo de execução: 3.72881 s. Memoria inicial: 9.25 MB. Memoria Final: 52 MB. Memoria Utilizada: 42.75 MB. -----------------------------------------------------
Alterally:
Laço While ----------------------------------------------------- Tempo de execução: 0.00576 s. Memoria inicial: 9.25 MB. Memoria Final: 9.5 MB. Memoria Utilizada: 0.25 MB. ----------------------------------------------------- Consulta banco de dados tabela "Blocos" ----------------------------------------------------- Tempo de execução: 0.0788 s. Memoria inicial: 9.5 MB. Memoria Final: 18.75 MB. Memoria Utilizada: 9.25 MB. ----------------------------------------------------- Consulta banco de dados tabela "Comerciais" ----------------------------------------------------- Tempo de execução: 0.54036 s. Memoria inicial: 18.75 MB. Memoria Final: 49 MB. Memoria Utilizada: 30.25 MB. ----------------------------------------------------- Laço Foreach Final ----------------------------------------------------- Tempo de execução: 2.20804 s. Memoria inicial: 49 MB. Memoria Final: 50.25 MB. Memoria Utilizada: 1.25 MB. ----------------------------------------------------- TEMPO TOTAL DE EXECUÇÃO DO SCRIPT ----------------------------------------------------- Tempo de execução: 2.83315 s. Memoria inicial: 9.25 MB. Memoria Final: 50.25 MB. Memoria Utilizada: 41 MB. -----------------------------------------------------
What can be happening ?
In the first version you take all the blocks, and then run a query for each taking the respective products. The second version has a Join, so it only brings the blocks that have products. They are different.
– bfavaretto
In addition, the second has no order by, which can greatly influence the execution time of the query.
– bfavaretto
@bfavaretto: Yes so I decided to use Join (even some saying to use Elouquent). But the problem is that the amount of memory spent on the trading table, and this has not been changed.
– Daniel Lopes
@bfavaretto: This query is executed in both cases and they are spending memory differently. $blocos_tempo = DB::table('commercials') ->Join('products', 'commercials.productos_id', '=', 'products.id') ->select('blocos_id', 'activities_commercialis_id', 'data', 'products.duration') ->wherein('blocos_id', $blocks) ->Where('data', '>=', $periodo_start) ->Where('data', '<=', $periodo_end) ->get();
– Daniel Lopes
I already find absurd a Framework that consumes 9mb of memory just to run one
Hello, world
, while a normal PHP pageHello, world
spent on average 270kb ... Now imagine the absurdity that is ~40mb for a server per request. The problem with Laravel is that he lives off "buffer," apart from the fact that his middleware goes through a lot of things, which means that anything in him turns into a gobbler and he’s not the only framework to go through it, most of the popular ones go through, of course I won’t stay here indicating frameworks good vs bad, I believe that of to improve much in the Aravel if you adjust your code.– Guilherme Nascimento
@Guilhermenascimento .
– Daniel Lopes
@bfavaretto "In the first version you take all the blocks, and then run a query for each taking the respective products." in fact it takes all blocks and products by the relation, it performs in fact 2
SQL
and if there are repeated products in the other blocks it places the class of the same instance. That relative to the bottom is faster even having 2orderBy
, of course should take much into account how tables are configured.– novic
@Daniellopes the two forms perform
Query Builder
and in the most current version was equaled both toEloquent
so much forDB
, missed to say a thing how many records are returned in the first, including those of the relation? and how many are returned in the second withinner join
? got curious.– novic
@Virgilionovic I will check here...but what impresses me most is because the code that comes after o this modified code... : Query database table "Commercial"
– Daniel Lopes