3 ms·
I'd argue that the author is actually under-representing the value of pagination by making an obvious mistake in implementation. For example, under the ratings
by adventured 8y ago
I'd argue that the author is actually under-representing the value of pagination by making an obvious mistake in implementation.
For example, under the ratings for pagination, they list: "Bad: Performance issues".
That bad rating is based on doing a dynamic post count for every page load, an approach that would only be used if someone wanted to maximize the performance penalty (or if your site/service is small enough that it simply doesn't matter if you do a new dynamic count on every page load). There's no common scenario where you aren't better off storing that post count, altered only as needed, rather than calling a new count of all posts for every single page load.
Even in a scenario of very heavy write, rare read, you're better off doing either a stored constantly incremented count or stored compromised occasional count that isn't perfectly accurate until or unless it needs to be.
Specifically the author uses this:
SELECT COUNT(*) AS total FROM posts
Instead of that, as a common scenario, let's say you have a posts table, and a comments table (which correlates comments that belong to posts by an id). You don't count how many comments each post has dynamically on every page load for a post, in the process of showing the comments. You have a counter in the post row, that increments only when a new comment is made, or decrements if a comment is removed. You pull that count at the same time you pull the core post information from the "posts" table, in the process of loading the post and then afterward pulling its associated comments.
The penalty is that you must update the post row when a new comment occurs. Assuming your service is heavier on read than write, which is almost universally the case, this is an easy win.
If all you wanted to do is track the post count, and not something further connected to it such as comments, that's also similarly trivial. There are numerous good ways to go about storing the post count until it needs incremented, rather than calculating it dynamically every page load.
All one has to do is consider a routine blog example, in which an author updates the blog once per day, and receives 1,000 or 10,000 visitors per day. There are 1,600 historical blog entries/posts spanning over four years. This article's approach is to needlessly dynamically count that over and over, perhaps tens of thousands of times per day, rather than altering and storing the count once per day when a new blog post is created. Performance bad indeed.