背景
MySQL,单表数据量超过 2000 万,需求是要求快速模糊查询。
select * from xxx where aaa like '%yyyyy' ;
有快速优化方案吗?
背景
MySQL,单表数据量超过 2000 万,需求是要求快速模糊查询。
select * from xxx where aaa like '%yyyyy' ;
有快速优化方案吗?
|
1
加一列把原来的列 reverse 一下 这样就能用到索引了( bushi
|
|
lscexpressApr 14, 2021
2
使用 fulltext
|
|
Justin13Apr 14, 2021 via Android
3
对实时性要求不高可以加缓存,把查询结果存储起来。
要求实时但是查询是固定的话,可以加一索引列,存储查询的结果,写入时一并添加。 如果什么都不能做,只能改查询,那就洗洗睡吧。 |
|
simon1991Apr 14, 2021
4
上 elasticsearch ()
|
|
MinQApr 14, 2021
5
上 ES 或者 Solr 吧
|
|
MinQApr 14, 2021
6
而且像 xxxx%的前缀查询才能用索引,%yyyyy 建议直接放弃治疗
|
|
3dwelcomeApr 14, 2021
7
@johnsona "加一列把原来的列 reverse 一下 这样就能用到索引了( bushi"
我觉得 reverse 这种,新数据库已经自己优化掉了。要不然 2000 万条数据无索引暴力查询,不知道慢到什么程度了。 |
|
8
不好意思,查询逻辑要求的是 like %企业名关键词%
|
|
MinQApr 14, 2021
10
@chinafengzhao 还是 fulltext 或者 ES 吧
|
|
11
我试试 fulltext
|
|
3dwelcomeApr 14, 2021
12
@MinQ "mysql 是没有优化的,需要自己新增,比如取前 N 个 reverse 然后存下来"
新版本已经加上了,有新的创建 index 语法, DESC 关键词, 不用手动 reverse 。 |
|
leviathan0992Apr 14, 2021
13
全文索引
|
|
DrinkerApr 14, 2021
14
es 可以或者分表。
|
|
securityCodingApr 14, 2021
15
2000 万表这个数量级应该禁止用 like 了 , 推荐用其他的中间件来解决这个问题,比如 es
|
|
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 以后生成索引? |
|
fiypigApr 14, 2021
17
%% 基本上索引就失效了
|
|
18
|
|
wakzzApr 14, 2021
20
这种模糊查询,mysql 还是放弃吧,用 es
|
|
catchexceptionApr 14, 2021
21
倒排索引。
|
|
zhangysh1995Apr 14, 2021
23
@johnsona 一楼这个虽然有点魔幻,但是挺靠谱的。。。贴主要不考虑下?@chinafengzhao
|
|
zzl22100048Apr 14, 2021
24
上 ES,我们 2 亿企业数据,就靠 ES 撑着
|
|
ilylx2008Apr 14, 2021
25
数据变化不大的话,遍历 2000w 条记录,与公司的对应关系,生成一个关系表。
新增数据同样处理。 |
|
L1shenApr 14, 2021
26
嫌 es 重的话,还可以用 sonic
https://github.com/valeriansaliou/sonic |
|
mlcqApr 14, 2021
27
2000 万还不分表吗,还有你的索引要建好,尽量不要回表。你那种写法是不走索引的
|
|
zhaokunApr 14, 2021
28
不增加服务比如 ES,可以考虑自己做倒排索引,当然要依赖分词服务
|
|
jenlorsApr 14, 2021
29
试试 MySQL 全文索引,最简单的方案,增加中间件也会增加系统复杂度,需要仔细评估
|
|
johnsonaApr 14, 2021 via iPhone
30
@zhangysh1995 主要 es 依赖分词 效果和模糊搜索不一样 分词没分出来 那就搜不到 github 的 issue 模块搜索功能就这样
|
|
vindurrielApr 15, 2021 via iPhone
31
ES Solr Lucene 解决不了 like %foo% 中文默认单字分词的时候可以按字搜出来 但这不是 like %foo% 而是 in (f, o, o)
|