简体   繁体   中英

MongoDb Indexing for MultiTenant

I have two collections, customSchemas, and customdata. Besides the default _id index, I've added the following indexes

db.customData.createIndex( { "orgId": 1, "contentType": 1 });
db.customSchemas.createIndex( { "orgId": 1, "contentType": 1 }, { unique: true });

I've decided to enforce orgId on all calls, so in my service layer, every query has an orgId in it, even the ones with ids, eg

db.customData.find({"_id" : ObjectId("557f30402598f1243c14403c"), orgId: 1});

Should I add an index that has both _id and orgId in it? Do the indexes I have currently help at all when I'm searching by both _id and orgId?

MongoDB 2.6+ provides index intersection feature that cover your case by using intersection of index _id {_id:1} and index prefix orgId in { "orgId": 1, "contentType": 1 }

So your query {"_id" : ObjectId("557f30402598f1243c14403c"), orgId: 1} should be covered by index already.

However, index intersection is less performant than a compound index on {"_id" : 1, orgId: 1} , as it comes with an extra step (intersection of the two sets). Hence, if this is a query that you use most of the time, creating the compound index on it is a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM