用MATCH() ... AGAINST 方式來進行搜索
match()表示搜索的是那個列,against表示要搜索的是那個字符串mysql
查看默認的分詞(以這些詞來區分不一樣的關鍵詞);也能夠自定義分詞,以這些詞來區分不一樣的關鍵詞
SELECT * FROM information_schema.INNODB_FT_DEFAULT_STOPWORD;
如sql
+-------+ | 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個字符做爲一個關鍵詞,增大該值可減小全文索引的大小app
innodb_ft_max_token_size
默認84,表示最大84個字符做爲一個關鍵詞,限制該值可減小全文索引的大小socket
ngram_token_size
默認2,表示2個字符做爲內置分詞解析器的一個關鍵詞,如對「abcd」創建全文索引,關鍵詞爲'ab','bc','cd'
當使用ngram分詞解析器時,innodb_ft_min_token_size和innodb_ft_max_token_size 無效測試
注意 這三個參數均不可動態修改,修改了這些參數,需重啓MySQL服務,並從新創建全文索引搜索引擎
一、目標
二、設置如下參數減小磁盤IO壓力
SET GLOBAL sync_binlog=100; SET GLOBAL innodb_flush_log_at_trx_commit=2;
三、導入1kw 數據進行測試全文索引
該數據來源網上搜索
提取碼:60l7
四、某個文章表 的結構
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
五、導入數據後總數據量和數據文件、索引文件大小
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 | +------------+-------------+--------------+------------+----------------+------------+---------------------+
一、該表已有關鍵詞字段(對文章內容的簡述),並以「,」做爲分詞符
select keywords from article limit 10; +-------------------------------------------------+ | keywords | +-------------------------------------------------+ | NULL | | NULL | | ,婚姻,愛情 | | 髮型,偏分,化妝,時尚 | | 小A, | | ,服裝搭配,女性,時尚 | | 漂亮,女性 | | 情人節,東莞,女性 | | 皮膚,護膚,護膚,食品養分,美容,養生 | | 三里屯,北京,時尚 | +-------------------------------------------------+
二、不建全文索引時搜索某個關鍵詞
須要進行全表掃描
select count(*) from article where keywords like '%時尚%'; +----------+ | count(*) | +----------+ | 163 | +----------+ 1 row in set (7.56 sec)
三、對關鍵詞字段建立全文索引(以 , 做爲分詞)
[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)
一、對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平米精裝小戶型,海量圖片,附戶型圖 | |夜店女王性感煙燻貓眼妝 | |大秀哥重摔「巨石」強森 | |少女時代 崔秀英 服飾科普 林允兒 黃美英 金泰妍 鄭秀晶 | |德陽戶外踏青,花田自助燒烤 | +------------------------------------------------------------------------------+
二、對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
三、會建立倒排索引(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
四、不創建全文索引搜索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 | +----------+
五、使用全文索引搜索某個關鍵詞
響應時間有很大的提高
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)
六、注意當搜索的關鍵詞字符數大於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)