用MATCH() ... AGAINST 方式来进行搜索 match()表示搜索的是那个列,against表示要搜索的是那个字符串
查看默认的分词(以这些词来区分不同的关键词);也可以自定义分词,以这些词来区分不同的关键词 SELECT * FROM information_schema.INNODB_FT_DEFAULT_STOPWORD; 如
+-------+
| value |
+-------+
| a |
| about |
| an |
| are |
| as |
| at |
| be |
| by |
| com |
| de |
| en |
| for |
| from |
natural language search(自然语言搜索) 通过MATCH AGAINST 传递某个特定的字符串来进行检,默认方式
boolean search(布尔搜索) 为检索的字符串增加操作符,如“+”表示必须包含,"-"不包含,"*" 表示通配符,即使传递的字符串较小或出现在停词中,也不会被过滤掉
query expansion search(查询扩展搜索) 搜索字符串用于执行自然语言搜索,然后,搜索返回的最相关行的单词被添加到搜索字符串,并且再次进行搜索,查询将返回来自第二个搜索的行
配置相关参数 innodb_ft_min_token_size 默认3,表示最小3个字符作为一个关键词,增大该值可减少全文索引的大小
innodb_ft_max_token_size 默认84,表示最大84个字符作为一个关键词,限制该值可减少全文索引的大小
ngram_token_size 默认2,表示2个字符作为内置分词解析器的一个关键词,如对“abcd”建立全文索引,关键词为'ab','bc','cd' 当使用ngram分词解析器时,innodb_ft_min_token_size和innodb_ft_max_token_size 无效
注意 这三个参数均不可动态修改,修改了这些参数,需重启MySQL服务,并重新建立全文索引
1、目标
2、设置以下参数减少磁盘IO压力
SET GLOBAL sync_binlog=100;
SET GLOBAL innodb_flush_log_at_trx_commit=2;
3、导入1kw 数据进行测试全文索引 该数据来源网上搜索 提取码:60l7
4、某个文章表 的结构
CREATE TABLE `article` (
`id` bigint(10) NOT NULL,
`url` varchar(1024) CHARACTER SET latin1 NOT NULL DEFAULT '',
`title` varchar(256) NOT NULL DEFAULT '',
`source` varchar(32) DEFAULT '' COMMENT '真实来源',
`keywords` varchar(32) DEFAULT NULL,
`publish_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `title_idx` (`title`)
) ENGINE=InnoDB
使用myloader 多线程导入测试数据
先把测试数据进行解压
tar -zxf mydumper_dump_article.tar.gz
time myloader -u $user -p $passwd -S $socket -t 32 -d /datas/dump_article -v 3
5、导入数据后总数据量和数据文件、索引文件大小
SELECT COUNT(*) FROM `article`;
+----------+
| COUNT(*) |
+----------+
| 10000000 |
+----------+
1 row in set (7.85 sec)
SELECT table_name, CONCAT(FORMAT(SUM(data_length) / 1024 / 1024,2),'M') AS dbdata_size, CONCAT(FORMAT(SUM(index_length) / 1024 / 1024,2),'M') AS dbindex_size, CONCAT(FORMAT(SUM(data_length + index_length) / 1024 / 1024 / 1024,2),'G') AS `db_size(G)`, AVG_ROW_LENGTH,table_rows,update_time FROM information_schema.tables WHERE table_schema = DATABASE() and table_name='article';
+------------+-------------+--------------+------------+----------------+------------+---------------------+
| table_name | dbdata_size | dbindex_size | db_size(G) | AVG_ROW_LENGTH | table_rows | update_time |
+------------+-------------+--------------+------------+----------------+------------+---------------------+
| article | 3,710.00M | 1,003.00M | 4.60G | 414 | 9388739 | 2019-07-05 15:31:37 |
+------------+-------------+--------------+------------+----------------+------------+---------------------+
1、该表已有关键词字段(对文章内容的简述),并以“,”作为分词符
select keywords from article limit 10;
+-------------------------------------------------+
| keywords |
+-------------------------------------------------+
| NULL |
| NULL |
| ,婚姻,爱情 |
| 发型,偏分,化妆,时尚 |
| 小A, |
| ,服装搭配,女性,时尚 |
| 漂亮,女性 |
| 情人节,东莞,女性 |
| 皮肤,护肤,护肤,食品营养,美容,养生 |
| 三里屯,北京,时尚 |
+-------------------------------------------------+
2、不建全文索引时搜索某个关键词 需要进行全表扫描
select count(*) from article where keywords like '%时尚%';
+----------+
| count(*) |
+----------+
| 163 |
+----------+
1 row in set (7.56 sec)
3、对关键词字段创建全文索引(以 , 作为分词)
[mysqld]
innodb_ft_min_token_size=2
USE mysql;
CREATE TABLE my_stopwords(VALUE VARCHAR(30)) ENGINE = INNODB;
INSERT INTO my_stopwords(VALUE) VALUE (',');
SET GLOBAL innodb_ft_server_stopword_table = 'mysql/my_stopwords';
SHOW GLOBAL VARIABLES WHERE Variable_name IN('innodb_ft_min_token_size','innodb_ft_server_stopword_table');
+---------------------------------+--------------------+
| Variable_name | Value |
+---------------------------------+--------------------+
| innodb_ft_min_token_size | 2 |
| innodb_ft_server_stopword_table | mysql/my_stopwords |
+---------------------------------+--------------------+
alter table article add fulltext index idx_full_keyword(keywords);
* [ ] Query OK, 0 rows affected, 1 warning (1 min 27.92 sec)
* [ ] Records: 0 Duplicates: 0 Warnings: 1
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 7.8G 6.3G 1.2G 85% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/mapper/vg_opt-lvol0
19G 12G 5.7G 68% /datas
会创建原表大小的临时文件
8.6K Jul 5 16:19 #sql-5250_3533.frm
4.4G Jul 5 16:20 #sql-ib117-1768830977.ibd
alter table article add fulltext index idx_full_keyword(keywords);
ERROR 1114 (HY000): The table 'article' is full
select count(*) from article where match(keywords) against('%时尚%');
+----------+
| count(*) |
+----------+
| 163 |
+----------+
1 row in set (0.05 sec)
表示完全匹配 "三里屯,北京" 的记录数
select count(*) from article where match(keywords) against('+三里屯,北京' in boolean mode);
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.06 sec)
表示匹配“三里屯” 或者 “北京”的记录数
select count(*) from article where match(keywords) against('三里屯,北京');
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.06 sec)
1、对title字段建立全文索引(该字段没有固定的stopwords 分词,使用ngram分词解析器) 需先在my.cnf 配置文件中设置ngram_token_size(默认为2,2个字符作为ngram 的关键词),并重启mysql服务 这里使用默认的 2
select title from article limit 10;
+------------------------------------------------------------------------------+
| title |
+------------------------------------------------------------------------------+
| worth IT |
|Launchpad 江南皮革厂小show |
|Raw 幕后罕见一刻 “疯子”被抬回后台 |
|Raw:公子大骂老爸你就是个绿茶 公子以一打四 |
|四组30平米精装小户型,海量图片,附户型图 |
|夜店女王性感烟熏猫眼妆 |
|大秀哥重摔“巨石”强森 |
|少女时代 崔秀英 服饰科普 林允儿 黄美英 金泰妍 郑秀晶 |
|德阳户外踏青,花田自助烧烤 |
+------------------------------------------------------------------------------+
2、对title字段创建全文索引
alter table article add fulltext index ft_index_title(title) with parser ngram;
Query OK, 0 rows affected (3 min 29.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
3、会创建倒排索引(title字段越长长,创建的倒排索引越大) 112M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_1.ibd 28M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_2.ibd 20M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_3.ibd 140M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_4.ibd 128M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_5.ibd 668M Jul 5 21:46 FTS_00000000000000a7_00000000000000cd_INDEX_6.ibd
4、不建立全文索引搜索title的某个关键词
select count(*) from article where title like '%户外%';
+----------+
| count(*) |
+----------+
| 22058 |
+----------+
1 row in set (8.60 sec)
select count(*) from article where title like '%后台%';
+----------+
| count(*) |
+----------+
| 1142 |
+----------+
5、使用全文索引搜索某个关键词 响应时间有很大的提升
select count(*) from article where match(title) against('户外');
+----------+
| count(*) |
+----------+
| 22058 |
+----------+
1 row in set (0.07 sec)
select count(*) from article where title like '%后台%';
+----------+
| count(*) |
+----------+
| 1142 |
+----------+
1 row in set (8.31 sec)
6、注意当搜索的关键词字符数大于2 (ngram_token_size定义大小)会出现不一致问题
普通搜索,实际中出现该关键词的记录数为6
select count(*) from article where title like '%公子大%';
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (8.40 sec)
全文搜索,出现关键字的记录数为9443
select count(*) from article where match(title) against('公子大');
+----------+
| count(*) |
+----------+
| 9443 |
+----------+
1 row in set (0.06 sec)
实际出现该关键字的记录数为1
select count(*) from article where title like '%花田自助%';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (8.33 sec)
全文搜索出现该关键词的记录数为3202
select count(*) from article where match(title) against('花田自助');
+----------+
| count(*) |
+----------+
| 3202 |
+----------+
1 row in set (0.06 sec)
参考 InnoDB FULLTEXT Indexes