postgresql - btree_gin index is not used for sorting -


let's assume have table:

create table test.widgets (   id          bigserial primary key,   categories  int[] not null ); 

with btree_gin index:

create extension btree_gin; create index widgets_idx on test.widgets using gin (categories, id); 

i insert test data:

insert test.widgets (categories) (   select array[       (random() * 1000)::int,       (random() * 1000)::int,       (random() * 1000)::int,       (random() * 1000)::int   ]   generate_series(1,100000) ); 

then want query widgets given category, ordered id:

select id, categories test.widgets array[48] <@ widgets.categories order id; 

instead of using btree_gin index sorting, sorting done in memory:

sort  (cost=2124.05..2125.45 rows=561 width=40) (actual time=5.107..5.120 rows=402 loops=1)   sort key: id   sort method: quicksort  memory: 56kb   ->  bitmap heap scan on widgets  (cost=1244.35..2098.43 rows=561 width=40) (actual time=4.759..5.061 rows=402 loops=1)         recheck cond: ('{48}'::integer[] <@ categories)         heap blocks: exact=320         ->  bitmap index scan on widgets_idx  (cost=0.00..1244.21 rows=561 width=0) (actual time=4.700..4.700 rows=402 loops=1)               index cond: ('{48}'::integer[] <@ categories) planning time: 0.158 ms execution time: 5.171 ms 

how make sure query leverages index?


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -