全文索引的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_34828933/article/details/80555570

百度搜索怎么做到快速检索的呢,是用到了全文索引,以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;

注意亮点:

1、全文索引的存储引擎一定是myisam,InnoDB没有全文索引

2、mysql的fulltext不支持中文,做分词搜索可以sphinx技术来处理中文

INSERT INTO articles(title,body) VALUES
 ('nihao','hello'),
 ('hei','hello xiaoming'),
 ('mysql','innodb');

假如我们要匹配body内容是hello的数据

select * from articles where body like 'hello%';

这样全文索引建了和没建是一样的,那么我们应该这样写

select * from articles where match(title,body) against('hello') ;

大家可以用explain 来对比一下以上两句sql是否用了索引

explain select * from articles where body like 'hello%' \G; 
explain select * from articles where match(title,body) against('hello') \G;

再给大家介绍一下停止词

在50%以上的记录中都会出现的词,不会被做全文索引,也就是说做全文索引,必须是海量数据

 

猜你喜欢

转载自blog.csdn.net/sinat_34828933/article/details/80555570