Skip to content

List page is too slow (times out) with a big database #686

Answered by noamtamir
noamtamir asked this question in Q&A
Discussion options

You must be logged in to vote

After reading this article: https://www.citusdata.com/blog/2016/10/12/count-performance/#:~:text=copy-,Estimated%20Counts,-Full%20Table%20Estimates
I ended up overriding the count_query with the following code:

    def count_query(self, request: Request) -> Select:
        query = '''\
SELECT
  COALESCE(reltuples / NULLIF(relpages,0), reltuples) * (
    pg_relation_size(:tablename) /
    (current_setting('block_size')::integer)
  )
FROM pg_class where relname = :tablename;'''

        return text(query).params(tablename=self.model.__tablename__)

Note: the intention of NULLIF and COALESCE functions are there to accommodate for an empty db, otherwise you will get a division by zero error.

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@noamtamir
Comment options

Comment options

You must be logged in to vote
2 replies
@noamtamir
Comment options

@aminalaee
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by noamtamir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants