由于模糊搜索的需求越来越多,所以在从库上创建全文索引来应付各种搜索需求。现在创建一个用户名的全文搜索,全表大小为1100W条记录左右。
1、表结构查询
mysql> desc ucenter_member; +------------------+------------------+------+-----+------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+------------------+------+-----+------------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | nickname | char(20) | YES | MUL | | | | devicetoken | varchar(60) | NO | MUL | | | | version | char(10) | YES | | | | | app_type | tinyint(1) | YES | | 0 | | | password | char(32) | NO | | NULL | | | salt | char(4) | NO | | NULL | | | mobile | varchar(13) | YES | MUL | NULL | | | avatar | int(11) | NO | | 0 | | | sex | tinyint(1) | NO | | 0 | | | birthday | date | YES | | 0000-00-00 | | +------------------+------------------+------+-----+------------+----------------+ 11 rows in set (0.00 sec)
必要的参数设置
mysql> show variables like ‘ft_min_word_len‘; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | ft_min_word_len | 1 | +-----------------+-------+ 1 row in set (0.00 sec)
2、添加全文索引
mysql> alter table ucenter_member add fulltext index ix_ft_ucenter_member_nickname(nickname) with parser ngram;
3、添加过程(添加全文整个过程要很长的时间,并且这段时间里所有业务将不能执行)
主从复制将会停止mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.200 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mybin.000750 Read_Master_Log_Pos: 746166232 Relay_Log_File: relay-bin.002247 Relay_Log_Pos: 17345090 Relay_Master_Log_File: mybin.000750 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: sys.%,mysql.%,information_schema.%,performance_schema.% Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 736231270 Relay_Log_Space: 27280306 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 1104 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 5a00f33b-8551-11e5-9122-0016310207a0 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Waiting for dependent transaction to commit Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 5a00f33b-8551-11e5-9122-0016310207a0:1451340674-1451398198 Executed_Gtid_Set: 5a00f33b-8551-11e5-9122-0016310207a0:1-1451377326, 9905e5a2-815d-11f5-8244-00163f004aa2:1-84 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: 1 row in set (0.00 sec)
查询进程
mysql> show processlist; +----+-------------+-----------+--------+---------+------+---------------------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+--------+---------+------+---------------------------------------------+------------------------------------------------------------------------------------------------------+ | 1 | system user | | NULL | Connect | 1673 | Waiting for master to send event | NULL | | 2 | system user | | NULL | Connect | 1123 | Waiting for dependent transaction to commit | NULL | | 3 | system user | | NULL | Connect | 1123 | Waiting for table metadata lock | NULL | | 4 | system user | | NULL | Connect | 1195 | Waiting for an event from Coordinator | NULL | | 5 | system user | | NULL | Connect | 1673 | Waiting for an event from Coordinator | NULL | | 6 | system user | | NULL | Connect | 1673 | Waiting for an event from Coordinator | NULL | | 9 | root | localhost | peiyin | Query | 1123 | altering table | alter table ucenter_member add fulltext index ix_ft_ucenter_member_nickname(nickname) with parser ng | | 14 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+-------------+-----------+--------+---------+------+---------------------------------------------+------------------------------------------------------------------------------------------------------+ 8 rows in set (0.00 sec)
4、完成创建
mysql> alter table ucenter_member add fulltext index ix_ft_ucenter_member_nickname(nickname) with parser ngram; Query OK, 0 rows affected, 1 warning (31 min 7.29 sec) Records: 0 Duplicates: 0 Warnings: 1
5、查询索引
按布尔全文搜索模式查询
mysql> select version,nickname from ucenter_member where match(nickname) against(‘中国‘ in boolean mode) limit 10; +---------+--------------------------------------------------+ | version | nickname | +---------+--------------------------------------------------+ | 4.63 | 我爱中国 立场中国 | | | 中国联通一窍不通中国移动一动不动 | | 4.21 | 姜振光(中国·北科、中国·沪航) | | 4.63 | 我是中国人,我爱中国 | | 4.40 | 中国人中国心 | | 4.52 | 中国特警――中国青年财神爷 | | 4.52 | 完美中国,中国完美 | | 4.81 | 中国梦、梦中国 | | 4.52 | 中国梦~我的梦 | | | A 中国梦 | +---------+--------------------------------------------------+ 10 rows in set (0.01 sec)
mysql> select version,nickname from ucenter_member where match(nickname) against(‘中国梦想‘ in boolean mode) limit 10; Empty set (0.03 sec)
按自然语言搜索模式查询
mysql> select version,nickname from ucenter_member where match(nickname) against(‘中国‘ in natural language mode) limit 10; +---------+--------------------------------------------------+ | version | nickname | +---------+--------------------------------------------------+ | 4.63 | 我爱中国 立场中国 | | | 中国联通一窍不通中国移动一动不动 | | 4.21 | 姜振光(中国·北科、中国·沪航) | | 4.63 | 我是中国人,我爱中国 | | 4.40 | 中国人中国心 | | 4.52 | 中国特警――中国青年财神爷 | | 4.52 | 完美中国,中国完美 | | 4.81 | 中国梦、梦中国 | | 4.52 | 中国梦~我的梦 | | | A 中国梦 | +---------+--------------------------------------------------+ 10 rows in set (0.01 sec) mysql> select version,nickname from ucenter_member where match(nickname) against(‘中国梦想‘ in natural language mode) limit 10; +------------+--------------------------------------------------+ | version | nickname | +------------+--------------------------------------------------+ | 4.81 | 中国梦、梦中国 | | 4.63 | 中国梦我的梦,我的梦想是行业顶尖 | | 4.52 | 中国梦~我的梦 | | | A 中国梦 | | | 中国梦 | | 3.17 | 中国梦 | | 3.171 | AST中国梦 | | 3.20201505 | 中国梦 红楼梦 | | 3.211 | 中国梦 | | 4.63 | 中国梦 | +------------+--------------------------------------------------+ 10 rows in set (0.05 sec)
6、产生的文件
[[email protected] mydbs]# ls -alh FTS* -rw-r----- 1 mysql mysql 104M Oct 26 11:47 FTS_0000000000000353_0000000000000710_INDEX_1.ibd -rw-r----- 1 mysql mysql 17M Oct 26 11:47 FTS_0000000000000353_0000000000000710_INDEX_2.ibd -rw-r----- 1 mysql mysql 14M Oct 26 11:47 FTS_0000000000000353_0000000000000710_INDEX_3.ibd -rw-r----- 1 mysql mysql 40M Oct 26 11:47 FTS_0000000000000353_0000000000000710_INDEX_4.ibd -rw-r----- 1 mysql mysql 44M Oct 26 11:48 FTS_0000000000000353_0000000000000710_INDEX_5.ibd -rw-r----- 1 mysql mysql 212M Oct 26 11:47 FTS_0000000000000353_0000000000000710_INDEX_6.ibd -rw-r----- 1 mysql mysql 96K Oct 26 11:43 FTS_0000000000000353_BEING_DELETED_CACHE.ibd -rw-r----- 1 mysql mysql 96K Oct 26 11:42 FTS_0000000000000353_BEING_DELETED.ibd -rw-r----- 1 mysql mysql 96K Oct 26 11:41 FTS_0000000000000353_CONFIG.ibd -rw-r----- 1 mysql mysql 96K Oct 26 11:40 FTS_0000000000000353_DELETED_CACHE.ibd -rw-r----- 1 mysql mysql 96K Oct 26 12:09 FTS_0000000000000353_DELETED.ibd
时间: 2024-10-11 16:41:04