mongodb compound index over extending -
i have question regarding compound indexes cant seem find, or maybe have misunderstood.
lets have created compound index {a:1, b:1, c:1}. should make according http://docs.mongodb.org/manual/core/indexes/#compound-indexes
the following queries fast.
db.test.find({a:"a", b:"b",c:"c"}) db.test.find({a:"a", b:"b"}) db.test.find({a:"a"})
as understand order of query important, explicit subset of {a:"a", b:"b",c:"c"} order important?
lets query
db.test.find({d:"d",e:"e",a:"a", b:"b",c:"c"})
or
db.test.find({a:"a", b:"b",c:"c",d:"d",e:"e"})
will these render useless specific compound index?
compound indexes in mongodb work on prefix mechanism whereby a
, {a,b}
considered prefixes, order, of compound index, however, order of fields in query not matter.
so lets take examples:
db.test.find({d:"d",e:"e",a:"a", b:"b",c:"c"})
will use index:
db.ghghg.find({d:1,e:1,a:1,c:1,b:1}).explain() { "cursor" : "btreecursor a_1_b_1_c_1", "ismultikey" : false, "n" : 1, "nscannedobjects" : 1, "nscanned" : 1, "nscannedobjectsallplans" : 2, "nscannedallplans" : 2, "scanandorder" : false, "indexonly" : false, "nyields" : 0, "nchunkskips" : 0, "millis" : 0, "indexbounds" : { "a" : [ [ 1, 1 ] ], "b" : [ [ 1, 1 ] ], "c" : [ [ 1, 1 ] ] }, "server" : "ubuntu:27017" }
since a
, b
there.
db.test.find({a:"a", b:"b",c:"c",d:"d",e:"e"})
depends upon selectivity , cardinality of d
, e
. use compound index whether use in such manner allows decent performance of query depends heavily upon what's in there.