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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With