简体   繁体   English

MongoDB $ IN查询性能问题

[英]MongoDB $IN query performance issue

thanks all for your help. 感谢你的帮助。

I have a collection with this stats I got 700 Milions of records that look something like this 我有一个具有此统计信息的集合,我得到了700百万条看起来像这样的记录

db.flight_availabillity.findOne() { "_id" : ObjectId("5226465fc3b53d4f249c19fc"), "flight_id" : 9803, "arrival" : 1384819200, "duration" : 1, "capacity" : 1, "rooms" : 1, "min_price" : 163, "min_price_packaged" : 50, "rates_has_wifi" : 1, "rates_has_baby_cot" : 1, "rates_has_pets_allow" : 1, "erank" : 0.25 } db.flight_availabillity.findOne(){“ _id”:ObjectId(“ 5226465fc3b53d4f249c19fc”),“ flight_id”:9803,“到达”:1384819200,“持续时间”:1,“容量”:1,“房间”:1,“ min_price”:163,“ min_price_packaged”:50,“ rates_has_wifi”:1,“ rates_has_baby_cot”:1,“ rates_has_pets_allow”:1,“ erank”:0.25}

When i do queries i do only on 4 fields so i build a index that look like this db.flight_availabillity.ensureIndex({"flight_id":1,"arrival":1,"duration":1,"capacity":1,"rooms":1}) 当我进行查询时,我只对4个字段进行查询,因此我建立了一个类似于db.flight_availabillity.ensureIndex({“ flight_id”:1,“到达”:1,“持续时间”:1,“容量”:1的索引, “房间”:1})

The problem : When sending only 1 flight id find({"flight_id":{$in:[236]}) The results is blasing fast 问题:仅发送1个航班ID时,find({“ flight_id”:{$ in:[236]})结果很快

when using several flight ids find({"flight_id":{$in:[236,232,545,757]}) ( And i can have up to 1000 flight ids in the queries ) . 当使用多个航班ID时,find({“ flight_id”:{$ in:[236,232,545,757]})(并且查询中最多可以有1000个航班ID)。 i get slower results. 我得到较慢的结果。

Here is an explain of one of them that took 3.5 seconds , but i had also severals with 10 seconds 这是其中一个用了3.5秒的解释,但我也用了10秒

db.flight_availabillity.find({"flight_id":{$in:[333,207731,33993,277,127,183345,169019,156473,92715,5046,2927,2473,2112,2024,281,264,185,125,95,80,208065,183074,31774,359,314,64010,56170,5107,4673,147,115571,214,101564,287,66356,128,194487,100,207984,66353]},"arrival":1384387200,"duration":1,"capacity":1,"rooms":1}).explain() { "cursor" : "BtreeCursor flight_id_1_arrival_1_departure_1_capacity_1_rooms_1 multi", "isMultiKey" : false, "n" : 40, "nscannedObjects" : 240, "nscanned" : 358, "nscannedObjectsAllPlans" : 597, "nscannedAllPlans" : 715, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 4, .... } db.flight_availabillity.find({“ flight_id”:{$ in:[333,207731,33993,277,127,183345,169019,156473,92715,5046,2927,2473,2112,2024,281,264,185,125,95,80,208065, 183074,31774,359,314,64010,56170,5107,4673,147,115571,214,101564,287,66356,128,194487,100,207984,66353]},“到达”:1384387200,“持续时间”:1, “ capacity”:1,“ rooms”:1})。explain(){“ cursor”:“ BtreeCursor flight_id_1_arrival_1_departure_1_capacity_1_rooms_1 multi”,“ isMultiKey”:false,“ n”:40,“ nscannedObjects”:240,“ nscanned”: 358,“ nscannedObjectsAllPlans”:597,“ nscannedAllPlans”:715,“ scanAndOrder”:false,“ indexOnly”:false,“ nYields”:0,“ nChunkSkips”:0,“ millis”:4,....}

What i miss ? 我想念什么? how to query it and get fast results ? 如何查询并获得快速结果?

Thanks ! 谢谢 !

In some MongoDB versions $in does not use index - also Mongo has a limitation of using more than one index for the same query. 在某些MongoDB版本中,$ in不使用索引-Mongo也有一个限制,即对同一查询使用多个索引。

You query comprises flight_id, arrival, duration, capacity and rooms. 您查询的内容包括flight_id,到达时间,持续时间,容量和房间。 Try to setup an index with arrival, duration, capacity and rooms, which shall provide you with good index on a selective criteria instead of putting all fields. 尝试设置一个包含到达时间,持续时间,容纳人数和房间的索引,这将为您提供具有选择性标准的良好索引,而不是将所有字段都放入。

The flight_id will be just a final selection, after the hard work has already been done by the selective criteria. 在根据选择标准进行了艰苦的工作之后,flight_id才是最终选择。

Also it does not help that indexBound was not pasted, it could give clues whether the index composition is optimal or not. 同样,如果没有粘贴indexBound也无济于事,它可能会为线索组成是否最佳提供线索。

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

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