Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB查询非常慢(使用索引)_Mongodb - Fatal编程技术网

MongoDB查询非常慢(使用索引)

MongoDB查询非常慢(使用索引),mongodb,Mongodb,这是我的问题 db.getCollection("images").find({ "$and": [ { "text": { "$ne": null } }, { "text": { "$ne": "" } }, ], "width":"3120" }) 和解释结果: { "queryPlanner" : { "plannerVersion" : 1.0, "namespace" : "mhx

这是我的问题

db.getCollection("images").find({
    "$and": [
        { "text": { "$ne": null } },
        { "text": { "$ne": "" } },
    ],
    "width":"3120"

})
解释
结果:

{ 
    "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不将二进制数据加载到内存中吗?

是的,t-“不等式运算符$ne不是很有选择性,因为它经常匹配索引的大部分。因此,在许多情况下,带有索引的$ne查询的性能可能不比必须扫描集合中所有文档的$ne查询好。另请参阅。”注意
MinKey
MaxKey
。正如手册所解释的,扫描整个索引要比扫描整个集合稍微好一些,但是您的查询条件基本上是未绑定的,这就是为什么需要花费很长时间的原因。值得注意的是,这里的索引似乎被定义为
text\u width
,这意味着
text
被标记为谓词(第一部分)。您的查询在第二部分中查找精确匹配。因此,相反,将索引定义为
width\u text
,将大大改善情况。