0

There's a thread at https://github.com/amatsuda/kaminari/issues/545 talking about a problem with a Ruby pagination gem when it encounters large tables.

When the number of records is large, the pagination will display something like:

[1][2][3][4][5][6][7][8][9][10][...][end]

This can incur performance penalties when the number of records is huge, because getting an exact count of, say, 50M+ records will take time. However, all that's needed to know in this case is that the count is greater than the number of pages to show * number of records per page.

Is there a faster SQL operation than getting the exact COUNT, which would merely assert that the COUNT is greater than some value x?

1
  • What's DB client you are using.. Postgresql, MySql .. which one ? Commented Mar 31, 2015 at 18:10

1 Answer 1

3

You could try with

SQL Server:

SELECT COUNT(*) FROM (SELECT TOP 1000 * FROM MyTable) X

MySQL:

SELECT COUNT(*) FROM (SELECT * FROM MyTable LIMIT 1000) X

With a little luck, the SQL Server/MySQL will optimize this query. Clearly instead of 1000 you should put the maximum number of pages you want * the number of rows per page.

Sign up to request clarification or add additional context in comments.

1 Comment

TOP function from which DB engine ? Do you have a generic solution, which can be used across the DB ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.