I'm newbie to mongodb and I need help about increasing performance for queries using compound indexes.
I am using pagination using "_id" field. Query:
db.data.find( {"$or": [ { "cat": "5" }, {"cat": "6" } ] }, {"style": "3"},
{"area": "London"}, {"_id": {"$lt": 103}).sort( { "_id": 1 } ).limit(30)
style
and area
can be optional so these are also possible.
db.data.find( {"$or": [ { "cat": "5" }, {"cat": "6" } ] },
{"area": "London"}, {"_id": {"$lt": 103}).sort( { "_id": 1 } ).limit(30)
db.data.find( {"$or": [ { "cat": "5" }, {"cat": "6" } ] }, {"style": "3"},
{"_id": {"$lt": 103}).sort( { "_id": 1 } ).limit(30)
db.data.find( {"$or": [ { "cat": "5" }, {"cat": "6" } ] },
{"_id": {"$lt": 103}).sort( { "_id": 1 } ).limit(30)
Will these queries be ok with this compound index, or do I need more additional index?
{ "cat": 1, "style": 1, "area": 1, "_id": 1 }
Edit
I'm not sure which of these index would be efficient for queries.
{ "cat": 1, "style": 1, "area": 1, "_id": 1 } or
{ "_id": 1, "cat": 1, "style": 1, "area": 1 }
Number of styles: 16
Number of Areas: 50
Number of id: 10 million
use
ObjectId has this structure:
because the
timestamp
comes first in above structure, it will sort in roughly insertion order.When id came first in index, it will reduce your looking objects.