简体   繁体   English

MongoDB Java驱动程序的索引无法提高性能

[英]Indexes with MongoDB Java Driver not improving performance

In my database, i have an index with 100K documents with the following structure: 在我的数据库中,我有一个包含以下结构的100K文档的索引:

{ "_id" : ObjectId("56f2ce94ef4c3043f12141b8"), "a" : "aaaa", "b" : "bbbb", "c" : "cccc" ...}

On Java, after inserting, i call the function: 在Java上,插入后,我调用函数:

myCollection.createIndex(new Document("a", 1)); 

and in order to query: 并为了查询:

 FindIterable<Document> iterable = 
DB.getCollection(collection).find(dbobj);

After several tests, the performance with or without query is the same. 经过几次测试,有或没有查询的性能是相同的。 I'm open to give extra information about my operations. 我愿意提供有关我的操作的更多信息。
The explain command gives me: 说明命令给我:

 {
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "db.MyCollection",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "a" : /^aaaa.*/i
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "filter" : {
                    "a" : /^aaaa.*/i
                },
                "keyPattern" : {
                    "a" : 1
                },
                "indexName" : "a_1",
                "isMultiKey" : false,
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 1,
                "direction" : "forward",
                "indexBounds" : {
                    "Modality" : [
                        "[\"\", {})",
                        "[/^aaaa.*/i, /^aaaa.*/i]"
                    ]
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "ok" : 1
}

在MongoDB中,如果要将索引用于不区分大小写的查询,则需要使用文本索引

As stated in the comments of the question, MongoDB gets slow when all the documents does not fit in memory, and it gets really slow when the indexed fields do not fit in memory. 如问题评论中所述,当所有文档都无法容纳在内存中时,MongoDB会变慢,而当索引字段无法容纳在内存中时,MongoDB会变得非常慢。 This is because MongoDB has to resort to memory paging . 这是因为MongoDB必须诉诸内存分页 This means MongoDB saves some content of the memory on your HDD, and retrieving this is slow. 这意味着MongoDB在您的HDD上保存了部分内存内容,并且检索速度很慢。 Basically, you are losing the advantage of having indexed fields. 基本上,您将失去拥有索引字段的优势。

Tactics to avoid that are to: 避免这种情况的策略是:

  • Increase the amount of RAM you have on your server 增加服务器上的RAM量
  • Use a sharded configuration containing multiple servers 使用包含多个服务器的分片配置
  • Limit data duplication across documents 限制跨文档的数据重复
  • Limit the indexed fields 限制索引字段

You can observe the amount of memory taken by MongoDB by using the db.my_collection.stats() command on the MongoDB console. 您可以使用MongoDB控制台上的db.my_collection.stats()命令来观察MongoDB占用的内存量。 This should be the output: 这应该是输出:

{
   "ns" : "guidebook.restaurants",
   "count" : 25359,
   "size" : 10630398,
   "avgObjSize" : 419,
   "storageSize" : 4104192
   "capped" : false,
   "wiredTiger" : {
         "metadata" : {
            "formatVersion" : 1
         },
         [...]
      "nindexes" : 4,
      "totalIndexSize" : 626688,
      "indexSizes" : {
         "_id_" : 217088,
         "borough_1_cuisine_1" : 139264,
         "cuisine_1" : 131072,
         "borough_1_address.zipcode_1" : 139264
      },
      "ok" : 1
 }

storageSize shows the amount of memory used to store documents in bytes, and totalIndexSize shows the amount of memory used to store indexed values in bytes. storageSize显示用于存储文档的内存量(以字节为单位), totalIndexSize显示用于存储索引值的内存量(以字节为单位)。 You can see which indexed fields takes most of the space in the indexSizes sub-document. 您可以在indexSizes子文档中查看哪些索引字段占用了大部分空间。

Ideally, you want to have more RAM than storageSize + totalIndexSize , but you really should have more RAM than totalIndexSize . 理想情况下,您想拥有比storageSize + totalIndexSize更多的RAM,但实际上您应该拥有比totalIndexSize更多的RAM。

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

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