Andrey
Andrey

Reputation: 21285

Efficiency of indexing a field vs sub-field

I have this kind of documents stored in a collection:

{
    _id : ...
    sender: {memberid:<something>, name:<something>}
}

I index the collection by sub-field sender.memberid. I've read somewhere that indexing by subfiled is less efficient (performance-wise) than by a field, so changing document structure to:

{
    _id: ...
    senderid: ...
    sendername: ...
}

and indexing by senderid would result in faster inserts and retrievals, BUT the article didn't explain why. Is there really ANY difference in those two approaches, and if yes then why?

Upvotes: 3

Views: 254

Answers (1)

David Raab
David Raab

Reputation: 4488

Indexing itself only effects storing. Reading should only be faster, not slower. Storing gets a little speed penalty because the index needs to be updated. But this is for every index that you create.

If the index is on top-level or in some embedded document should not be a matter.

Otherwise this sounds more like premature optimization. Use the database like intended and creating embedded documents and indexing them is intended.

Upvotes: 2

Related Questions