17-mysql优化之全文索引的创建

创建全文索引
全文索引, 主要是对文件,文本的检索,比如文章。
create table articles(id int unsigned auto_increment not null primary key,title varchar(200),body text,fulltext(title,body))engine=myisam charset utf8;
全文索引针对MyISAM有用,innodb没有用

INSERT INTO articles (title,body) VALUES
     ('MySQL Tutorial','DBMS stands for DataBase ...'),
     ('How To Use MySQL Well','After you went through a ...'),
     ('Optimizing MySQL','In this tutorial we will show ...'),
     ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
     ('MySQL vs. YourSQL','In the following database comparison ...'),
     ('MySQL Security','When configured properly, MySQL ...');




错误用法:
select * from  articles where body like '%mysql%';
这样是不会使用到全文索引的。
explain select * from  articles where body like '%mysql%';
为空
正确的使用方法:
select * from articles where match(title,body) against('database');
+----+-------------+----------+----------+---------------+-------+---------+-----+------+-------------+
| id | select_type | table    | type     | possible_keys | key   | key_len | ref | rows | Extra       |
+----+-------------+----------+----------+---------------+-------+---------+-----+------+-------------+
|  1 | SIMPLE      | articles | fulltext | title         | title | 0       |     |    1 | Using where |
+----+-------------+----------+----------+---------------+-------+---------+-----+------+-------------+
possible_keys keys不为空


说明:
1.在mysql中fulltext索引只针对myisam生效
2.只针对英文生效--->用sphinx技术处理中文
3.使用方法是match(字段名)against('关键字')

4.全文索引——停止词


匹配度:

select match(title,body)against('database') from articles;
+--------------------------------------+
| match(title,body)against('database') |
+--------------------------------------+
|                     0.65545834044456 |
|                                    0 |
|                                    0 |
|                                    0 |
|                     0.66266459031789 |
|                                    0 |
+--------------------------------------+
匹配度65%,66%第一条数据,第二条数据。。。。
select match(title,body)against('a') from articles;
结果都是0.
因为在一个文本中,创建索引是一个无穷大的数,因此,对
一些常用词和字符,就不会创建,这些词称为停止词
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会编程的阿强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值