您的位置:首页 > 数据库 > MySQL

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

2016-01-13 23:54 666 查看
创建全文索引

全文索引,主要是对文件,文本的检索,比如文章。
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.

因为在一个文本中,创建索引是一个无穷大的数,因此,对
一些常用词和字符,就不会创建,这些词称为停止词

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: