- 浏览: 984364 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
mysql大表查询的时候,'String%'模糊查询可以使用B+树类型的索引prefix,然而'String%'匹配模式在应用中不是我们所需要的,往往需要'%String%',这是我们可以考虑使用FULLTEXT索引,INNODE是以红黑树来,存储全文索引,下面我们就来测试一下全文索引。
首先建表:
CREATE TABLE fts_a(
FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
body TEXT,
PRIMARY KEY (FTS_DOC_ID)
);
插入记录:
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('1', 'some one like you');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('2', 'you can you up');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('3', 'I like your style');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('4', 'one day ,i see you');
创建全文索引:
mysql> CREATE FULLTEXT INDEX idx_fts ON fts_a(body);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看索引:
mysql> show index from fts_a;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fts_a | 0 | PRIMARY | 1 | FTS_DOC_ID | A | 4 | NULL | NULL | | BTREE | | |
| fts_a | 1 | idx_fts | 1 | body | NULL | 4 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set
设置索引参数:
mysql> SET GLOBAL innodb_ft_aux_table='test/fts_a';
Query OK, 0 rows affected
查看全文索引(倒排索引)信息:
mysql> select * from fts_a;
+------------+--------------------+
| FTS_DOC_ID | body |
+------------+--------------------+
| 1 | some one like you |
| 2 | you can you up |
| 3 | I like your style |
| 4 | one day ,i see you |
+------------+--------------------+
4 rows in set
mysql> select * from information_schema.INNODB_FT_INDEX_TABLE;
+-------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+-------+--------------+-------------+-----------+--------+----------+
| can | 2 | 2 | 1 | 2 | 4 |
| day | 4 | 4 | 1 | 4 | 4 |
| like | 1 | 3 | 2 | 1 | 9 |
| like | 1 | 3 | 2 | 3 | 2 |
| one | 1 | 4 | 2 | 1 | 5 |
| one | 1 | 4 | 2 | 4 | 0 |
| see | 4 | 4 | 1 | 4 | 11 |
| some | 1 | 1 | 1 | 1 | 0 |
| style | 3 | 3 | 1 | 3 | 12 |
| you | 1 | 4 | 3 | 1 | 14 |
| you | 1 | 4 | 3 | 2 | 0 |
| you | 1 | 4 | 3 | 2 | 8 |
| you | 1 | 4 | 3 | 4 | 15 |
| your | 3 | 3 | 1 | 3 | 7 |
+-------+--------------+-------------+-----------+--------+----------+
14 rows in set
删除记录innodb并不会立即删除索引,要进行优化操作,测试如下
mysql> DELETE FROM fts_a WHERE fts_doc_id=4;
Query OK, 1 row affected
mysql> SELECT * FROM information_schema.INNODB_FT_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
优化:
mysql> SET GLOBAL innodb_optimize_fulltext_only=1;
Query OK, 0 rows affected
mysql> OPTIMIZE TABLE test.fts_a;
+------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------+----------+----------+----------+
| test.fts_a | optimize | status | OK |
+------------+----------+----------+----------+
1 row in set
mysql> SELECT * FROM information_schema.INNODB_FT_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
mysql> SELECT * FROM information_schema.INNODB_FT_BEING_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
利用全文索引查询记录:
mysql> SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE);
+------------+-------------------+
| FTS_DOC_ID | body |
+------------+-------------------+
| 1 | some one like you |
| 3 | I like your style |
+------------+-------------------+
2 rows in set
从查询解释我们可以看出使用个全文索引
mysql> EXPLAIN SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE);
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | fts_a | fulltext | idx_fts | idx_fts | 0 | NULL | 1 | Using where |
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
1 row in set
查询文档相关性
mysql>
SELECT FTS_DOC_ID,body,MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE) AS Relevance FROM fts_a ;
+------------+-------------------+--------------------+
| FTS_DOC_ID | body | Relevance |
+------------+-------------------+--------------------+
| 1 | some one like you | 0.0906190574169159 |
| 2 | you can you up | 0 |
| 3 | I like your style | 0.0906190574169159 |
| 5 | hell girls | 0 |
+------------+-------------------+--------------------+
4 rows in set
查询存在like和you的文档
mysql> SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('+like +you' IN BOOLEAN MODE);
+------------+-------------------+
| FTS_DOC_ID | body |
+------------+-------------------+
| 1 | some one like you |
+------------+-------------------+
1 row in set
查看一般匹配查询,并没有使用索引
mysql> EXPLAIN SELECT * FROM fts_a WHERE body LIKE '%like%';
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | fts_a | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set
首先建表:
CREATE TABLE fts_a(
FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
body TEXT,
PRIMARY KEY (FTS_DOC_ID)
);
插入记录:
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('1', 'some one like you');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('2', 'you can you up');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('3', 'I like your style');
INSERT INTO `test`.`fts_a` (`FTS_DOC_ID`, `body`) VALUES ('4', 'one day ,i see you');
创建全文索引:
mysql> CREATE FULLTEXT INDEX idx_fts ON fts_a(body);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看索引:
mysql> show index from fts_a;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fts_a | 0 | PRIMARY | 1 | FTS_DOC_ID | A | 4 | NULL | NULL | | BTREE | | |
| fts_a | 1 | idx_fts | 1 | body | NULL | 4 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set
设置索引参数:
mysql> SET GLOBAL innodb_ft_aux_table='test/fts_a';
Query OK, 0 rows affected
查看全文索引(倒排索引)信息:
mysql> select * from fts_a;
+------------+--------------------+
| FTS_DOC_ID | body |
+------------+--------------------+
| 1 | some one like you |
| 2 | you can you up |
| 3 | I like your style |
| 4 | one day ,i see you |
+------------+--------------------+
4 rows in set
mysql> select * from information_schema.INNODB_FT_INDEX_TABLE;
+-------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+-------+--------------+-------------+-----------+--------+----------+
| can | 2 | 2 | 1 | 2 | 4 |
| day | 4 | 4 | 1 | 4 | 4 |
| like | 1 | 3 | 2 | 1 | 9 |
| like | 1 | 3 | 2 | 3 | 2 |
| one | 1 | 4 | 2 | 1 | 5 |
| one | 1 | 4 | 2 | 4 | 0 |
| see | 4 | 4 | 1 | 4 | 11 |
| some | 1 | 1 | 1 | 1 | 0 |
| style | 3 | 3 | 1 | 3 | 12 |
| you | 1 | 4 | 3 | 1 | 14 |
| you | 1 | 4 | 3 | 2 | 0 |
| you | 1 | 4 | 3 | 2 | 8 |
| you | 1 | 4 | 3 | 4 | 15 |
| your | 3 | 3 | 1 | 3 | 7 |
+-------+--------------+-------------+-----------+--------+----------+
14 rows in set
删除记录innodb并不会立即删除索引,要进行优化操作,测试如下
mysql> DELETE FROM fts_a WHERE fts_doc_id=4;
Query OK, 1 row affected
mysql> SELECT * FROM information_schema.INNODB_FT_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
优化:
mysql> SET GLOBAL innodb_optimize_fulltext_only=1;
Query OK, 0 rows affected
mysql> OPTIMIZE TABLE test.fts_a;
+------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------+----------+----------+----------+
| test.fts_a | optimize | status | OK |
+------------+----------+----------+----------+
1 row in set
mysql> SELECT * FROM information_schema.INNODB_FT_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
mysql> SELECT * FROM information_schema.INNODB_FT_BEING_DELETED;
+--------+
| DOC_ID |
+--------+
| 4 |
+--------+
1 row in set
利用全文索引查询记录:
mysql> SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE);
+------------+-------------------+
| FTS_DOC_ID | body |
+------------+-------------------+
| 1 | some one like you |
| 3 | I like your style |
+------------+-------------------+
2 rows in set
从查询解释我们可以看出使用个全文索引
mysql> EXPLAIN SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE);
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | fts_a | fulltext | idx_fts | idx_fts | 0 | NULL | 1 | Using where |
+----+-------------+-------+----------+---------------+---------+---------+------+------+-------------+
1 row in set
查询文档相关性
mysql>
SELECT FTS_DOC_ID,body,MATCH(body) AGAINST ('like' IN NATURAL LANGUAGE MODE) AS Relevance FROM fts_a ;
+------------+-------------------+--------------------+
| FTS_DOC_ID | body | Relevance |
+------------+-------------------+--------------------+
| 1 | some one like you | 0.0906190574169159 |
| 2 | you can you up | 0 |
| 3 | I like your style | 0.0906190574169159 |
| 5 | hell girls | 0 |
+------------+-------------------+--------------------+
4 rows in set
查询存在like和you的文档
mysql> SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('+like +you' IN BOOLEAN MODE);
+------------+-------------------+
| FTS_DOC_ID | body |
+------------+-------------------+
| 1 | some one like you |
+------------+-------------------+
1 row in set
查看一般匹配查询,并没有使用索引
mysql> EXPLAIN SELECT * FROM fts_a WHERE body LIKE '%like%';
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | fts_a | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set
- FULLTEXT.rar (494 Bytes)
- 下载次数: 0
发表评论
-
Deadlock found when trying to get lock; try restarting transaction解决方式
2017-07-18 23:00 2073MySQL 事务的学习整理:http://blog.csdn. ... -
MySQL慢日志
2017-05-18 16:05 1034The Slow Query Log:https://dev. ... -
The table is full问题解决过程
2017-05-06 15:29 7641The table‘xxxx’is full 设置临时表大小 ... -
百万级数据-程序迁移后续
2017-04-13 18:09 1635百万级数据-程序迁移:http://donald-draper ... -
Msyql日期字符串转换
2017-04-01 14:13 544Date和String的互相转换:http://www.tui ... -
Mysql添加约束
2017-03-31 16:28 902MySQL中对三种约束的支持:http://leekai.me ... -
Mysql FEDERATED引擎
2016-11-29 15:51 608使用mysql federated引擎构建MySQL分布式数据 ... -
MySQL触发器
2016-11-24 19:04 722CHANGE MASTER:http://dev.mysql. ... -
Mysql主从配置
2016-11-11 18:31 5311、主从服务器分别作以下操作: 1)版本一致 2)初始 ... -
百万级数据-程序迁移
2016-09-29 19:03 2634JVM学习笔记:http://blog.csdn.net/cu ... -
Mysql 备份工具XtraBackup增量备份
2016-08-05 18:11 723安装:http://donald-draper.iteye.c ... -
Mysql 备份工具XtraBackup全量备份
2016-08-05 16:41 569Percona安装:http://donald-draper. ... -
Mysql 备份工具XtraBackup 安装
2016-08-05 16:28 946开源热备工具XtraBackup下载:https://www. ... -
sysbench基准测试
2016-08-01 17:45 791下载sysbench:http://dev.mysql.com ... -
mysql 事务处理
2016-07-29 16:07 512创建表: CREATE TABLE `role` ( ` ... -
MySQL 物理文件的迁移
2016-07-26 15:39 2350参考资料:http://www.cnblogs.com/adv ... -
centos7 安装mysql
2016-07-26 11:36 748下载MYSQL-RPM包:http://downloads.m ... -
mysql 大表添加索引注意事项
2016-07-25 16:01 2646LINXU top命令: http://www.c ... -
mysql 大表分页查询测试分析优化
2016-07-25 11:30 1502索引概念: http://blog.csdn.net/xlur ... -
MySQL事务
2016-06-01 10:49 615事务基础知识:http://my.oschina.net/je ...
相关推荐
MySQL全文索引是一种高效检索文本数据的技术,尤其适用于大数据量的文本字段搜索。在MySQL中,全文索引主要应用于MyISAM和InnoDB两种表引擎,尽管MyISAM是传统选择,但自MySQL 5.6以后,InnoDB也开始支持全文索引。 ...
MySQL全文索引是一种高效检索文本数据的机制,尤其适用于大数据量的文本检索场景。全文索引在MySQL中主要用于提升对长文本字段的搜索性能,它能够理解查询字符串中的语义,找出与之最相关的记录。在MySQL 5.6之前,...
首先,全文索引(Full-text Index)是MySQL提供的一种特殊类型的索引,专门用于提高全文搜索的性能。全文索引适用于处理大量文本数据,它能够快速地找出包含特定单词或短语的记录。但是,全文索引并不适用于简单的...
MySQL全文索引是一种高效检索大量文本数据的机制,尤其适用于大数据搜索场景。在MySQL 5.5.24版本中,全文索引主要用于提升文本字段的搜索效率,它通过分词技术将文本拆分成可搜索的词项。全文索引在MyISAM存储引擎...
MySQL全文索引是一种高效检索文本数据的机制,它在处理大量文本数据的查询时能显著提升性能。全文索引在数据库设计中起着至关重要的作用,尤其对于那些需要执行复杂文本搜索的应用程序。 首先,创建全文索引需要...
MySQL全文索引是一种提高数据库查询性能的技术,尤其适用于大规模文本数据的检索。它通过分词技术和特定的算法,分析文本中的关键词频率和重要性,从而快速定位到匹配的记录。在MySQL中,全文索引主要应用于MYISAM...
总之,MySQL的全文模糊查找是提高数据库搜索效率的有效手段,而PHP的Unicode工具类可以帮助我们处理中文字符,使其在MySQL全文搜索中得以应用。通过这样的组合,开发者可以构建出适应多语言环境的高效搜索系统。
MySQL全文索引增强** CoreSeek通过与MySQL的紧密集成,提供了一种无缝的全文索引解决方案。用户可以通过SQL语句进行全文检索,同时保持对MySQL数据库的原生操作。这不仅简化了开发流程,也降低了系统的学习成本。 ...
4. **全文索引**:适用于对文本类型的列进行全文搜索,自MySQL 3.23.23版本开始支持。创建方式: - 创建表时指定全文索引:`CREATE TABLE 表名 (..., FULLTEXT INDEX [索引名] (列名列表));` - 修改表添加全文索引...
Solr3.6用DIH组件进行MySQL数据库全文索引部署包 完整的工程部署包 apache-solr-3.6.0.xml 放入apache-tomcat-7.0.27\conf\Catalina\localhost
③、标准插件式:以MySQL 5.1全文索引的标准插件形式开发,不修改MySQL源代码,不影响MySQL的其他功能,可快速跟进MySQL新版本; ④、支持版本多:支持所有的MySQL 5.1 Release Candidate版本,即MySQL 5.1.22 ...
MySQL全文搜索功能主要依赖于`MATCH()`和`AGAINST()`两个函数,用于在MyISAM表类型的字段上执行高效的文本搜索。以下是对标题和描述中所述知识点的详细说明: 1. **全文搜索的前提条件** - 表的存储引擎必须是...
MySQL 的索引分为两种主要类型:MyISAM 使用非聚集索引,索引与数据分开存储,而 InnoDB 使用聚集索引,索引和数据在同一结构中,因此 InnoDB 的索引支持更快的查找,但不支持全文检索。在索引优化方面,最左前缀...
- **全文索引**:在MyISAM和计划在MySQL 5.6版本的InnoDB中可用,用于全文本搜索,如搜索引擎功能。 ### B-Tree索引详解 B-Tree索引具有多种实现方式,它们共享相同的加速操作特性,但根据内存和磁盘的不同使用...