Recently I have been reading SQL Performance Explained by Markus Winand and I wanted to share with you what I have learned regarding SQL pagination scalability.
Probably if you would be given the task to implement pagination, you would do it with LIMIT
and OFFSET
in a query. This would be completely fine, but it is good to know the limitations of it.
The more you browse back in history (increase LIMIT
and OFFSET
) the more response time increases. This is due to the fact that DB has to count all rows until it reaches the requested page.
An answer for that would be to include a WHERE
statement in a query with FETCH FIRST X ROWS ONLY
, which does not select previous results. Each "page" is limited with a different WHERE
statement. It also has its own limitations (harder to implement pagination, harder to browser backward, fetch arbitrary pages) but at a cost of simplicity, you get a performance increase.
Which option we should choose? As always in computer science - it depends :)
Let me know how do you use pagination and if you ever had any performance issues related to that.
P.S Remember that pagination needs deterministic order and do include ORDER BY
in your queries when needed ;)
Top comments (0)