Mysql全文索引实现模糊查询
MySQL 5.7.6开始,MySQL内置了ngram全文解析器,用来支持中、日、韩文的分词
启动方法
配置mysql的ngram,打开mysql server的配置文件,编辑在[mysqld]下面加入这样的配置
# vim /etc/my.cnf [mysqld] ngram_token_size=2
查看是否开启
mysql> show variables like 'ngram_token_size%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | ngram_token_size | 1 | +------------------+-------+
演示数据表
CREATE TABLE `t_testfulltext` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(128) DEFAULT NULL, `data` int(11) DEFAULT NULL, PRIMARY KEY (`id`), FULLTEXT KEY `ft_index` (`name`) /*!50100 WITH PARSER `ngram` */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
执行语句
SELECT * FROM t_testfulltext WHERE MATCH (name) AGAINST ('今天');
参考:https://cloud.tencent.com/developer/article/1634247?areaSource=&traceId=