赞
踩
● 索引是用于加速数据存取的数据对象。合理的使用索引可以大大降低 i/o 次数,从而提高数据访问性能。
● 索引有很多种我们主要介绍常用的几种:
为什么添加了索引之后,会加快查询速度呢?
● 语法:
CREATE index 索引名 on 表名(列名)
Create index emp_idx1 on emp(ename,job);
Create index emp_idx1 on emp(job,ename);
● 范例:给 person 表的 name 建立索引
● 范例:给 person 表创建一个 name 和 gender 的索引
● 索引的使用原则:
以上操作完整源码:
select * from person;
--创建单例索引
create index pname_index on person(pname);
--索引不需要主动使用,根据索引列查询时就自动的使用了索引
select * from person t where t.pname = '魏宇轩';
--复合索引,查询数据使用的时候是有索引顺序的,下面的两种创建是有区别的
create index pg_index on person(pname, age);
create index gp_index on person(age, pname);
--下面这 sql 使用 pg_index
select * from person t where t.pname = '魏宇轩' and t.age = 21;
--下面这 sql 使用 gp_index
select * from person t where t.age = 21 and t.pname = '魏宇轩';
如有错误,欢迎指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。