On this page
F.7. btree_gist
btree_gist
提供了 GiST 索引运算符类别,这些类别针对数据类型int2
,int4
,int8
,float4
,float8
,numeric
,timestamp with time zone
,timestamp without time zone
,time with time zone
,time without time zone
,date
,interval
,oid
,money
,char
,varchar
实现了 B 树等效行为,bytea
,bit
,varbit
,macaddr
,macaddr8
,inet
,cidr
,uuid
和所有enum
类型。
通常,这些运算符类不会优于等效的标准 B 树索引方法,并且它们缺少标准 B 树代码的一个主要功能:强制执行唯一性的能力。但是,它们提供了 B 树索引不具备的一些其他功能,如下所述。同样,这些运算符类在需要多列 GiST 索引时非常有用,其中某些列的数据类型只能用 GiST 索引,而其他列只是简单的数据类型。最后,这些运算符类对于 GiST 测试非常有用,并且可以作为开发其他 GiST 运算符类的基础。
除了典型的 B 树搜索运算符之外,btree_gist
还为<>
提供索引支持(“不等于”)。如下所述,与exclusion constraint结合使用可能很有用。
另外,对于具有自然距离度量的数据类型,btree_gist
定义距离运算符<->
,并为使用该运算符的最近邻搜索提供 GiST 索引支持。为int2
,int4
,int8
,float4
,float8
,timestamp with time zone
,timestamp without time zone
,time without time zone
,date
,interval
,oid
和money
提供了距离运算符。
F.7.1. 用法示例
使用btree_gist
而不是btree
的简单示例:
CREATE TABLE test (a int4);
-- create index
CREATE INDEX testidx ON test USING GIST (a);
-- query
SELECT * FROM test WHERE a < 10;
-- nearest-neighbor search: find the ten entries closest to "42"
SELECT *, a <-> 42 AS dist FROM test ORDER BY a <-> 42 LIMIT 10;
使用exclusion constraint来执行以下规则:动物园的笼子只能容纳一种动物:
=> CREATE TABLE zoo (
cage INTEGER,
animal TEXT,
EXCLUDE USING GIST (cage WITH =, animal WITH <>)
);
=> INSERT INTO zoo VALUES(123, 'zebra');
INSERT 0 1
=> INSERT INTO zoo VALUES(123, 'zebra');
INSERT 0 1
=> INSERT INTO zoo VALUES(123, 'lion');
ERROR: conflicting key value violates exclusion constraint "zoo_cage_animal_excl"
DETAIL: Key (cage, animal)=(123, lion) conflicts with existing key (cage, animal)=(123, zebra).
=> INSERT INTO zoo VALUES(124, 'lion');
INSERT 0 1
F.7.2. Authors
Teodor Sigaev(<teodor@stack.net>
),Oleg Bartunov(<oleg@sai.msu.su>
),Janko Richter(<jankorichter@yahoo.de>
)和 Paul Jungwirth(<pj@illuminatedcomputing.com>
)。有关其他信息,请参见http://www.sai.msu.su/~megera/postgres/gist/。