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.
因为在一个文本中,创建索引是一个无穷大的数,因此,对
一些常用词和字符,就不会创建,这些词称为停止词。
全文索引,主要是对文件,文本的检索,比如文章。
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.
因为在一个文本中,创建索引是一个无穷大的数,因此,对
一些常用词和字符,就不会创建,这些词称为停止词。
相关文章推荐
- Mysql 常用函数
- mysql 无法远程登陆解决办法
- Mysql分库分表方案
- mysql备份之Xtrabackup
- MySQL表结构为InnoDB类型从ibd文件恢复数据
- Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份)
- MySql导入导出数据库(含远程导入导出)
- mysql5.5 主从备份设置
- MySQL High Availability总结(二) 配置Master
- Mac MySql的安装配置
- MySQL 中 EXISTS 的用法
- 【MySql】ERROR 1045 (28000): Access denied for user 'ambari'@'localhost' (using password: YES)
- 【MYSQL】修改密码
- MySQL Windows上编码问题和解决方法
- Mysql步长及删除等小方法整理
- MySQL高级十五——表的分区
- mysql通过frm+ibd文件还原data
- MYSQL数据库根据data文件中的.frm和ibd文件恢复单表数据
- mysql查看'datadir'目录
- MySQL数据类型及范围用法一览表