Back

Collection total count does not show > 5000

  • 0
  • Databases
Said H
23 Feb, 2023, 08:23

hello, i have a collection containing 8k docs, but the console only shows total of 5k. Also, when i create new document manually, the count does not increase. When i query the total count, it is also showing just 5000

TL;DR
The user has a collection with 8,000 documents, but the console only shows a total of 5,000. Additionally, manually creating a new document does not increase the count. When querying the total count, it also shows just 5,000. The solution suggested is to use cursor-based pagination instead of offset pagination. Counting is an expensive operation and is limited to 5,000. It is recommended to use a separate collection to store the count for better performance.
Said H
23 Feb, 2023, 08:26
Maniac_Fighter
23 Feb, 2023, 15:24

count is an expensive operation. So it's limited till 5000. while if you view more in the console by going till the end, you can see more documents. I would recommend if you wanted to keep a count operation then maybe make a seperate collection to store that for better performance

VincentGe
23 Feb, 2023, 15:49

^ What Biswa said 😉 Count is a O(N) operation and pretty evil in general. We discourage it. Numbers exceeding 5000 will just be 5000+

Exact counts can be tracked many different ways. With metadata tables that are incremented by an Appwrite function triggered by database document events, or run periodically.

We also recommend cursor based pagination like what's used by GitHub. ID + Offset instead of offsetting by number.

Ponguta_
23 Feb, 2023, 19:00

How ID + Offset works?

VincentGe
23 Feb, 2023, 19:01

You basically find the ID of the last item from the previous page and fetch the next N items

VincentGe
23 Feb, 2023, 19:01

This way, if there were inserts or deletes, you will avoid duplicate items in your pages

VincentGe
23 Feb, 2023, 19:02

For example

Given items [a, b, c, d, e, f] The first page is [a, b, c] The next page would be

TypeScript
Query.limit(3),
Query.cursorAfter(c),
VincentGe
23 Feb, 2023, 19:03

If you use offset pagination, and someone inserted a1, the new data is: [a, a1, b, c, d, e, f]

If you used offset pagination

TypeScript
Query.limit(3),
Query.offset(3),

Might actually return c, d, e instead of d, e, f

VincentGe
23 Feb, 2023, 19:04

So we recommend cursor pagination because it performs better and avoids the above described pitfall

Ponguta_
23 Feb, 2023, 19:36

Thats pretty interesting, but how that cursor can work? it orders data based on the creation time?

I mean, data should be sorted somehow to use an offset cursor, right?

Ponguta_
23 Feb, 2023, 19:39

@VincentGe Thanks for the info

VincentGe
23 Feb, 2023, 20:08

Data is always sorted

VincentGe
23 Feb, 2023, 20:08

It's got a natural sort order, never gonna fetch data at total random

VincentGe
23 Feb, 2023, 20:09

You can also specify order in Appwrite during queries, but there should be a natural order never the less

Ponguta_
23 Feb, 2023, 22:02

@VincentGe I understand now, thank you so much

Said H
23 Feb, 2023, 22:56

i see, thanks @Maniac_Fighter and @VincentGe

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more