對於mysql 5.6.4以上版本innodb支持全文索引的測試
在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.27 版。
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倍左右;