目录
唯一索引
CREATE UNIQUE INDEX idx_set_driving_unique ON t_set_driving (
line_code, -- 线路编码
direction, -- 行别
train_number, -- 车次
monitor_set_code, -- 监测单元编码
arrive_time -- 分区键(显式包含,必须!)
);
CREATE UNIQUE INDEX idx_t_set_driving_stat_unique ON t_set_driving_stat (
line_code,
direction,
monitor_set_code,
arrive_time
);
B-Tree
CREATE TABLE test1 (
id integer,
content varchar
);
CREATE INDEX test1_id_index ON test1 (id);
B-Tree索引主要用于等于和范围查询,特别是当索引列包含操作符" <、<=、=、>=和>"作为查询条件时,PostgreSQL的查询规划器都会考虑使用B-Tree索引。在使用BETWEEN、IN、IS NULL和IS NOT NULL的查询中,PostgreSQL也可以使用B-Tree索引。然而对于基于模式匹配操作符的查询,如LIKE、ILIKE、~和 ~*,仅当模式存在一个常量,且该常量位于模式字符串的开头时,如col LIKE 'foo%'或col ~ '^foo',索引才会生效,否则将会执行全表扫描,如:col LIKE '%bar'。
Hash:
CREATE INDEX name ON table USING hash (column);
散列(Hash)索引只能处理简单的等于比较。当索引列使用等于操作符进行比较时,查询规划器会考虑使用散列索引。
复合索引
PostgreSQL中的索引可以定义在数据表的多个字段上,如:
CREATE TABLE test2 (
major int,
minor int,
name varchar
}
CREATE INDEX test2_mm_idx ON test2 (major, minor);
只有B-tree、GiST和GIN支持复合索引,其中最多可以声明32个字段
参考: https://blog.csdn.net/jubaoquan/article/details/78850899