S
S
Sergey2015-10-26 17:09:30
Yii
Sergey, 2015-10-26 17:09:30

Why is Yii1 query builder faster than Yii2?

I decided to compare the performance of query execution. It is not clear to me why the code was executed twice as slowly on Yii2.
Yii1 (0.56 sec):

$command = Yii::app()->db->createCommand();
$command->select('id');
$command->from('task');
$command->limit = 4000;
$result = $command->queryAll();
$data = [];
$start = microtime(true);
foreach($result as $a){
    $command = Yii::app()->db->createCommand();
    $command->from('task AS T');
    $command->where('id = :id', [':id' => $a['id']]);
    $command->limit = 1;
    $data[] = $command->queryRow();
}
$stop = microtime(true);
$time = $stop - $start;
print_r($time);exit();

Yii2 (0.97 sec):
$query = new Query();
$query->select('id');
$query->from('task');
$query->limit(4000);
$result = $query->all();
$data = [];
$start = microtime(true);
foreach($result as $a){
    $query = new Query();
     $query->from('task AS T');
    $query->where('id = :id', [':id' => $a['id']]);
    $query->limit(1);
    $data[] = $query->one();
}
$stop = microtime(true);
$time = $stop - $start;
print_r($time);exit();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rikcon, 2015-10-26
@Rikcon

I didn’t understand something, in your tests it says that yii2 is faster)
Yii1 (0.97 sec):
Yii2 (0.56 sec):

P
Puma Thailand, 2015-10-26
@opium

Well, the second one did it faster, what else do you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question