简体   繁体   中英

PostgresQL — GIN index and intersection with B-trees?

I have two indexes:

CREATE INDEX table_a_b ON table (a, b);
CREATE INDEX table_c_gin ON table USING GIN(c);

My queries look like this:

SELECT * FROM table WHERE a = 'test' and b = 1 and c @> '{"test1", "test2"}'::text[];

The query planner prints out this:

 Index Scan using table_a_b on table  (cost=0.13..8.15 rows=1 width=52)
 Index Cond: (((a)::text = 'test'::text) AND (b = 1))
 Filter: (c @> '{test1, test2}'::text[])

So is there any way I could make the GIN index work, too? Maybe there's a way to create a composite index with two different index types?

Appreciate any help.

If the planner would think that it is worth doing, it could use a bitmap index scan on table_a_b and combile the two results. You'd have to look at the output of EXPLAIN to understand why it doesn't choose to do that.

If you want to create a combined index, you have to install the btree_gin extension. Then you can use text and integer columns in a GIN index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM