I'm diving into API design and running into some challenges while trying to build realistic projects. Right now, I have a basic CRUD API that handles Posts and Comments. My goals are: 1) to create a gallery view showing about 12 posts per page (using scrolling pagination) where each post displays the first 3 comments; and 2) to present some statistics for each post, like the total number of comments and whether a moderator has commented. However, I'm struggling because my ORM fetches all comments for each post, forcing me to resort to native SQL techniques like lateral joins to manage it. Plus, as I add more statistics, the queries become complex and performance degrades. Caching doesn't seem effective since any modification to comments requires purging the cache for the entire page of posts. I'm curious about how real-life applications manage fetching associated collections (like comments) and aggregated statistics.
4 Answers
Consider fetching all posts in one go, then extract their IDs to get the first few comments per post with a second query. It’s efficient and allows you to manage the post data neatly. Make sure you have the right indexes in place to speed things up. And don't hesitate to precompute stats for performance—just store the relevant numbers in the post row.
As your app scales, it’s common practice to run background jobs that calculate post statistics and store them alongside each post. Running stats on every single request can bog down performance. If you store those comments counts and moderation flags on the post itself, updating them as changes occur can keep your read queries light and fast.
For the gallery view you're aiming for, why not create a dedicated endpoint? Something like GET /posts?page=1&include=comments.preview and structure the response to include a preview array of comments with each post. Utilize eager loading with constraints to handle comments in manageable packs, plus cache individual posts to avoid purging entire pages at once!
Your idea of loading more comments initially is reasonable. From a database perspective, fetching a larger set of comments (like 50) might not take significantly longer than a smaller set. Just think about how best to communicate the updates to clients without always needing to reload everything from the server. Incremental updates can save a ton of time and resources!

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically