1

What is the rationale for having auto index on _id field for capped collections by default? We can find in the docs that:

Without this indexing overhead, capped collections can support higher insertion throughput.

There was a post about capped collection insert performance and my own tests also show that for inserts capped collection without an index is the fastest option, then the normal collection goes, and the slowest option is capped collection with an index. So why auto index was added along with _id fields in version 2.2 if it hits performance while capped collections are proposed as fast alternatives to normal collections in certain scenarios?

Community
  • 1
  • 1
wombatonfire
  • 4,585
  • 28
  • 36

1 Answers1

1

Well, we certainly can't rule out benefits of _id in capped collection too. It help you and in fact required for replication.

MongoDB turned it on by default since deployment of MongoDB in replica set configuration is very normal now a days. You can find more information in documentation, please look for autoIndexId

I believe, the reason of slowness is Index, not _id field itself. So if your requirements warrant special needs, you can always disable auto index.

But...

you still need to supply _id field with zero (0) value.

e.g. 2 GB capped collection with auto index disabled.

db.createCollection("people", { capped: true, size: 2147483648, autoIndexId: false } )

I'm sure this trick will bring insertion speed back.

Saleem
  • 8,728
  • 2
  • 20
  • 34
  • @Salem, thank you for your response. autoIndexId is actually what I used to test the performance for inserts. And yes, if you disable it, inserts will be faster. But what are the reasons to keep it enabled? The only one I see right now is that for replica sets, all collections must have autoIndexId set to true. As for the maintaining insertion order: for capped collection it works without index as well. – wombatonfire Mar 08 '16 at 14:00
  • Yes, it maintains insertion order but there is no guarantee. Also, MongoDB (company) is targeting mass scalability and competing with some major players so they weighed it as more beneficial to target for more common use rather than some special use – Saleem Mar 08 '16 at 14:02
  • This the reason that they enable auto indexing by default. Again they allow us to tweak MongoDB based on our needs under special circumstances. – Saleem Mar 08 '16 at 14:05
  • Docs tell us that "Capped collections guarantee preservation of the insertion order. As a result, queries do not need an index to return documents in insertion order." (https://docs.mongodb.org/manual/core/capped-collections/#insertion-order). – wombatonfire Mar 08 '16 at 14:21