SQLite: COUNT slow on big tables

前端 未结 8 2036
不知归路
不知归路 2021-02-02 07:43

I\'m having a performance problem in SQLite with a SELECT COUNT(*) on a large tables.

As I didn\'t yet receive a usable answer and I did some further testing, I edited m

8条回答
  •  孤城傲影
    2021-02-02 08:03

    The output for the fast queries all start with the text "QP: SEARCH". Whilst those for the slow queries start with text "QP: SCAN", which suggests that sqlite is performing a scan of the entire table in order to generate the count.

    Googling for "sqlite table scan count" finds the following, which suggests that using a full table scan to retrieve a count is just the way sqlite works, and is therefore probably unavoidable.

    As a workaround, and given that status has only eight values, I wondered if you could get a count quickly using a query like the following?

    select 1 where status=1 union select 1 where status=2 ...

    then count the rows in the result. This is clearly ugly, but it might work if it persuades sqlite to run the query as a search rather than a scan. The idea of returning "1" each time is to avoid the overhead of returning real data.

提交回复
热议问题