Yii2查询响应时间长

时间:2017-02-09 10:04:53

标签: php mysql pdo yii

我正在使用yii开发一个php web应用程序。

我已经编写了一个访问mysql表的小查询,我注意到使用Yii查询时,术语或响应时间的性能比使用PDO慢得多。

这里有一个重现问题的最小代码:

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

class TestController extends Controller {

public function actionTest1(){

    $start = round(microtime(true)*1000);

    $col = 'mysql:host=127.0.0.1;dbname=my_database';
    $db = new \PDO($col , 'user', 'pwd');
    $sql = $db->prepare("SELECT id FROM user_content");
    $sql->execute(); 
    $res = $sql->fetchAll();
    echo "Execution time (ms):".(round(microtime(true)*1000) - $start);
}


public function actionTest2(){


    //**** YII db configuration
    // return [
    //     'class' => 'yii\db\Connection',
    //     'dsn' => 'mysql:host=127.0.0.1;dbname=my_database',
    //     'username' => 'user',
    //     'password' => 'pwd',
    //     'charset' => 'utf8',
    // ];

    $start = round(microtime(true)*1000);
    $sql = "SELECT id FROM user_conten";
    $result = Yii::$app->db->createCommand($sql)->execute();

    echo "Execution time (ms):".(round(microtime(true)*1000) - $start);
    }
 }

在这两种情况下,打印执行时间相当短1-2ms,但使用chrome dev工具监控的响应时间却截然不同。 在测试1中,它像50-70ms,而在第二种情况下,它约为500ms。

Case 1

Case 2

任何人都有这种行为的解释? (禁用Yii调试)

谢谢!

0 个答案:

没有答案