索引总结
索引为什么提高了查询的速率,但是update delete insert 的速度会变慢
因为:这三种操作会破坏二叉树的,所以会造成速度变慢
什么样的索引使用什么样的方法进行查询
对于模糊查询
1 使用全文索引的目的就是为了弥补,其他的普通索引不能在前面不能加'%'的模糊查询。
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
body TEXT,
FULLTEXT (title,body) -- --如果只有这句话是不生效的只有针对myIsam 生效--
)engine=myisam charset utf8; -- --全文索引针对MyISAM有用--
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 ...');
show indexes from articles;
desc articles
如果mysql估计使用全表扫描要比使用索引快,则不使用索引。
如何查看索引使用的情况:
show status like ‘Handler_read%’;
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数。
handler_read_rnd_next:这个值越高,说明查询低效。
错误用法: 这个possible_key 是空的,索引没有用到全文索引 正确的用法是:select * from articles where match(title,body) against(‘database’); 【可以】 database 表示的是含有database 的字段 可以看出来真的是可以找到database??????不懂
字符串的概率
只会是针对不太常用的词才会创建索引 Eg 常用的词不会创建索引,就像a字符串,查找的匹配度为0 ,就是没有创建索引,这种词叫做停止词。 Selectmatch(title,body) against('a') from articles;
利用sphinx (coreseek) 技术处理中文 |
|
对于使用like的查询,查询如果是 ‘%aaa’ 不会使用到索引 如果一定要前面有变化的值,则考虑使用 全文索引->sphinx. |