MySQL InnoDB full-text index usage

Keywords: MySQL

1. mysql 5.7 full text index the following parameters (configuration file / etc/my.cnf)
#Controls the minimum length of segmentation in innodb full-text retrieval. If it is set to 2, a Chinese character and a letter will not be found.
ngram_token_size=1
#The minimum word length stored in the FULLTEXT index of InnoDB. After NGram token size is used, InnoDB ft min token size will not be used. But for the sake of insurance, I have set both.
innodb_ft_min_token_size=1
#Minimum participle length, generally modified to 1
ft_min_word_len = 1

2. Create table

mysql> show create table s_test;
+--------+---------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                                                                                                   |
+--------+---------------------------------------------------------+
| s_test | CREATE TABLE `s_test` (
  `id` int(11) NOT NULL DEFAULT '0' COMMENT 'primary key',
  `uname` varchar(50) DEFAULT NULL COMMENT 'User name',
  `dept` int(11) DEFAULT NULL COMMENT 'Departmental group ID',
  `info` varchar(200) DEFAULT NULL COMMENT 'Other information'
) ENGINE=InnoDB DEFAULT CHARSET=utf8           |
+--------+--------------------------------------------------------+
1 row in set (0.00 sec)

3. Create index

mysql> create fulltext index ix_ft_s_test_uname_info on s_test(uname,info) WITH PARSER ngram;
Query OK, 0 rows affected, 1 warning (2.68 sec)
Records: 0  Duplicates: 0  Warnings: 1

4. Query all indexes of this table

mysql> show index from s_test;
+--------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name                | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| s_test |          1 | ix_ft_s_test_uname_info |            1 | uname       | NULL      |       99750 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
| s_test |          1 | ix_ft_s_test_uname_info |            2 | info        | NULL      |       99750 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+--------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

5. Query index details

mysql> select * from mysql.innodb_index_stats where database_name='mydb' and table_name='s_test' ;
+---------------+------------+------------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name       | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------------+---------------------+--------------+------------+-------------+-----------------------------------+
| mydb          | s_test     | FTS_DOC_ID_INDEX | 2015-10-03 14:07:18 | n_diff_pfx01 |     100672 |          20 | FTS_DOC_ID                        |
| mydb          | s_test     | FTS_DOC_ID_INDEX | 2015-10-03 14:07:18 | n_leaf_pages |        121 |        NULL | Number of leaf pages in the index |
| mydb          | s_test     | FTS_DOC_ID_INDEX | 2015-10-03 14:07:18 | size         |        161 |        NULL | Number of pages in the index      |
| mydb          | s_test     | GEN_CLUST_INDEX  | 2015-10-03 14:07:18 | n_diff_pfx01 |      99750 |          20 | DB_ROW_ID                         |
| mydb          | s_test     | GEN_CLUST_INDEX  | 2015-10-03 14:07:18 | n_leaf_pages |        525 |        NULL | Number of leaf pages in the index |
| mydb          | s_test     | GEN_CLUST_INDEX  | 2015-10-03 14:07:18 | size         |        545 |        NULL | Number of pages in the index      |
+---------------+------------+------------------+---------------------+--------------+------------+-------------+-----------------------------------+
6 rows in set (0.00 sec)

6. Full index query

By specifying the
 1. In natural language mode expr is the string to be searched. 
2. IN NATURAL MODE WITH QUERY EXPANSION searches for the first time with a given phrase, and for the second time with a given phrase combined with some highly relevant lines in the first search results.
3. IN BOOLEAN MODE expr has special characters to assist special search syntax.
Query can only be by phrase, not by middle character
mysql> select count(*) from  s_test where MATCH(uname,info) AGAINST ('a*' IN BOOLEAN MODE);
+----------+
| count(*) |
+----------+
|     6238 |
+----------+
1 row in set (0.02 sec)

Posted by cruz610 on Fri, 01 Nov 2019 10:22:12 -0700