title: PHP Laravel Efficient Feed Generation
body: <p>Alright, so I have a feed-based web app, kind of like <em>facebook</em>'s <strong>News Feed</strong>. The issue is in the way I am generating the feed. I'm not sure it's the most efficient and/or best way to do it.</p>
  
  <p>I use Laravel + Eloquent ORM</p>
  
  <pre><code>$feed = Post::whereIn('id', $following_ids)
                          -&gt;orderBy('created_at', 'desc')
                          -&gt;take(20)
                          -&gt;skip($page)
                          -&gt;get();
  </code></pre>
  
  <p>Where <code>$following_ids</code> is an array of ids that the user is following. </p>
  
  <p>The issue is that as I understand it, the SQL generated by the above statement causes every post by the users you're following to be loaded first, and then sorted chronologically only to have a small subset actually used.</p>
  
  <p>Is there any more efficient way to do this? This seems really wasteful.</p>