首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mongodb包含查询空结果慢

mongodb包含查询空结果慢
EN

Stack Overflow用户
提问于 2020-12-28 20:09:10
回答 1查看 83关注 0票数 0

我有大约1000万的MongoDB文档。我试图在db db.outMessage.find({ "text" : /.*m.*/})中搜索文本,但是花费了太长时间(大约30秒)没有结果,但是如果我搜索现有的文本,它只用了不到一秒。我试着把索引放在text上,结果也一样。

代码语言:javascript
运行
复制
db.outMessage.find({ "text" : /.*m.*/}).explain(true)
代码语言:javascript
运行
复制
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "notification_center.outMessage",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "text" : {
                                "$regex" : ".*m.*"
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "filter" : {
                                        "text" : {
                                                "$regex" : ".*m.*"
                                        }
                                },
                                "keyPattern" : {
                                        "text" : 1
                                },
                                "indexName" : "text",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "text" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "text" : [
                                                "[\"\", {})",
                                                "[/.*m.*/, /.*m.*/]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 0,
                "executionTimeMillis" : 14354,
                "totalKeysExamined" : 10263270,
                "totalDocsExamined" : 0,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 12957,
                        "works" : 10263271,
                        "advanced" : 0,
                        "needTime" : 10263270,
                        "needYield" : 0,
                        "saveState" : 80258,
                        "restoreState" : 80258,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 0,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "filter" : {
                                        "text" : {
                                                "$regex" : ".*m.*"
                                        }
                                },
                                "nReturned" : 0,
                                "executionTimeMillisEstimate" : 12461,
                                "works" : 10263271,
                                "advanced" : 0,
                                "needTime" : 10263270,
                                "needYield" : 0,
                                "saveState" : 80258,
                                "restoreState" : 80258,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "text" : 1
                                },
                                "indexName" : "text",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "text" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "text" : [
                                                "[\"\", {})",
                                                "[/.*m.*/, /.*m.*/]"
                                        ]
                                },
                                "keysExamined" : 10263270,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                },
                "allPlansExecution" : [ ]
        },
        "serverInfo" : {
                "host" : "acsdptest.arabiacell.net",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
EN

Stack Overflow用户

发布于 2020-12-29 10:21:59

该索引将本质上是text字段的所有值的列表,按照词典编排顺序,即按第一个字母排序。

由于查询执行器无法预测哪些值可能包含'm',因此它必须检查所有索引条目。

在这个查询中,这意味着如果索引不在缓存中,那么在从磁盘读取索引键之后,将检查10,263,270个索引键。

如果这实际上是关键字搜索,而不是单字母匹配,则可以使用$text查询运算符,这需要一个text index

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65477386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档