如何减少这个 mongo db find 查询的执行时间?

发布于 2025-01-15 02:01:29 字数 1178 浏览 0 评论 0原文

文档样本数据如下所示,

{
"_id" : ObjectId("62317ae9d007af22f984c0b5"),
"productCategoryName" : "Product category 1",
"productCategoryDescription" : "Description about product category 1",
"productCategoryIcon" : "abcd.svg",
"status" : true,
"productCategoryUnits" : [ 
    {
        "unitId" : ObjectId("61fa5c1273a4aae8d89e13c9"),
        "unitName" : "kilogram",
        "unitSymbol" : "kg",
        "_id" : ObjectId("622715a33c8239255df084e4")
    }
],
"productCategorySizes" : [ 
    {
        "unitId" : ObjectId("61fa5c1273a4aae8d89e13c9"),
        "unitName" : "kilogram",
        "unitSize" : 10,
        "unitSymbol" : "kg",
        "_id" : ObjectId("622715a33c8239255df084e3")
    }
],
"attributes" : [ 
    {
        "attributeId" : ObjectId("62136ed38a35a8b4e195ccf4"),
        "attributeName" : "Country of Origin",            
        "attributeOptions" : [],
        "isRequired" : true,
        "_id" : ObjectId("622715ba3c8239255df084f8")
    }
]
}

该集合已在“_id”中建立索引。没有子文档,执行时间会减少,但所有文档字段都是必需的。

db.getCollection('product_categories').find({})

该集合包含 30000 条记录,并且该查询执行时间超过 30 秒。那么如何解决这个问题呢。有人问我更好的解决方案。谢谢。

document sample data followed like this,

{
"_id" : ObjectId("62317ae9d007af22f984c0b5"),
"productCategoryName" : "Product category 1",
"productCategoryDescription" : "Description about product category 1",
"productCategoryIcon" : "abcd.svg",
"status" : true,
"productCategoryUnits" : [ 
    {
        "unitId" : ObjectId("61fa5c1273a4aae8d89e13c9"),
        "unitName" : "kilogram",
        "unitSymbol" : "kg",
        "_id" : ObjectId("622715a33c8239255df084e4")
    }
],
"productCategorySizes" : [ 
    {
        "unitId" : ObjectId("61fa5c1273a4aae8d89e13c9"),
        "unitName" : "kilogram",
        "unitSize" : 10,
        "unitSymbol" : "kg",
        "_id" : ObjectId("622715a33c8239255df084e3")
    }
],
"attributes" : [ 
    {
        "attributeId" : ObjectId("62136ed38a35a8b4e195ccf4"),
        "attributeName" : "Country of Origin",            
        "attributeOptions" : [],
        "isRequired" : true,
        "_id" : ObjectId("622715ba3c8239255df084f8")
    }
]
}

This collection has been indexed in "_id". without sub-documents execution time is reduced but all document fields are required.

db.getCollection('product_categories').find({})

This collection contains 30000 records and this query takes more than 30 seconds to execute. so how to solve this issue. Anybody ask me a better solution. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-22 02:01:29

索引和复合索引将使其使用缓存而不是每次查询时扫描文档。 30.000 个文档对于 MongoDB 来说不算什么,它可以在一秒钟内处理数百万个文档。如果在进程中填充这些字段,那么这对于查询来说是另一个繁重的操作。

查看您的架构是否有效构建,或者是否限制了与服务器的连接。其他需要考虑的事情是使用聚合管道仅投影您需要的字段。

Indexing and compound indexing will make it use cache instead of scanning document every time you query it. 30.000 documents is nothing to MongoDB, it can handle millions in a second. If these fields are populated in the process that's another heavy operation for the query.

See if your schema is efficiently structured or you're throttling your connection to the server. Other thing to consider is to project only the fields that you require, using aggregation pipeline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文