Additional queries executed when using the command builder?

Hi everybody,

I recently started using Yii (former Kohana and Zend user) and Ive been playing around with the different database classes and its methods to construct and execute a query. While profiling the queries, I noticed extra queries were being executed when I used the command builder and its respective methods as opposed to writing out the sql and passing it to the createCommand method. For example, I run a query to see how many books belong to a user.

Method One (2 additional queries)

$criteria = new CDbCriteria();

$criteria->condition = ‘user_id = :userID’;

$criteria->params = array(’:userID’ => $userID);

return Yii::app()->db->commandBuilder->createCountCommand(‘books’, $criteria)->queryScalar();

The two additional queries shown when profiling the queries are

system.db.CDbCommand.query(SHOW CREATE TABLE books)

system.db.CDbCommand.query(SHOW COLUMNS FROM books)

Method Two (0 additional queries)

$sql = "SELECT COUNT(*)




user_id = :userID";

$parameters = array(’:userID’ => $userID);

return Yii::app()->db->createCommand($sql)->queryScalar($parameters);

Is there a specific reason as to why additional queries are being executed using method 1?

Thanks in advance

These SQLs are executed to analyze the database table schema. They can be skipped if schema caching is enabled. For more details, please see the guide