chinafengzhao
V2EX  ›  数据库

大数据量模糊匹配有快速响应的优化方案吗?

  •  1
     
  •   chinafengzhao · Apr 14, 2021 · 3718 views
    This topic created in 2041 days ago, the information mentioned may be changed or developed.

    背景

    MySQL,单表数据量超过 2000 万,需求是要求快速模糊查询。

    select * from xxx where aaa like '%yyyyy' ;

    有快速优化方案吗?

    31 replies  •  2021-04-15 06:13:39 +08:00
    johnsona
    johnsona
    Apr 14, 2021 via iPhone❤️1
    1
    加一列把原来的列 reverse 一下 这样就能用到索引了( bushi
    lscexpress
    lscexpress
    Apr 14, 2021
    2
    使用 fulltext
    Justin13
    Justin13
    Apr 14, 2021 via Android
    3
    对实时性要求不高可以加缓存,把查询结果存储起来。
    要求实时但是查询是固定的话,可以加一索引列,存储查询的结果,写入时一并添加。
    如果什么都不能做,只能改查询,那就洗洗睡吧。
    simon1991
    simon1991
    Apr 14, 2021
    4
    上 elasticsearch ()
    MinQ
    MinQ
    Apr 14, 2021
    5
    上 ES 或者 Solr 吧
    MinQ
    MinQ
    Apr 14, 2021
    6
    而且像 xxxx%的前缀查询才能用索引,%yyyyy 建议直接放弃治疗
    3dwelcome
    3dwelcome
    Apr 14, 2021
    7
    @johnsona "加一列把原来的列 reverse 一下 这样就能用到索引了( bushi"

    我觉得 reverse 这种,新数据库已经自己优化掉了。要不然 2000 万条数据无索引暴力查询,不知道慢到什么程度了。
    chinafengzhao
    chinafengzhao
    OP
    Apr 14, 2021
    8
    不好意思,查询逻辑要求的是 like %企业名关键词%
    MinQ
    MinQ
    Apr 14, 2021
    9
    @3dwelcome mysql 是没有优化的,需要自己新增,比如取前 N 个 reverse 然后存下来
    MinQ
    MinQ
    Apr 14, 2021
    10
    @chinafengzhao 还是 fulltext 或者 ES 吧
    chinafengzhao
    chinafengzhao
    OP
    Apr 14, 2021
    11
    我试试 fulltext
    3dwelcome
    3dwelcome
    Apr 14, 2021
    12
    @MinQ "mysql 是没有优化的,需要自己新增,比如取前 N 个 reverse 然后存下来"
    新版本已经加上了,有新的创建 index 语法, DESC 关键词, 不用手动 reverse 。
    leviathan0992
    leviathan0992
    Apr 14, 2021
    13
    全文索引
    Drinker
    Drinker
    Apr 14, 2021
    14
    es 可以或者分表。
    securityCoding
    securityCoding
    Apr 14, 2021
    15
    2000 万表这个数量级应该禁止用 like 了 , 推荐用其他的中间件来解决这个问题,比如 es
    MinQ
    MinQ
    Apr 14, 2021❤️1
    16
    @3dwelcome
    你说的是这个吗?

    https://dev.mysql.com/doc/refman/8.0/en/create-index.html

    A key_part specification can end with ASC or DESC to specify whether index values are stored in ascending or descending order. The default is ascending if no order specifier is given. ASC and DESC are not permitted for HASH indexes. ASC and DESC are also not supported for multi-valued indexes. As of MySQL 8.0.12, ASC and DESC are not permitted for SPATIAL indexes.

    好像这个只是吧索引的顺序按从小到大或者从大到小排吧?而不是把某一列的字符 reverse 以后生成索引?
    fiypig
    fiypig
    Apr 14, 2021
    17
    %% 基本上索引就失效了
    3dwelcome
    3dwelcome
    Apr 14, 2021❤️1
    18
    @MinQ 记错了,那就是 CREATE INDEX idx1 ON table1 REVERSE(col1);

    REVERSE 是对针对字符串处理的。
    MinQ
    MinQ
    Apr 14, 2021
    19
    @3dwelcome create index 的时候不能使用 reverse 吧
    wakzz
    wakzz
    Apr 14, 2021
    20
    这种模糊查询,mysql 还是放弃吧,用 es
    catchexception
    catchexception
    Apr 14, 2021
    21
    倒排索引。
    3dwelcome
    3dwelcome
    Apr 14, 2021
    22
    @MinQ 我刚试了一下,没发现有什么问题。

    ALTER TABLE table1 ADD INDEX REVERSE(col);
    zhangysh1995
    zhangysh1995
    Apr 14, 2021
    23
    @johnsona 一楼这个虽然有点魔幻,但是挺靠谱的。。。贴主要不考虑下?@chinafengzhao
    zzl22100048
    zzl22100048
    Apr 14, 2021
    24
    上 ES,我们 2 亿企业数据,就靠 ES 撑着
    ilylx2008
    ilylx2008
    Apr 14, 2021
    25
    数据变化不大的话,遍历 2000w 条记录,与公司的对应关系,生成一个关系表。
    新增数据同样处理。
    L1shen
    L1shen
    Apr 14, 2021
    26
    嫌 es 重的话,还可以用 sonic
    https://github.com/valeriansaliou/sonic
    mlcq
    mlcq
    Apr 14, 2021
    27
    2000 万还不分表吗,还有你的索引要建好,尽量不要回表。你那种写法是不走索引的
    zhaokun
    zhaokun
    Apr 14, 2021
    28
    不增加服务比如 ES,可以考虑自己做倒排索引,当然要依赖分词服务
    jenlors
    jenlors
    Apr 14, 2021
    29
    试试 MySQL 全文索引,最简单的方案,增加中间件也会增加系统复杂度,需要仔细评估
    johnsona
    johnsona
    Apr 14, 2021 via iPhone
    30
    @zhangysh1995 主要 es 依赖分词 效果和模糊搜索不一样 分词没分出来 那就搜不到 github 的 issue 模块搜索功能就这样
    vindurriel
    vindurriel
    Apr 15, 2021 via iPhone
    31
    ES Solr Lucene 解决不了 like %foo% 中文默认单字分词的时候可以按字搜出来 但这不是 like %foo% 而是 in (f, o, o)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5027 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 01:56 · PVG 09:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.