Skip to content

index on some field sort descending/ascending issue #3

Open

Description

@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?

Activity

  1. shoujs commented on Aug 24, 2012

    @shoujs
    Author

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

  2. esedor commented on Sep 5, 2012

    @esedor
    Contributor

    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!

  3. reopened this on Sep 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions