使用mysql innodb或myisam引擎创建fulltext全文索引

mysql 5.5以后就支持了innodb引擎创建全文索引了。我看网上有写博主说全文索引只有myisam引擎支持,在这里我坚决辟谣 了。接下来看我的一顿操作

以下来自mysql官网的sql

#创建表并指定全文索引的列

CREATE TABLE articles (
         id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
         title VARCHAR(200),
         body TEXT,
         FULLTEXT (body)
       ) ENGINE=InnoDB default charset = utf8;

#插入数据

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 ...');
             
     #全文索引特定的语法。match() against()
  select * from articles where match(body) against('DBMS Tutorial')

为啥要用全文索引呢?用like模糊查询不行吗?

答案:在文本比较少时是合适的,但是对于大量的文本数据检索,是不可想象的。全文索引在大量的数据面前比like模糊查询块多了

最后补充一句。全文索引fulltext。只对char ,varchar,text类型的字段有效哦

猜你喜欢

转载自blog.csdn.net/saygood999/article/details/109124042