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

2021-04-14 10:04:49 +08:00
 chinafengzhao

背景

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

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

有快速优化方案吗?

2311 次点击
所在节点    数据库
31 条回复
johnsona
2021-04-14 10:10:02 +08:00
加一列把原来的列 reverse 一下 这样就能用到索引了( bushi
lscexpress
2021-04-14 10:10:11 +08:00
使用 fulltext
Justin13
2021-04-14 10:16:54 +08:00
对实时性要求不高可以加缓存,把查询结果存储起来。
要求实时但是查询是固定的话,可以加一索引列,存储查询的结果,写入时一并添加。
如果什么都不能做,只能改查询,那就洗洗睡吧。
simon1991
2021-04-14 10:18:40 +08:00
上 elasticsearch ()
MinQ
2021-04-14 10:20:12 +08:00
上 ES 或者 Solr 吧
MinQ
2021-04-14 10:21:04 +08:00
而且像 xxxx%的前缀查询才能用索引,%yyyyy 建议直接放弃治疗
3dwelcome
2021-04-14 10:21:52 +08:00
@johnsona "加一列把原来的列 reverse 一下 这样就能用到索引了( bushi"

我觉得 reverse 这种,新数据库已经自己优化掉了。要不然 2000 万条数据无索引暴力查询,不知道慢到什么程度了。
chinafengzhao
2021-04-14 10:30:16 +08:00
不好意思,查询逻辑要求的是 like %企业名关键词%
MinQ
2021-04-14 10:30:37 +08:00
@3dwelcome mysql 是没有优化的,需要自己新增,比如取前 N 个 reverse 然后存下来
MinQ
2021-04-14 10:33:04 +08:00
@chinafengzhao 还是 fulltext 或者 ES 吧
chinafengzhao
2021-04-14 10:38:43 +08:00
我试试 fulltext
3dwelcome
2021-04-14 10:39:31 +08:00
@MinQ "mysql 是没有优化的,需要自己新增,比如取前 N 个 reverse 然后存下来"
新版本已经加上了,有新的创建 index 语法, DESC 关键词, 不用手动 reverse 。
leviathan0992
2021-04-14 10:39:47 +08:00
全文索引
Drinker
2021-04-14 10:44:36 +08:00
es 可以或者分表。
securityCoding
2021-04-14 10:45:49 +08:00
2000 万表这个数量级应该禁止用 like 了 , 推荐用其他的中间件来解决这个问题,比如 es
MinQ
2021-04-14 10:48:51 +08:00
@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
2021-04-14 11:02:55 +08:00
%% 基本上索引就失效了
3dwelcome
2021-04-14 11:11:52 +08:00
@MinQ 记错了,那就是 CREATE INDEX idx1 ON table1 REVERSE(col1);

REVERSE 是对针对字符串处理的。
MinQ
2021-04-14 11:19:16 +08:00
@3dwelcome create index 的时候不能使用 reverse 吧
wakzz
2021-04-14 11:21:53 +08:00
这种模糊查询,mysql 还是放弃吧,用 es

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/770500

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX