1

I need to give away results of the query in response to a following URL:

http://foo.com/search?from=20&perpage=10

What is the best practice from performance point of view?

  1. Run the query every time with WHERE row > N and row <= K

  2. Run query once and keep all the result for certain amount of time

  3. What else could be done?

Nerd
  • 321
  • 1
  • 4
  • 9
  • Depends on your database. Running a `... LIMIT xx, 10` query with changing `xx` but identical conditions may cause your database to cache results internally already. – deceze Jul 18 '12 at 10:33

1 Answers1

2

If your query will produce a very small amount of data you can consider to cache the results. But think about this:

  • High performance pagination isn't trivial. It can be done database-side with better performance (if you're using Microsoft SQL Server 2012 it has its own extensions for this).
  • Pagination is done by users and they need time to read data and click the "Next page" button. Performance hit of a new query won't be big and the database will handle its own cache better than what we can do.
  • Cache a large amount of data is useless, memory consuming and less efficient than a new query.

Then my answer is run the query every time. Your database will handle that better than you and it'll scale with your data (otherwise what may work for 500 record will be broken when your system will have 1000000 records).

To summarize: database engines are more efficient than us to cache data and they scale better, (usually) a good index is all they need to perform well.

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208