0
CREATE TABLE hierarchy_table(id integer PRIMARY KEY,path ltree);

INSERT INTO hierarchy_table
VALUES (1, '1'),
(2,'1.2'),
(3,'1.2.3'),
(4,'1.2.4'),
(5,'1.5'),
(6,'1.5.6'),
(7,'1.5.7'),
(8,'1.5.8');   

CREATE INDEX idx_hierarchy_table_gist ON hierarchy_table USING gist(path);


explain analyze select * from hierarchy_table where '1.2' @> path

结果:

hierarchy_table 上的 Seq Scan(成本=0.00..1.10 行=1 宽度=36)(实际时间=0.009..0.011 行=3 循环=1)

http://sqlfiddle.com/#!17/6e363/2

4

2 回答 2

1

一个有 8 行的表对索引毫无意义,对测试也毫无意义。

于 2021-12-02T19:26:31.950 回答
0

我观察到,如果我在插入之前创建了 gist 索引,它可以与索引一起使用。

但是所以重新创建索引是危险的:)

于 2021-12-02T19:14:58.457 回答