5

I'm trying to speed up some SQL queries. My SQL query on pagination is like this (3rd page):

SELECT 
    SQL_CALC_FOUND_ROWS book_id, book_title
FROM
    books
LIMIT 40, 60

And then getting all result count:

SELECT FOUND_ROWS();

But if the result count is huge (milions of books) then FOUND_ROWS() can took very long time.

The fact is that there is no need to count milions of rows (books) and the answer "10000+" is enough for usual user.

Is possible something like this? Pseudocode:

SELECT FOUND_ROWS(LIMIT 10000)
Martin Ille
  • 6,747
  • 9
  • 44
  • 63
  • If you have access to the row count via the db wrapper library for your other programming language that uses it (if there is one), then that library has the row count returned in a property. A table schema would also be nice (indexing). – Drew Jun 26 '16 at 14:19
  • 2
    The infamous [Percona article](https://www.percona.com/blog/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/) – Drew Jun 26 '16 at 14:25
  • @Drew this article is almost 10 years old, is it still actual ? – krokodilko Jun 26 '16 at 15:06
  • 1
    @kordirko it is a never ending debate what is the best approach. Your results will lead your way. And the topic gets old discussing it after a while though we all get [sucked into it](http://stackoverflow.com/a/883382) based on partially broken interfaces, driver patches, personal programming styles, etc – Drew Jun 26 '16 at 15:10

1 Answers1

0

I am not sure, if it is possible. But I would suggest trying the following:

SELECT count(*) into number_of_rows from books.

then use number_of_rows for pagination.

Frits
  • 7,341
  • 10
  • 42
  • 60
Gopal
  • 11
  • 3