Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're trying to optimize for large result sets like this you're almost always better off rolling your own. The Yii developers <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.ar" rel="nofollow">even recommend</a> not using activerecord for complex situations like this (very different loading order).</p> <h1>Although</h1> <p>To begin with, I would suggest building your own <code>CDBCriteria</code> based on the IDs of all posts you retrieve. If you do this within the <code>Posts</code> model it's also tied nicely (obvisouly not perfectly) with the criteria defined for the relation. </p> <pre><code>static public function loadComments(array $ids) { $crit = Comment::model()-&gt;dbCriteria; $crit-&gt;addInCondition('post_id', ..... ..... return Comment::model()-&gt;findAll($crit); } </code></pre> <p>This would give you all comments back for that set of IDs, you could even do some extra processing to index them by the post ID they belong too.</p> <h1>But, really</h1> <p>In your comment you say this 100 posts with 200 comments each could mean loading 2000 (actually 20 000). But if you're loading that many posts + comments on a single page, you're optimizing in the wrong place.</p> <p>You have a few things to implement before worrying about DB query size in my opinion:</p> <ol> <li><p>Don't load comments until they're needed, be this on clicking a button, scrolling to that posts' title or simply progressively down the page until it's complete. This way the server only looks up comments for one post at a time, and the user may not even want to see comments for post number 99.</p></li> <li><p>Don't load 100 posts per page, unless they're very small posts (in which case, what's the worry). Progressive scrolling again will help, or even <a href="http://www.codinghorror.com/blog/2012/03/the-end-of-pagination.html" rel="nofollow">pagination</a></p></li> <li><p>Filter your results to only show those relevant, there are very likely not 100 posts the user wants to see, and if there's 200 comments, how many users will read more than 10?</p></li> </ol> <p>If you implement even 1 of these, I think the problem of result set size will simply go away.</p> <h1>However</h1> <p>If you <em>really</em> do need to worry about 20 000 rows, ActiveRecord instantiation should be first on your radar. 20 000 rows is nothing to worry about in any normal setup, but 20 000 active record objects certainly is, even on higher spec systems. You ought to be using DAO if you're dealing with very large and/or complex data sets.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload