1

Is it possible to delegate responsibility for specific indexes for a collection to specific shards in a mongoDB database?

For instance, say I have a collection called 'books':

{ title: <string>, pages_in_book: <number>, price: <number>, author: <string> }

And say I want 3 indexes for this collection: one for the price, one for pages_in_book, and one for author.

Also say I have 3 shards.

Can I have each of these shards be responsible for updating one index each when a new document is inserted into the 'books' collection?

I want to do this because I plan to make a write-heavy application. User actions would initiate the creation of multiple documents at once and each document would need to be added to multiple indices like above (but five or six indices in my real case). I could distribute the writes using a shard key (with luck here, the documents' writes and it's writes to the collection's indices would be distributed evenly to the different shards but I could get unlucky if no good shard keys) or distribute the writes so that each shard is assigned an equal amount of indexes to be responsible for (extremely more likely for there to be a near even distribution of writes this way???)

1
  • Each shard will have to maintain all indexes. With sharding you will achieve to maintain smaller "local" indexes but each shard must have all indexes. Assuming you searching for price:{$gt:10}. Most probably this query will hit all three shards. If you are missing the {price:1} index on one shard the query will perform a full collection scan which will slow down your database. If I understand your question you want to scale writes. The best way is a shard key like {_id:hashed} or any other field that can achieve write scale and a minimal set of indexes - consider partial indexes as well.
    – Antonios
    Commented Mar 7, 2017 at 22:22

1 Answer 1

0

As Antonios wrote, you cannot distribute different indexes to different shards. To distribute load, you should select good shard key and this case key what is used most (best situation is if all queries would always use "some" key) it should be "hashed". ( {"some":"hashed"} )

Check hased sharding from here!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.