Mogodb使用了文本索引,就不能使用复合索引?

linkBG
  • 387

是不是如果使用了文本索引,就不能使用复合索引?
因为我用了下面的查询语句但是只做了文本索引:

db.tests.find({$text:{$search:'zhangshan'},'age':5,'city':'beijing'}

通过explain查看,他是做了text的索引。然后再从这个索引中去过滤出其他条件的!
那么我该怎么做才能全部走索引呢?
我想到把所有字段建立一个text索引。因为数据量大,所以没有尝试,想问问这个可行吗?
谢谢


补充一下explain()

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 50424,
        "executionTimeMillis" : 510228,
        "totalKeysExamined" : 83546,
        "totalDocsExamined" : 167092,
        "executionStages" : {
            "stage" : "FETCH",
            "filter" : {
                "$and" : [
                    {
                        "age" : {
                            "$eq" : "5"
                        }
                    },
                    {
                        "city" : {
                            "$eq" : "beijing"
                        }
                    }
                ]
            },
            "nReturned" : 50424,
            "executionTimeMillisEstimate" : 509543,
            "works" : 167095,
            "advanced" : 50424,
            "needTime" : 116670,
            "needYield" : 0,
            "saveState" : 24418,
            "restoreState" : 24418,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 83546,
            "alreadyHasObj" : 83546,
            "inputStage" : {
                "stage" : "TEXT",
                "nReturned" : 83546,
                "executionTimeMillisEstimate" : 509362,
                "works" : 167095,
                "advanced" : 83546,
                "needTime" : 83548,
                "needYield" : 0,
                "saveState" : 24418,
                "restoreState" : 24418,
                "isEOF" : 1,
                "invalidates" : 0,
                "indexPrefix" : {
                    
                },
                "indexName" : "banner_text",
                "parsedTextQuery" : {
                    "terms" : [
                        "ii"
                    ],
                    "negatedTerms" : [ ],
                    "phrases" : [ ],
                    "negatedPhrases" : [ ]
                },
                "textIndexVersion" : 3,
                "inputStage" : {
                    "stage" : "TEXT_MATCH",
                    "nReturned" : 83546,
                    "executionTimeMillisEstimate" : 509320,
                    "works" : 167095,
                    "advanced" : 83546,
                    "needTime" : 83548,
                    "needYield" : 0,
                    "saveState" : 24418,
                    "restoreState" : 24418,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "docsRejected" : 0,
                    "inputStage" : {
                        "stage" : "TEXT_OR",
                        "nReturned" : 83546,
                        "executionTimeMillisEstimate" : 509310,
                        "works" : 167095,
                        "advanced" : 83546,
                        "needTime" : 83548,
                        "needYield" : 0,
                        "saveState" : 24418,
                        "restoreState" : 24418,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 83546,
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "nReturned" : 83546,
                            "executionTimeMillisEstimate" : 1642,
                            "works" : 83547,
                            "advanced" : 83546,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 24418,
                            "restoreState" : 24418,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "keyPattern" : {
                                "_fts" : "text",
                                "_ftsx" : 1
                            },
                            "indexName" : "name_text",
                            "isMultiKey" : true,
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "backward",
                            "indexBounds" : {
                                
                            },
                            "keysExamined" : 83546,
                            "seeks" : 1,
                            "dupsTested" : 83546,
                            "dupsDropped" : 0,
                            "seenInvalidated" : 0
                        }
                    }
回复
阅读 1.8k
2 个回答

试着回答一下:

1、首先想看看你的复合索引是具体怎么建立的?

2、执行的结果是按照选择的执行计划执行的;如果性能不满意,可以使用hint来指定您所创建的复合索引。

Love MongoDB! Have fun!


2017MongoDB中文社区北京用户组大会在即,2017年6月3日 13:00-18:00

众多精彩分享!值得参与!

图片描述

MongoDB中複合索引可以包含一個 文本 索引鍵,和其它遞增/遞減索引鍵。
具体讨论,下面网址有,供您参考:
http://forum.foxera.com/mongo...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏