I'm playing around with postgresql 9.3's hstore. I'm trying to use and index for an hstore column just like documentation states. MY problem is that the index appear not to be used. Let me give you an example:
I created a table 'Person':
=# CREATE TABLE Person (Id BIGSERIAL PRIMARY KEY NOT NULL, Values hstore);
And inserted a test value:
=# INSERT INTO Person (Values, 'a=>1,b=>3');
Then if I EXPLAIN a SELECT query which uses operator "@>" on 'Values' column, I unsurprisingly get:
=# EXPLAIN SELECT P.* FROM Person AS P WHERE P.Values @> hstore('a', '1');
QUERY PLAN
----------------------------------------------------------
Seq Scan on person p (cost=0.00..24.50 rows=1 width=40)
Filter: ("values" @> '"a"=>"1"'::hstore)
No index < - > sequential scan. Makes sense. Anyway, it doesn't matter if I create a GIN or GIST index, the explain keeps talking about sequential scan:
=# CREATE INDEX IX_GIN_VALUES ON Person USING GIN (values);
CREATE INDEX
=# EXPLAIN SELECT P.* FROM Person P WHERE P.values @> hstore('a', '1');
QUERY PLAN
----------------------------------------------------------
Seq Scan on person p (cost=0.00..1.01 rows=1 width=246)
Filter: ("values" @> '"age"=>"2"'::hstore)
Maybe I'm missing something obvious?
If you're just playing with it, be sure to add enough data for the index scan to make sense. If you only have a few rows, or if many row contains similar values (i.e. your where criteria isn't selective enough), a seq scan will usually be faster than an index scan.
Also, be sure to
analyze
your table after filling it with your test data.Some extra reading for @maxm:
What is a "Bitmap heap scan" in a query plan?
PostgreSQL query very slow with limit 1
Maximum amount of entries in the histogram_bounds
Are postgres JSON indexes efficient enough compared with classic normalized tables?
(Performance has greatly improved since the latter was written.)
Because it's faster for Postgres to seq scan the entire table (which has a single row) and filter the row out of a single disk page, than it is to do the index look up, and then seq scan the table likewise in order to retrieve the row's data.
None, but see the above link on when it's better to use normalized data.
And prefer json or jsonb rather than hstore.
Nothing, but again see the above link on when it's better to use normalized data.