indexing - Clarification on sorting subsets in mongodb -
i have documents fields a, b, c, d, e, p
in mongodb. have indexes { a: 1 }
, { b: 1 },
{ c: 1 }, { d: 1 },
{ e: 1 }, { p: 1 }
, compound indexes { a: 1, b: 1, c: 1 }
. field p
represents position of fields.
if have select this: { a: 'something', b: 'something else', c: 'and again' }
, know use index { a: 1, b: 1, c: 1 }
search (obviously).
however, if want sort p
( { p: 1 }
), indexes used sort data? worried because can potentially have large dataset.
i read http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/ , cannot quite figure out use case (although it's there)
probably best said "can mongodb use 1 index match , 1 sort?" , answer no.
the thing "looking" in explain output see if index used sort value scanandorder show true
when index cannot used , false
can.
considering adapted sample, compound index on "a", "b", , "c" , separate index on "p", query this:
db.collection.find( { a: "a", b: "b" }).sort({ p: 1 }).explain()
would show compound index used query, not used sort. if had index created this:
db.collection.ensureindex({ a: 1, b: 1, p: 1 })
then index would used sort. in similar fashion, if has created this:
db.collection.ensureindex({ p: 1, a: 1, b: 1 })
then index selected , done sort action , used sort. not efficient of values a
, b
scanned in index determine result.
as final case small anount of documents , original compound selections optimizer pick "p" index, given small amount of documents scan fact use index affect sort operation.
Comments
Post a Comment