Compound key index vs. single key indexes

96 views
Skip to first unread message

Łukasz Kuczera

unread,
Mar 22, 2012, 6:02:50 PM3/22/12
to mongod...@googlegroups.com
Is there a performance difference for querying one field from compound index than from single key one ?
Its not clear in documentation at least not for me. 

Thanks


Max Schireson

unread,
Mar 22, 2012, 6:06:57 PM3/22/12
to mongod...@googlegroups.com

The compound index will be bigger and it may be be a hair slower but in real life I doubt you'll notice if the field you are querying is the first field. If it is the second field it won't be used for the most part.

-- Max

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

lkuczera

unread,
Mar 22, 2012, 6:24:13 PM3/22/12
to mongod...@googlegroups.com
Thanks for quick reply. I don't quite get it it is second field.
Basically I have several keys and query on them should I go for single key indexes or compound. I query mostly 3 fields and have 7 key index. Sounds like a candidate for single key index right ?


On Thursday, March 22, 2012 11:06:57 PM UTC+1, Max Schireson wrote:

The compound index will be bigger and it may be be a hair slower but in real life I doubt you'll notice if the field you are querying is the first field. If it is the second field it won't be used for the most part.

-- Max

On Mar 22, 2012 6:03 PM, "Łukasz Kuczera" <l.ku...@jextreme.pl> wrote:
Is there a performance difference for querying one field from compound index than from single key one ?
Its not clear in documentation at least not for me. 

Thanks


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@​googlegroups.com.

Max Schireson

unread,
Mar 22, 2012, 7:56:48 PM3/22/12
to mongod...@googlegroups.com

If you have an index on a, b, c and you query on a it will be fast; also fast if you are querying on a, b or a, b, c. Querying on just b or just c won't be fast.

If you are querying the 3 keys individually then a single compound index won't work well.

-- Max

To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/iwXVyLis0ggJ.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

lkuczera

unread,
Mar 22, 2012, 8:27:39 PM3/22/12
to mongod...@googlegroups.com
Ok cool and the other way if I query on three fields and have three different indexes on a, b and c ? Is that also slow.

Max Schireson

unread,
Mar 22, 2012, 9:02:41 PM3/22/12
to mongod...@googlegroups.com

If any one of the 3 fields you query on is selective on its own it will be fast. If not it won't.

For example if you are querying on userid, status with an index on each it will probably be fast because the index on userid will likely match only a few records.

That said you can replace the index on a with a, b c (and keep the indexes on b and c).

-- Max

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/pinCE-GbCpUJ.
Reply all
Reply to author
Forward
0 new messages