Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Yii2 db getStats (db queries number)





There is useful method getStats in Db-component Yii

$sql_stats = YII::app()->db->getStats();
echo $sql_stats[0] //the number of SQL statements executed
echo $sql_stats[1] //total time spent

Official documentation link

Is there method in Yii2 to get this information?

like image 238
Alex Avatar asked Mar 17 '23 17:03


1 Answers

Here is equivalent for Yii 2:

$profiling = Yii::getLogger()->getDbProfiling();

$profiling[0] contains total count of DB queries, $profiling[1] - total execution time.

Note that if you want to get information about all queries at the end of request you should execute this code in right place, for example in afterAction():

public function afterAction($action, $result)
    $result = parent::afterAction($action, $result);

    $profiling = Yii::getLogger()->getDbProfiling();


    return $result;

Otherwise you will get the information according to the moment of execution this command.

Official documentation:

  • getDbProfiling()
  • afterAction()
like image 86
arogachev Avatar answered Mar 24 '23 20:03