MongoDB query on all sharded collections without s

2019-03-06 14:36发布

问题:

I have several shard-(ed) collections. The collection is user requests. and the shard key is User Id.

I have a field named "Execution Time" and I want query all the requests in a period of time (lte and gte).

The index is with the shard key, but my query is without. I would like not to put all the shard Key in query with a "in" operator because I have a 1000 shard keys (users).. futher more to do that i need to get all user ids on every query - it means 2 queries each time instead of 1.

But still i want to use an index.. what option is to add userId > 0 < maxUserId to the query?

What is the right approach?

Thanks in advance

回答1:

For ideal performance, shard keys should be chosen in a way the router (mongos) can tell which shard will have the data for the most common queries. This is only possible when the find-query has a criteria which is also the shard-key.

But in this case it is impossible for the router to tell which shard has the data for the query. It is not unlikely that there are relevant results on every shard. In that case the query needs to be forwarded to all shards, which will process it simultaneously. But when you have an appropriate index, this will help them doing so.