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

Result:

Seq Scan on hierarchy_table (cost=0.00..1.10 rows=1 width=36) (actual time=0.009..0.011 rows=3 loops=1)

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

2
  • 2
    For a tiny table like that, using an index won't make things faster to begin with Commented Dec 2, 2021 at 19:23
  • 1
    Why would it? It would take longer to do the lookup in the index and use that to find the rows in the table then to just do the Seq Scan. Commented Dec 2, 2021 at 19:32

2 Answers 2

1

A table with 8 rows is pointless to index, and pointless to test with.

Sign up to request clarification or add additional context in comments.

1 Comment

You have right, if I create index before inserts then use index, but after ANALYZE doesn't! Thanks.
0

I have observered, if I have created gist index before inserts it works with index.

But so the recreate index is dangerous :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.