如何提高 Mongo 文档检索性能
How to improve Mongo document retrieval performance
我正在从 Mongo 数据库中检索文档并将它们复制到内部存储。我发现检索和存储一百个这样的文档需要几秒钟以上的时间。我可以做些什么来提高性能吗?有些馆藏有超过 1000 份文件。这是我所拥有的(写在 vb.net)
' get the documents from collection "reqitems" and put them in "collection"
Dim collection As IFindFluent(Of BsonDocument, BsonDocument) = _
reqitems.Find(Builders(Of BsonDocument).Filter.Empty)
ReDim model.ReqItems(TotalCollection.ToList.Count) ' storage for the processed documents
For Each item As BsonDocument In TotalCollection.ToList()
' note: given a string a=x, "GetRHS" returns x
Dim parentuid As String = GetRHS(item.GetElement("parentuid").ToString)
Dim nodename As String = GetRHS(item.GetElement("nodename").ToString)
' .... about a dozen of these elements
' .... process the elements and copy them to locations in model.ReqItems
next
您可以为 collection 添加索引(如果您还没有这样做的话)。请参考:https://docs.mongodb.com/manual/indexes/
此外,我建议 运行 带有执行统计信息的特定 Mongodb 查询。例如:db.mycollection.find().explain("executionStats");
这将为您提供有关查询性能的更多统计信息。 https://docs.mongodb.com/manual/reference/explain-results/#executionstats
添加索引并没有真正帮助。减慢速度的是一次访问文档中的元素(发布代码中的 GetRHS)。因此,作为修复,我将文档转换为字符串,然后解析字符串以获取关键字值对。希望我的发现可以帮助遇到同样问题的人
我正在从 Mongo 数据库中检索文档并将它们复制到内部存储。我发现检索和存储一百个这样的文档需要几秒钟以上的时间。我可以做些什么来提高性能吗?有些馆藏有超过 1000 份文件。这是我所拥有的(写在 vb.net)
' get the documents from collection "reqitems" and put them in "collection"
Dim collection As IFindFluent(Of BsonDocument, BsonDocument) = _
reqitems.Find(Builders(Of BsonDocument).Filter.Empty)
ReDim model.ReqItems(TotalCollection.ToList.Count) ' storage for the processed documents
For Each item As BsonDocument In TotalCollection.ToList()
' note: given a string a=x, "GetRHS" returns x
Dim parentuid As String = GetRHS(item.GetElement("parentuid").ToString)
Dim nodename As String = GetRHS(item.GetElement("nodename").ToString)
' .... about a dozen of these elements
' .... process the elements and copy them to locations in model.ReqItems
next
您可以为 collection 添加索引(如果您还没有这样做的话)。请参考:https://docs.mongodb.com/manual/indexes/
此外,我建议 运行 带有执行统计信息的特定 Mongodb 查询。例如:db.mycollection.find().explain("executionStats");
这将为您提供有关查询性能的更多统计信息。 https://docs.mongodb.com/manual/reference/explain-results/#executionstats
添加索引并没有真正帮助。减慢速度的是一次访问文档中的元素(发布代码中的 GetRHS)。因此,作为修复,我将文档转换为字符串,然后解析字符串以获取关键字值对。希望我的发现可以帮助遇到同样问题的人