Mysql应用mysql索引与mysql索引优化查询
《Mysql应用mysql索引与mysql索引优化查询》要点: 本节内容: 恰当的索引可以加快查询速度,可以分为四种类型:主键、唯一索引、全文索引、普通索引. alter table customer modify id int not null,add primary key(id); 普通索引:允许重复的值出现. create table tablename add index [indexname] (fieldname1 [fieldname2...]); alter table slaes add index(value); 全文索引:用来对大表的文本域(char,varchar,text)进行索引. 使用全文索引:MYSQL实例 create table ft2 (f1 varchar(255),fulltext(f1));insert into ft2 values(‘wating for the bvarbariands‘),(‘in the heart of the country‘),(‘the master of petersburg‘),(‘writing and being‘),(‘heart of the beast‘),(‘master master‘); select * from ft2 where match(f1) against(‘master‘); // match()-匹配域;against()匹配值. ? mysql会对某些字忽略,造成查询的误差:a. 50%以上的域出现的单词;b.少于三个字的单词;c.mysql预定义的列表,包括the.查询语句:select * from ft2 where match(f1) against(‘the master‘); // 与希望的结果是不同的 ? mysql4的新功能-布尔全文查询:MYSQL实例 select * from ft2 where match(f1) against(‘+master -pet‘ in boolean mode); // 运算符类型 +-<>()~*"唯一索引:除了不能有重复的记录外,其它和普通索引一样. alter table ui_test add unique(f2); ? 对域(varchar,char,blob,text)的部分创建索引:alter table customer add index (surname(10)); alter table tablename modify fieldname columntype auto_increment; ? last_insert_id()函数返回最新插入的自动增加值. 重置自动增加计数器的值: alter table tablename auto_increment=50; ? 如果重置的值比存在的值小,自动增加计数器会从记录中最大的那个值开始增加计数,比如customer表中的id已经有1、2、3、15、16、20,当把自动增加计数器的值设为1时,下次插入的记录会从21开始. insert into staff(rank,position) values(‘employee‘,‘cleaner‘),(‘cotractor‘,‘network maintenance‘),(‘manager‘,‘sales manager‘); ? 在对每个级别添加一些数据,会看到熟悉的自动增加现象: ? 在这种情况下是不能重置自动增加计数器的. alter table table drop index indexname; drop index on tablename; ? 高效使用索引:下面讨论的是用了索引会给我们带来什么? 2.) 查找max()和min()值时,mysql只需在排序的索引中查找第一个和最后一个值. 3.) 返回的部分是索引的一部分,mysql就不需要去查询全表的数据而只需看索引:select id from customer; 4.) 对域使用order by的地方:select * from customer order by surname; 5.) 还可以加速表的连接:select first_name,surname,commission from sales,sales_rep where sales.sales_rep=sales_rep.employee_number and code=8; 6.) 在通配符的情况下:select * from sales_rep where surname like ‘ser%‘; 这种情况就不能起作用:select * from sales_rep where surname like ‘%ser%‘; ? 选择索引: alter table customer add index(surname,initial,first_name); update customer set initial=‘x‘ where id=1; update customer set initial=‘c‘ where id=2; update customer set initial=‘v‘ where id=3; update customer set initial=‘b‘ where id=4; update customer set initial=‘n‘ where id=20; update customer set initial=‘m‘ where id=21; ? 如果在查询中使用了这三个域,那就最大限度的利用了索引:MYSQL实例 select * from customer where surname=‘clegg‘ and initial=‘x‘ and first_name=‘yvonne‘;? 或者是利用索引的大部分:MYSQL实例 select * from customer where surname=‘clegg‘ and initial=‘x‘;? 或仅仅是surname:MYSQL实例 select * from customer where surname=‘clegg‘;? 如果打破最左边规则,下面的例子就不会用到索引:select * from customer where initial=‘x‘ and first_name=‘yvonne‘; select * from customer where first_name=‘yvonne‘; select * from customer where surname=‘clegg‘ and first_name=‘yvonne‘; 使用explain-解释mysql如何使用索引来处理select语句及连接表的. explain select * from sales_rep left join sales on sales.sales_rep = sales_rep.employee_number; ? 结果如下: 这个结果表示sales_rep表有个不好的连接类型-all,没用到索引,要查询的行数为5;sales的连接类型为ref,可用的索引是sales_rep,实际也使用sales_rep索引,这个索引的长度是5,对应的列是employee_number,要查询的行数为2,所以这次查询对表共进行了5×2次查询.MYSQL实例 查看索引信息:show index from tablename; 欢迎参与《Mysql应用mysql索引与mysql索引优化查询》讨论,分享您的想法,编程之家PHP学院为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |