public function getCriteria($pid){
return new CDbCriteria;
$criteria->alias='a';
$criteria->select="*,subid as a";
$criteria->condition="(select count(*) from documents as b where b.revision = a.revision and b.revid > a.revid ) <=0 pid={$pid}";
$criteria->order="dateModified";
$criteria->limit = 10;
}
$criteria = Documents::getCriteria($pid);
$documents = new CActiveDataProvider('Documents', array('criteria' => $criteria));
That criteria returns all the records associated with $pid, and is working as expected. It will also only return the latest version of a single record.
My issues lie within speed of that query, its taking 5 seconds to run and load the page, is there a way to speed that up? Or reduce the SQL query todo the same thing?
It selects all the documents ordered by the date they were changed, groups them by a revision stream (revision) and only returns the highest version from that group. max(revid).
Is there a simpler way todo this in MySQL or yii?
With 5 - 10 records in the table, load time is nothing, its perfect. When there is 1009 records in the table, the load and query time is increased tenfold.
and take a look at the sql-statements and the consumed time.
Test the performance of the generated sql directly in phpMyAdmin. Maybe it’s an issue of the statement.
For complex requests and better performance I would prefer the CSqlDataProvider (returning the records as array and not createing a model for each fetched row).
Thanks Joblo, I will have a look at that some time today see if I can’t fix the query, it is using pagination, and should only be loading 10 results at a time. Unfortunately I need these results as an active record.
Okay, here are the two queries that take 2.3 seconds each to run.
SELECT COUNT(*) FROM `documents` `a` WHERE (select count(*) from documents as b where b.revision = a.revision and b.revid > a.revid ) <=0 AND status in ('Approved', 'Pending Approval') AND cid='442' AND pid='112'
SELECT *,subid as a FROM `documents` `a` WHERE (select count(*) from documents as b where b.revision = a.revision and b.revid > a.revid ) <=0 AND status in ('Approved', 'Pending Approval') AND cid='442' AND pid='112' ORDER BY dateModified LIMIT 10
If any one can see a way to fix those queries, +10 internets for you