mongodb查询字符串字段,索引优化无效?
发布于 3 年前 作者 maxxfire 3285 次浏览 来自 问答

数据表person中有一个name字段存入名字信息,类型是字符串。

db.person.find().pretty(); { “_id” : ObjectId(“5983d9d7b7f1aa5c1aafeafc”), “name” : “张三” } { “_id” : ObjectId(“5983d9d7b7f1aa5c1aafeafd”), “name” : “李四” } { “_id” : ObjectId(“5983d9d8b7f1aa5c1aafeafe”), “name” : “王五” } { “_id” : ObjectId(“5983d9e2b7f1aa5c1aafeaff”), “name” : “赵六” } { “_id” : ObjectId(“5983d9ebb7f1aa5c1aafeb00”), “name” : “马七” }

为其创建了索引:db.person.createIndex({name: 1}, {collation: {locale: “zh”}});

db.person.stats(); 查看索引信息: “indexSizes” : { “id” : 16384, “name_1” : 16384 },

前缀查找:> db.person.find({name: /^李/}).explain(‘allPlansExecution’);
“executionStats” : { “executionSuccess” : true, “nReturned” : 1, “executionTimeMillis” : 0, “totalKeysExamined” : 5, “totalDocsExamined” : 5, “executionStages” : { “stage” : “FETCH”, “filter” : { “name” : { “$regex” : “^李” } }, “nReturned” : 1, “executionTimeMillisEstimate” : 0, “works” : 6, “advanced” : 1, “needTime” : 4, “needYield” : 0, “saveState” : 0, “restoreState” : 0, “isEOF” : 1, “invalidates” : 0, “docsExamined” : 5, “alreadyHasObj” : 0, “inputStage” : { “stage” : “IXSCAN”, “nReturned” : 5, “executionTimeMillisEstimate” : 0, “works” : 6, “advanced” : 5, “needTime” : 0, “needYield” : 0, “saveState” : 0, “restoreState” : 0, “isEOF” : 1, “invalidates” : 0, “keyPattern” : { “name” : 1 }, “indexName” : “name_1”, “collation” : { “locale” : “zh”, “caseLevel” : false, “caseFirst” : “off”, “strength” : 3, “numericOrdering” : false, “alternate” : “non-ignorable”, “maxVariable” : “punct”, “normalization” : false, “backwards” : false, “version” : “57.1” }, “isMultiKey” : false, “multiKeyPaths” : { “name” : [ ] }, “isUnique” : false, “isSparse” : false, “isPartial” : false, “indexVersion” : 2, “direction” : “forward”, “indexBounds” : { “name” : [ “[”", {})", “[/^李/, /^李/]” ] }, “keysExamined” : 5, “seeks” : 1, “dupsTested” : 0, “dupsDropped” : 0, “seenInvalidated” : 0 } }, “allPlansExecution” : [ ] },

完全查找:> db.person.find({name: ‘李四’}).explain(‘allPlansExecution’);
“executionStats” : { “executionSuccess” : true, “nReturned” : 1, “executionTimeMillis” : 0, “totalKeysExamined” : 0, “totalDocsExamined” : 5, “executionStages” : { “stage” : “COLLSCAN”, “filter” : { “name” : { “$eq” : “李四” } }, “nReturned” : 1, “executionTimeMillisEstimate” : 0, “works” : 7, “advanced” : 1, “needTime” : 5, “needYield” : 0, “saveState” : 0, “restoreState” : 0, “isEOF” : 1, “invalidates” : 0, “direction” : “forward”, “docsExamined” : 5 }, “allPlansExecution” : [ ] },

前缀查找还是扫描了所有记录?,完全查找竟然没有启用索引?

4 回复

@haozxuan 用了这种索引测试了一下,发现无法用 前缀查找,也无法中间查找,只能查全部:

db.person.getIndices(); 查看text索引: [ { “v” : 2, “key” : { “_id” : 1 }, “name” : “id”, “ns” : “libra.person” }, { “v” : 2, “key” : { “_fts” : “text”, “_ftsx” : 1 }, “name” : “name_text”, “ns” : “libra.person”, “language_override” : “simplified chinese”, “weights” : { “name” : 1 }, “default_language” : “english”, “textIndexVersion” : 3 } ] db.person.find( { $text: { $search: “张” } } ); 返回空 db.person.find( { $text: { $search: “三” } } ); 返回空 db.person.find( { $text: { $search: “张三” } } );
{ “_id” : ObjectId(“5983d9d7b7f1aa5c1aafeafc”), “name” : “张三” }

@maxxfire 你可以db.person.find({$test:{$search:“张”}}).explain() 看下,即使你强制在创建索引的事情修改语言格式,但是在查询时,使用的仍旧是English,所以你是查询不出来的。至于原因,参见截图: image.png

@haozxuan 企业版是付费的吧,不过我想到一个曲线救国的办法来解决 中文索引的问题。 就是先把中文转成拼音,存储到另外一个字段,这个就变成搜索英文了,索引就可以生效了。

回到顶部