python - MongoDb startswith和全文索引问题
PHPz
PHPz 2017-04-18 09:38:12
0
1
856

场景是目前有个Mongo的collection中的name字段,name字段长度正常在4-10个中文左右,25W数据量。如果我用startswith匹配name查询的话性能会有怎样的压力呢?
如果给name字段加上全文索引又会有什么样的好处和坏处呢?

PHPz
PHPz

学习是最好的投资!

reply all(1)
刘奇

If there is a common index, queries with fixed prefix can use the index. But I don’t know what startswith method you are talking about. If it is in the driver, it depends on the driver and what its documentation says. The prefix I am referring to is the matching of regular expressions starting with ^, such as:

db.collection.find({name: /^张/})

If you look at the execution plan, you can see IX_SCAN:

db.collection.find({name: /^张/}).explain(true)

Full-text index is another thing. Simply put, you don’t need to consider whether the prefix is ​​fixed, but the basic unit of full-text index is , not a word. So the following sentence:

First Division is a good school

When searching, you can directly find the word you want, such as:

db.collection.find({ $text: { $search: "好学校" } })

But if you directly search for "school", you may not be able to find it:

db.collection.find({ $text: { $search: "校" } })  // 无结果

Of course, this is related to how different word segmentation engines are divided. The example is just to illustrate the limitations of full-text indexing.
In addition, articles, quantifiers and other words that have no actual meaning will also be filtered out, such as "人":

db.collection.find({ $text: { $search: "个" } })  // 无结果

In addition, you may also notice that the above query does not mention name this field at all, because there can only be one full-text index on a collection.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!