Reputation:
Let's suppose I have a table with 10 000 rows. Some user (for example on web site) wants to work with this table applying some where conditions and order by conditions.
Variant #1 - using sql limit and usual pagination
As I understand database must 1)find all(!) rows that meet where conditions, 2) database must sort all(!) found rows at step 1 and from these found rows 3) it must return some set defined by limit and offset.
Variant #2 - reading all ids in list structure
Another solution is do a first query to get list of ids for certain where conditions and order by conditions and save it. For every page we get rows where id in {..} and sort on client side by list we have (because database will return rows in unsorted order). Advantage of this variant is not to filter and sort all rows for every page. Disadvantage is necessity to save rather long list. Besides this variant can be used only if data don't change frequently.
Is my understand of variant #1 right? Is variant #2 used in practice or it is bad solution?
Upvotes: 0
Views: 353
Reputation: 1269443
Variant 1 is the "normal" way of approaching this. In many cases, you would be surprised at how well it works. Here are some reasons:
The second approach has some downsides:
order by
, this probably is no more expensive than the first run for Variant 1.That said, Variant 2 might be the best solution under some circumstances. However, the more typical solution is to let the database handle the pagination.
Upvotes: 0