mongodb - MongoDB查询很慢(使用索引)
问题描述
这是我的查询
db.getCollection("images").find({
"$and": [
{ "text": { "$ne": null } },
{ "text": { "$ne": "" } },
],
"width":"3120"
})
结果explain
:
{
"queryPlanner" : {
"plannerVersion" : 1.0,
"namespace" : "mhxy.images",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"width" : {
"$eq" : "3120"
}
},
{
"text" : {
"$not" : {
"$eq" : null
}
}
},
{
"text" : {
"$not" : {
"$eq" : ""
}
}
}
]
},
"queryHash" : "7DCCCA85",
"planCacheKey" : "5D3F5173",
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"text" : {
"$not" : {
"$eq" : null
}
}
},
{
"text" : {
"$not" : {
"$eq" : ""
}
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"width" : 1.0
},
"indexName" : "idx_width",
"isMultiKey" : false,
"multiKeyPaths" : {
"width" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"width" : [
"[\"3120\", \"3120\"]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "FETCH",
"filter" : {
"width" : {
"$eq" : "3120"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"text" : 1.0
},
"indexName" : "idx_text",
"isMultiKey" : false,
"multiKeyPaths" : {
"text" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"text" : [
"[MinKey, undefined)",
"(null, \"\")",
"(\"\", MaxKey]"
]
}
}
},
{
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"text" : 1.0,
"width" : 1.0
},
"indexName" : "idx_text_width",
"isMultiKey" : false,
"multiKeyPaths" : {
"text" : [
],
"width" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"text" : [
"[MinKey, undefined)",
"(null, \"\")",
"(\"\", MaxKey]"
],
"width" : [
"[\"3120\", \"3120\"]"
]
}
}
}
]
},
"serverInfo" : {
"host" : "...",
"port" : 27017.0,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1.0
}
此集合大小超过 13Gb。和这样的文件:
{
"_id" : ObjectId("5dbac8665ef144e92017e190"),
"o_img" : BinData(0, "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIo.. 1103798 more bytes"),
"c_img" : BinData(0, "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIo.. 81058 more bytes"),
"date" : ISODate("2019-10-31T11:41:26.238+0000"),
"text" : "....",
"correct_text" : "....",
"bit" : "119667",
"width" : "3120",
"height" : "1440"
}
似乎查询过程的每一步都命中了索引。
但查询至少需要 20 秒。
更重要的是,我发现mongodb占用了我电脑90%以上的内存。
我的电脑内存太小了吗?我可以让 mongodb 不将二进制数据加载到内存中吗?
解决方案
推荐阅读
- css - 创建具有有限标准 CSS 属性的圆形图像?
- php - 更改 auth_redirect() 页面
- google-cloud-platform - GCP JSON API:如何向所有经过身份验证的用户授予存储桶编辑器权限 [创建和编辑自己的私有存储桶]
- javascript - Phaser 3 中的组或容器是否可拖动?
- java - JMeter 线程不会被终止
- c# - 在 Unity / Android 中从网络共享读取 PDF
- search - 如何在此处的地图中离线搜索位置?
- javascript - JavaScript / Three.js - 添加移动过渡曲线以改变 z 轴上的相机位置
- spring-boot - 模块化项目从 IntelliJ IDEA 运行 SpringBoot 应用程序失败
- python - PIL:如何获取 RGB 的调色板索引?