如何获得搜索多个术语以使用索引的 hstore 查询?
How can I get an hstore query that searches multiple terms to use indexes?
我有一个性能不佳的查询,数据是 hstore
列:
SELECT "vouchers".* FROM "vouchers" WHERE "vouchers"."type" IN ('VoucherType') AND ((data -> 'giver_id')::integer = 1) AND ((data -> 'recipient_email') is NULL)
我试过添加以下索引:
CREATE INDEX free_boxes_recipient ON vouchers USING gin ((data->'recipient_email')) WHERE ((data->'recipient_email') IS NULL);
CREATE INDEX voucher_type_giver ON vouchers USING gin ((data->'giver_id')::int)
以及综合指数:CREATE INDEX voucher_type_data ON vouchers USING gin (data)
这是当前的查询计划:
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) IS NULL) AND ((type)::text = 'VoucherType'::text) AND (((data -> 'giver_id'::text))::integer = 1))
Rows Removed by Filter: 335148
Planning time: 0.196 ms
Execution time: 122.860 ms
如何索引这个 hstore 列以将其归结为更合理的查询?
hstore has GiST and GIN index support for the @>, ?, ?& and ?| operators.
您正在搜索一个整数值,因此您可以像这样使用简单的 btree 索引:
CREATE INDEX ON vouchers (((data->'giver_id')::int));
EXPLAIN ANALYSE
SELECT *
FROM vouchers
WHERE vtype IN ('VoucherType')
AND (data -> 'giver_id')::integer = 1
AND (data -> 'recipient_email') is 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) IS NULL) AND (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)
我有一个性能不佳的查询,数据是 hstore
列:
SELECT "vouchers".* FROM "vouchers" WHERE "vouchers"."type" IN ('VoucherType') AND ((data -> 'giver_id')::integer = 1) AND ((data -> 'recipient_email') is NULL)
我试过添加以下索引:
CREATE INDEX free_boxes_recipient ON vouchers USING gin ((data->'recipient_email')) WHERE ((data->'recipient_email') IS NULL);
CREATE INDEX voucher_type_giver ON vouchers USING gin ((data->'giver_id')::int)
以及综合指数:CREATE INDEX voucher_type_data ON vouchers USING gin (data)
这是当前的查询计划:
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) IS NULL) AND ((type)::text = 'VoucherType'::text) AND (((data -> 'giver_id'::text))::integer = 1))
Rows Removed by Filter: 335148
Planning time: 0.196 ms
Execution time: 122.860 ms
如何索引这个 hstore 列以将其归结为更合理的查询?
hstore has GiST and GIN index support for the @>, ?, ?& and ?| operators.
您正在搜索一个整数值,因此您可以像这样使用简单的 btree 索引:
CREATE INDEX ON vouchers (((data->'giver_id')::int));
EXPLAIN ANALYSE
SELECT *
FROM vouchers
WHERE vtype IN ('VoucherType')
AND (data -> 'giver_id')::integer = 1
AND (data -> 'recipient_email') is 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) IS NULL) AND (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)