postgresql - How can I get an hstore query that searches multiple terms to use indexes? -


i have query underperforming, data hstore column:

select "vouchers".* "vouchers" "vouchers"."type" in ('vouchertype') , ((data -> 'giver_id')::integer = 1) , ((data -> 'recipient_email') null)

i've tried adding following indexes:

create index free_boxes_recipient on vouchers using gin ((data->'recipient_email')) ((data->'recipient_email') null);

create index voucher_type_giver on vouchers using gin ((data->'giver_id')::int)

as overall index: create index voucher_type_data on vouchers using gin (data)

here's current query plan:

seq scan on vouchers  (cost=0.00..15158.70 rows=5 width=125) (actual time=122.818..122.818 rows=0 loops=1)   filter: (((data -> 'recipient_email'::text) null) , ((type)::text = 'vouchertype'::text) , (((data -> 'giver_id'::text))::integer = 1))   rows removed filter: 335148 planning time: 0.196 ms execution time: 122.860 ms 

how can index hstore column down more reasonable query?

for the documentation:

hstore has gist , gin index support @>, ?, ?& , ?| operators.

you searching integer value can use simple btree index this:

create index on vouchers (((data->'giver_id')::int));  explain analyse select *  vouchers vtype in ('vouchertype')  , (data -> 'giver_id')::integer = 1  , (data -> 'recipient_email') null;                                                           query plan                                                          ----------------------------------------------------------------------------------------------------------------------------  bitmap heap scan on vouchers  (cost=4.66..82.19 rows=1 width=34) (actual time=0.750..0.858 rows=95 loops=1)    recheck cond: (((data -> 'giver_id'::text))::integer = 1)    filter: (((data -> 'recipient_email'::text) null) , (vtype = 'vouchertype'::text))    heap blocks: exact=62    ->  bitmap index scan on vouchers_int4_idx  (cost=0.00..4.66 rows=50 width=0) (actual time=0.018..0.018 rows=95 loops=1)          index cond: (((data -> 'giver_id'::text))::integer = 1)  planning time: 2.115 ms  execution time: 0.896 ms (8 rows) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -