count vs cdbcommand


This is a question I was wondering.

Which of the two is more efficient and uses less resources.

// this

$total = Tbl_SomeTable::model()->count('id=:id',array(':id'=>1));

// or this

$connection=Yii::app()->db;   // assuming you have configured a "db" connection 

$sql = 'SELECT







$total = $command->queryScalar(array(':id'=>1));


DAO(2 way) because of memory usage.

i think, i like the first way.

but the question is… which is faster execution?

So from what I can see DAO is better and more efficient.

But the model way is cleaner.

Will the performance impact be that much different when using one or the other though?

Or will it not be really noticeable?

If anyone has ever done the test before of course…

This topic is not new, but as I’ve encountered it through a search on Google, I could say I’ve made some tests from a table with 16334 results.

The speed varies from 14ms to 15ms. No noticeable differences between both methods. So I think you should be free using DAO or CDbCommand.

I’ll stick with CDbCommand just to go along with my conscience :)

The code I have tested (commented the 2nd method):

        $count = User::model()->count();

 /*       $countCommand  = Yii::app()->db->createCommand();

        $countCommand->select( 'COUNT(fld_int_id)' )->from('mg_user');

        $count = $countCommand->queryScalar();


Then I’ve compared SQL call loading time in Yii’s profilling.