您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
mysql中的全文索引与停止词
发布时间:2015-11-19 19:38:29编辑:雪饮阅读()
全文索引的创建:
create table member(id int not null default '0',email varchar(30),tel char(11),intro text);
若是创建失败请检查你的表存储引擎是否是myisam,默认的innodb存储引擎是不支持的全文索引的。
修改表存储引擎为myisam:
alter table member engine=myisam;
添加一个全文索引的字段:
alter table member add fulltext (intro);
向全文索引的字段添加数据:
insert into member values(1,'a@a.com','110','这里是雪饮个人博客');
select * from member where intro like '%itcast%';
传统的索引来查询intro字段中包含“itcast”的方法:
select * from member where intro like '%itcast%';
该查询效率底下
select id,email,match(intro) against('itcast') from member;
匹配度为0
若你用全文索引发现匹配度为0,那么你需要检查下你的数据中你检索的值是否在记录中以单词的形式存在,还有就是你若检索中文一般也会很难匹配到。如下数据是一种正确的全文索引匹配:
insert into member values(1,'w@qq.com','123','ni hao wo shi xueyin');
select id,email,match(intro) against('xueyin') from member;
+----+----------+--------------------------------+
| id | email | match(intro) against('xueyin') |
+----+----------+--------------------------------+
| 1 | a@a.com | 0 |
| 1 | a@a.com | 0 |
| 3 | q@qq.com | 0 |
| 1 | w@qq.com | 0 |
| 1 | w@qq.com | 1.3705332279205322 |
+----+----------+--------------------------------+
5 rows in set (0.00 sec)
+----+----------+--------------------------------+
| id | email | match(intro) against('xueyin') |
+----+----------+--------------------------------+
| 1 | a@a.com | 0 |
| 1 | a@a.com | 0 |
| 3 | q@qq.com | 0 |
| 1 | w@qq.com | 0 |
| 1 | w@qq.com | 1.3705332279205322 |
+----+----------+--------------------------------+
5 rows in set (0.00 sec)
在如你这样索引则会失败:
select id,email,match(intro) against('雪饮个人博客') from member;
+----+----------+--------------------------------------+
| id | email | match(intro) against('雪饮个人博客') |
+----+----------+--------------------------------------+
| 1 | a@a.com | 0 |
| 1 | a@a.com | 0 |
| 3 | q@qq.com | 0 |
| 1 | w@qq.com | 0 |
+----+----------+--------------------------------------+
4 rows in set (0.00 sec)
当然还有另外一种情况,也就是所谓的停止词
停止词一般是常见的单词,就如百度里面你搜索一个“的”、“是”等这种无所谓的词一般是没有百度百科一样
若是什么词都做了全文索引,那么就无法体现全文索引所带来的性能优势了
匹配度是逐行匹配的
select id,email,match(intro) against('雪饮个人博客') from member;
+----+----------+--------------------------------------+
| id | email | match(intro) against('雪饮个人博客') |
+----+----------+--------------------------------------+
| 1 | a@a.com | 0 |
| 1 | a@a.com | 0 |
| 3 | q@qq.com | 0 |
| 1 | w@qq.com | 0 |
+----+----------+--------------------------------------+
4 rows in set (0.00 sec)
当然还有另外一种情况,也就是所谓的停止词
停止词一般是常见的单词,就如百度里面你搜索一个“的”、“是”等这种无所谓的词一般是没有百度百科一样
若是什么词都做了全文索引,那么就无法体现全文索引所带来的性能优势了
匹配度是逐行匹配的
关键字词:mysq,全文索引,停止词,雪饮个人博客
下一篇:mysql温故而知新