search

Home  >  Q&A  >  body text

python - MongoDb startswith和全文索引问题

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

PHPzPHPz2805 days ago904

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:40:12

    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.

    reply
    0
  • Cancelreply