Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

index on some field sort descending/ascending issue #3

Open
shoujs opened this Issue · 2 comments

2 participants

@shoujs

I have a collection, "features", with 3 fields, name, active, weight,

I query the collection by weight descending:

db.features.find({active:true},{name:1, weight:1}).sort({weight:-1})

Dex gives me suggestion:

db['features'].ensureIndex({'active': 1, 'weight': 1}, {'background': true})

While I thought I should create index on weight descend:

db['features'].ensureIndex({'active': 1, 'weight': -1}, {'background': true})

I have post a question on Stackoverflow
in more details, would you please check it out?

@shoujs

after reading the answers in stackoverflow, i understand that the index works for both ascending and descending cases.

@shoujs shoujs closed this
@esedor
Owner

Hi!

Thanks for your question. There are a few situations where the order does matter, such as when you have a compound orderby clause combined with other factors (such as a query, with a return limit, on field A, ordered by { B: -1, C: 1}). For this and similar examples, an index on { A: 1, B: -1, C: 1 } should perform slightly more quickly and with slightly less memory used, because of a lefthanded optimization within the MongoDB index structure.

Right now, Dex blindly recommends ascending order for all indexes.

For your specific index, there is indeed not much difference in the order chosen. That said, I'd like to explore ways to include something about this in Dex's heuristics for generating recommendations!

@esedor esedor reopened this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.