PostgreSQL中有几种索引类型,如B-tree,Hash,GiST,SP-GiST和GIN等。每种索引类型根据不同的查询使用不同的算法。 默认情况下,CREATE INDEX命令使用B树索引。
默认btree
在项目中经常使用单列索引,对应查询where语句搜索字段
语法
create index index_name on table_name(column_name);
多列索引,后面加多个即可
create index index_name on table_name(column_name1,column_name2...)
唯一索引,可以确保保证表中相关字段不能插入重复值。
create unique index index_name on table_name(column_name);
删除索引
drop index index_name;
可以看到,PostgreSQL的相关索引操作跟mysql,oracle数据库基本语法相似。