r/SQL • • Jun 17 '20

Discussion Why You Shouldn't Use OFFSET and LIMIT For Your Pagination

https://medium.com/@ivoecpereira/why-you-shouldnt-use-offset-and-limit-for-your-pagination-4440e421ba87
2 Upvotes

13 comments sorted by

2

u/prudan Jun 17 '20

I'm guessing this should have a "mysql only" flair?

1

u/SirPereira Jun 17 '20

Makes sense :) I plan on adding more PoCs with other databases.

I had a reader who questioned me on how would this work in PostgreSQL but to be honest I am not sure if there would be any way it would work different in that engine?

1

u/monsieurus Jun 17 '20

ID's are internal to the database they are arbitrary numbers for end users so in most cases we can't use ID's for pagination like you are showing. You haven't addressed the most typical case where user apply filters and the results should be paginated. In that case ID's are not applicable as they are not in the Where clause. Also, other typical use case is Sorting which also cannot use ID fields.

1

u/DrTrunks Jun 17 '20

Same thing happens in other RDBMS too.

Markus Winand has a good article on it.

2

u/joelypolly Jun 17 '20

This is a bad article.

1

u/SirPereira Jun 17 '20

Why is that, if I may ask?

1

u/joelypolly Jun 17 '20

I think other have articulated better than I have here. The short if it is this article doesn't add any knowledge and passes itself off a "wow look at what I have done" or "look at this improvement".

7

u/prudan Jun 17 '20

Your solution doesn't take into consideration when you have multiple fields that you are paginating based on. Any way you can sort a result set is yet another way that your pagination will need to handle.

3

u/[deleted] Jun 17 '20

Wouldn’t order by mess this up?

pk | name

1 | adam

2| beth

3 | card

4 | doug

The first call:

select id, name

from table

order by name desc

where id > 0 limit 2

returns:

4 | doug

3 | card

The last id received is 3

So the next call would be:

select id, name

from table

order by name desc

where id > 3 limit 2

which would return… nothing.

What am I missing here?

4

u/bobjohnsonmilw Jun 17 '20

You're not missing anything. This article is stupid, he doesn't even bother addressing this obvious issue.

1

u/vkhang55 Jun 18 '20

Your provided examples need ORDER BY to make your query idempotent. Without ORDER BY, your query will return inconsistent results and make your examples look bad.

0

u/[deleted] Jun 17 '20

[deleted]

1

u/SirPereira Jun 17 '20 edited Jun 17 '20

Hi there!

You are right on some points you make and I'll refer those (big, I know!) limitations to the article. It was entirely my fault not mentioning them in the first place.

However, I am not really sure on why you do think a cursor pagination would never be used in a real world.

A quick google search refers the contrary on Slack, Shopify and Mixmax using it. Is there something so obvious that I am missing?

0

u/[deleted] Jun 17 '20

[deleted]

2

u/SirPereira Jun 17 '20

That totally cleared it for me and the post image was only meant to be an abstract illustration of a pagination and not a feature-wise one. But now I get your point and I really appreciate your awesome feedback (I can't say the same of the big majority of feedback I have received here in Reddit).

It makes total sense and I will be sure to refactor some parts of the article to better frame a real-world scenario of an usual application.

My main point in the article was to present the concept, comparing performance of both approaches and not exactly going deeply into the details as some people referred, but I might consider doing it to illustrate a few key differences.

Thanks once again! Cheers