mongodb order of AND clauses -
mongodb order of AND clauses -
when running , query on mongodb, there performance effect order of clauses? tests seem indicate 'no', wonder whether i'm missing because saw articles claiming contrary.
e.g. consider 'blog' documents:
{_id:1, type:'personal', author:'mr a', ...} {_id:2, type:'commercial', author:'mr a', ...} {_id:3, type:'personal', author:'mr b', ...} {_id:4, type:'commercial', author:'mr b', ...} {_id:5, type:'personal', author:'mr c', ...} {_id:6, type:'commercial', author:'mr c', ...} {_id:7, type:'personal' , author:'mr d', ...} {_id:8, type:'commercial', author:'mr d', ...} indexes: col.ensureindex({type:1}) col.ensureindex({author:1})
now can run , queries different ordering:
col.find({type:'commercial', author:'mr a'}) col.find({author:'mr a' , type:'commercial' })
i tried running 'explain' , got impression take 'author' index regardless of order (because "mr a" has 2 blogs, while "commercial" appears in 4 blogs). documented feature select "most narrowing" index, regardless of clause order? much.
consider using compound index if query more 1 field.
db.col.ensureindex( { "type": 1, "author": 1 } )
lookup docs: http://docs.mongodb.org/manual/core/index-compound/
the order of fields in compound index important. in previous example, index contain references documents sorted first values of item field and, within each value of item field, sorted values of stock field. see sort order more information.
mongodb
Comments
Post a Comment