【发布时间】:2021-12-02 19:14:58
【问题描述】:
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)
【问题讨论】:
-
对于这样的小表,使用索引不会让事情开始变得更快
-
为什么会这样?在索引中进行查找并使用它来查找表中的行然后只执行
Seq Scan需要更长的时间。
标签: postgresql ltree