is it advised to created two separate indexes for the query to filter on one field and sort on another?

StackOverflow https://stackoverflow.com/questions/9059941

سؤال

I have a query that's something like the following

find( { a : { $gt: 3 } } ).sort( { b : -1 } )

Is it advised to create an index for field a and b separately?

هل كانت مفيدة؟

المحلول

No, with compound index query will work faster:

db.items.ensureIndex({a:1, b:-1});

Also you can make sure that your query use index with explain command.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top