简体   繁体   English

在mongo分片环境中排序会降低性能

[英]Sorting in mongo sharding environments degrades performance

I have a query that performs something like this, 我有一个执行这样的事情的查询,

last_shipment_id = OrderDelivery.where(platform: 'business').desc(:shipment_id).limit(1).pluck(:shipment_id)[0]

It works great with only 1 ~ 5ms when I have correctly index with { platform: 1, shipment_id: -1 } without sharding environment in staging machine 当我正确索引{platform:1,shipment_id:-1}而没有在分段机器中分割环境时,它只有1~5ms的效果很好

However, our production is set up with 4 sharding mongo db, the result ends up in 1000 ~ 3000ms. 但是,我们的生产设置了4个分片mongo db,结果最终在1000~3000ms。

Does anyone know how this could happen or how might solve this case? 有谁知道这可能会如何发生或如何解决这种情况?

I've read about this slides https://www.slideshare.net/mongodb/how-queries-work-with-sharding 我已经阅读过这张幻灯片https://www.slideshare.net/mongodb/how-queries-work-with-sharding

Well, it's said on slide 13, but still not sure it has mentioned how to solve the case though. 好吧,它在幻灯片13中说,但仍然不确定它是否已经提到如何解决这个案例。

You don't mention what your shard key is, but this query has to be scattered to all three shards, and that means if any of the shards are slow, the overall result will be slow. 你没有提到你的分片键是什么,但是这个查询必须分散到所有三个分片,这意味着如果任何分片很慢,整体结果会很慢。 In the gist you include explain for shard1 (which is fast) but omit it for shard2 and shard3 and overall numbers show that it's slow on one of those shards. 在要点中,你包括shard1的解释(这是快速的),但是对于shard2和shard3省略了它,并且整数表明它在其中一个碎片上很慢。

This means either the optimal index is not present on one of those shards, or there is a different index present that's being picked even though it's suboptimal. 这意味着最佳索引不存在于其中一个分片上,或者存在不同的索引,即使它不是最理想的,也会被选中。 The solution in the former case is to build all correct indexes, the solution in the second case is to use hint with the query to force the use of the correct index. 前一种情况下的解决方案是构建所有正确的索引,第二种情况下的解决方案是使用查询的hint来强制使用正确的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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