$test = Test::model()->findBySql("SELECT * FROM test WHERE active = 1 ORDER BY counter ASC");
Secondly:
$db = Yii::app()->db;
$sql = "SELECT * FROM test WHERE active = 1 ORDER BY counter DESC";
$command = $db->createCommand($sql);
$test = $command->query();
(Table has only two records)
AR took almost 0.3sec to run. ADO took just 0.01sec. Is this the price for AR`s convenience?
This is not likely because the extra cost in your example in AR is only the call to createFindCommand() and the creation of the AR object, which can’t take that much overhead.
Because you are using schema caching, it is possible that the overhead is caused by that part, depending on what caching you are using.
To reduce the impact of the schema caching, you may conduct the test by running findBySql() 1000 times versus query() 1000 times.
Actually, my server`s CPU is heavily used right now. Maybe this is the reason. I guess DAO doesnt require calculations at all, thats why it performed well.
As I know twitter initially was built using AR, after their initial crashes they had to rewrite all to direct querys to db, this removes some overhead, and also gives you fine grain control, on the other hand only small amount of web apps become such a hit so AR for simple web app/site is good choice, also AR includes security features as I know still very fresh here.
So in your case it is the other way around? AR is FASTER than DAO? That is really strange. AR is built on top of DAO so it doesn’t make sense at all. Maybe you are using 2 different db components to connect to the database? One with schemaCaching and the other one without? If that’s not the case than I think that there is some code in your loop that slows down the process.
Connectios string is the same like any other part of the code. I’ve only replaced DAO code with AR code leaving the rest unchanged. Maybe AR uses some optimizations for INSERT querys?