Mongodb的复合(组合)查询怎么看explain?

linkBG
  • 387

Mongodb的版本:v3.4

db.h.reateIndex({'port':1});
db.h.reateIndex({'geo.city':1});

在使用的时候

db.h.find({'port':'80','geo.city':'Taipei'},{'port':1,'geo.city':1,'geo.country':1})

explain('executionStats')

"executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 280952,
                "executionTimeMillis" : 10056,
                "totalKeysExamined" : 879232,
                "totalDocsExamined" : 879232,
                "executionStages" : {
                        "stage" : "PROJECTION",
                        "nReturned" : 280952,
                        "executionTimeMillisEstimate" : 9763,
                        "works" : 879233,
                        "advanced" : 280952,
                        "needTime" : 598280,
                        "needYield" : 0,
                        "saveState" : 6915,
                        "restoreState" : 6915,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "transformBy" : {
                                "port" : 1,
                                "geo.city" : 1,
                                "geo.country" : 1,
                        },
                        "inputStage" : {
                                "stage" : "FETCH",
                                "filter" : {
                                        "geo.city" : {
                                                "$eq" : "Taipei"
                                        }
                                },
                                "nReturned" : 280952,
                                "executionTimeMillisEstimate" : 9246,
                                "works" : 879233,
                                "advanced" : 280952,
                                "needTime" : 598280,
                                "needYield" : 0,
                                "saveState" : 6915,
                                "restoreState" : 6915,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "docsExamined" : 879232,
                                "alreadyHasObj" : 0,
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "nReturned" : 879232,
                                        "executionTimeMillisEstimate" : 466,
                                        "works" : 879233,
                                        "advanced" : 879232,
                                        "needTime" : 0,
                                        "needYield" : 0,
                                        "saveState" : 6915,
                                        "restoreState" : 6915,
                                        "isEOF" : 1,
                                        "invalidates" : 0,
                                        "keyPattern" : {
                                                "port" : 1
                                        },
                                        "indexName" : "port_1",
                                        "isMultiKey" : false,
                                        "multiKeyPaths" : {
                                                "port" : [ ]
                                        },
                                        "isUnique" : false,
                                        "isSparse" : false,
                                        "isPartial" : false,
                                        "indexVersion" : 2,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "port" : [
                                                        "[\"80\", \"80\"]"
                                                ]
                                        },
                                        "keysExamined" : 879232,
                                        "seeks" : 1,
                                        "dupsTested" : 0,
                                        "dupsDropped" : 0,
                                        "seenInvalidated" : 0
                                }
                        }
                }

像上面的例子,除了端口使用了索引吗?还是两个都使用了,如果两个都使用了,那还要建立复合索引吗?
谢谢

回复
阅读 2.1k
2 个回答

自己回答一下,上面有用到索引,但是只有一部分。
如果建立复合索引,将会更快的搜索到。
所以,组合搜索还是得用复合索引。

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

宣传栏