<p>I am trying to paginate the results of an SQL query for use on a web page.  The language and the database backend are PHP and SQLite.</p>

<p>The code I'm using works something like this (page numbering starts at 0)</p>

<p><a href="http://example.com/table?page=0" rel="nofollow noreferrer">http://example.com/table?page=0</a></p>

<pre><code>page = request(page)
per = 10 // results per page
offset = page * per

// take one extra record so we know if a next link is needed
resultset = query(select columns from table where conditions limit offset, per + 1)

if(page &gt; 0) show a previous link
if(count(resultset) &gt; per) show a next link

unset(resultset[per])

display results
</code></pre>

<p>Are there more efficient ways to do pagination than this?</p>

<p>One problem that I can see with my current method is that I must store all 10 (or however many) results in memory before I start displaying them.  I do this because PDO does not guarantee that the row count will be available.</p>

<p>Is it more efficient to issue a <code>COUNT(*)</code> query to learn how many rows exist, then stream the results to the browser?</p>

<p>Is this one of those "it depends on the size of your table, and whether the <code>count(*)</code> query requires a full table scan in the database backend", "do some profiling yourself" kind of questions?</p>

[{'answer_id': 52742, 'author': 'Owen', 'author_id': 4853, 'author_profile': 'https://Stackoverflow.com/users/4853', 'pm_score': 2, 'selected': True, 'text': "<p>i'd suggest just doing the count first. a count(primary key) is a very efficient query.</p>\n"}, {'answer_id': 52747, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 1, 'selected': False, 'text': '<p>I doubt that it will be a problem for your users to wait for the backend to return ten rows. (You can make it up to them by being good at specifying image dimensions, make the webserver negotiate compressed data transfers when possible, etc.)</p>\n\n<p>I don\'t think that it will be very useful for you to do a count(*) initially.</p>\n\n<p>If you are up to some complicated coding: When the user is looking at page x, use ajax-like magic to pre-load page x+1 for improved user experience.</p>\n\n<p>A general note about pagination:\nIf the data changes while the user browses through your pages, it <em>may</em> be a problem if your solution demands a very high level of consistency. I\'ve writte a note about that <a href="http://troels.arvin.dk/db/rdbms/#select-limit-offset-note" rel="nofollow noreferrer">elsewhere</a>.</p>\n'}, {'answer_id': 53415, 'author': 'Martin', 'author_id': 2581, 'author_profile': 'https://Stackoverflow.com/users/2581', 'pm_score': 2, 'selected': False, 'text': "<p>I've opted to go with the COUNT(*) two query method, because it allows me to create a link directly to the last page, which the other method does not allow.  Performing the count first also allows me to stream the results, and so should work well with higher numbers of records with less memory.</p>\n\n<p>Consistency between pages is not an issue for me.  Thank you for your help.</p>\n"}, {'answer_id': 62933, 'author': 'Mark Webster', 'author_id': 7068, 'author_profile': 'https://Stackoverflow.com/users/7068', 'pm_score': 2, 'selected': False, 'text': '<p>There are several cases where I have a fairly complex (9-12 table join) query, returning many thousands of rows, which I need to paginate. Obviously to paginate nicely, you need to know the total size of the result. With MySQL databases, using the SQL_CALC_FOUND_ROWS directive in the SELECT can help you achieve this easily, although the jury is out on whether that will be more efficient for you to do.</p>\n\n<p>However, since you are using SQLite, I recommend sticking with the 2 query approach. <a href="http://marc.info/?l=sqlite-users&amp;m=113579007126860&amp;w=2" rel="nofollow noreferrer">Here</a> is a very concise thread on the matter.</p>\n'}]
['https://Stackoverflow.com/questions/52723', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2581/']