MongoDB - Unique index vs compound index

后端 未结 2 1482
春和景丽
春和景丽 2021-01-05 02:43

Assume a hypothetical document with 3 fields:

  1. _id : ObjectId
  2. emailAddress : string
  3. account : string

Now, given a query on emai

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 03:06

    In terms of performance the difference will be small at best. Due to the fact that your e-mail addresses are unique any compound index that has an e-mail field will not ever be more helpful than an index on e-mail address alone. The reason for this is that your e-mail field already has maximal cardinality for your collection and any further index fields will not help the database to filter records more quickly since it will always arrive on the correct documents with just the e-mail field.

    In terms of memory usage (which is very important for databases like MongoDB) the e-mail index alone is much smaller as well.

    TL;DR : Use the index on e-mail address alone.

提交回复
热议问题