在mysql官网,innodb引擎在5.6.4版本提供了对全文索引的支持,笔者对此做了测试,发现对中文全文检索的支持依然不理想,但却确实提供了对英文的全文支持。
12.9.5 Full-Text Restrictions
Full-text searches are supported for InnoDB and MyISAM tables only. FULLTEXT index support for InnoDB tables requires MySQL 5.6.4 or higher.
测试过程如下:
1、版本选择,选5.6.17 GA版。
2、安装完成后,添加全文索引进行测试,表引擎为Innodb
(1)修改ft_min_word_len参数值为1(默认是4,2个汉字)调整全文索引检索字段的最小长度为1个字节;
mysql> show variables like '%ft_min_word_len%'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | ft_min_word_len | 1 | +-----------------+-------+ 1 row in set (0.00 sec)
(2)对表A的content列添加全文索引;
> alter table ask_questions add fulltext ind_ask_questions_content(content);
查看
>show index from ask_questions;
mysql> show index from ask_questions; +---------------+------------+----------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------------+------------+----------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | ask_questions | 1 | ind_ask_questions_content_cft | 1 | content | NULL | 1 | NULL | NULL | | FULLTEXT | | | +---------------+------------+----------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 9 rows in set (0.00 sec)
模糊匹配查询,看看包含关键字的行数:
> select count(*) from ask_questions where content like '%xxx%';
一共有182行,其中包括四种情况:'%xxx%','%xxx','xxx%','xxx'。
在全文索引查看的时候只能对 'xxx' 这种情况进行检索,其他的行由于汉字与英文字母的断字不一样而忽略掉了;
> select * from ask_questions where MATCH(content) AGAINST ('好玩吗') order by id limit 10;
但是其中的行,比如不被空格,"?"等隔开的行,是不会被检索出来的:
> select * from ask_question_bak where content like '%好玩吗%' limit 10;
我们可以通过查看id列看出来差别;
3、myisam中的中文全文索引测试;
对于mysql自带的功能,也是一样的,支持并不是很好。
4、myisam安装mysqlcft插件,测试对中文全文索引的支持;
从coder.google下载mysqlcft,下载地址:https://code.google.com/p/mysqlcft/downloads/list
(1) 解压,安装plugin。
查看插件目录:
> show variables like '%plugin%';
+---------------+---------------------------------+ | Variable_name | Value | +---------------+---------------------------------+ | plugin_dir | /usr/local/mysql56//lib/plugin/ | +---------------+---------------------------------+
(2)解压,把mysqlcfg.so文件cp到这个目录下,安装plugin
> INSTALL PLUGIN mysqlcft SONAME 'mysqlcft.so';
(3)查看。
> select * from mysql.plugin;
+----------+-------------+ | name | dl | +----------+-------------+ | mysqlcft | mysqlcft.so | +----------+-------------+
安装mysqlcft插件成功;
测试查看查询数目与模糊查询一致,效率提高了300倍左右;