From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Barker <mikeb01@gmail.com>
Cc: pgsql-performance <pgsql-performance@postgresql.org>
Subject: Re: Query doesn't use index on hstore column
Date: Thu, 04 Dec 2014 20:32:50 -0500
Message-ID: <20532.1417743170@sss.pgh.pa.us> (raw)
In-Reply-To: <CALwNKeTZyva2ev5dFDXs4PX01yk27Rk5-UCSYhCWh+jnyDTNNA@mail.gmail.com>
References: <CALwNKeTZyva2ev5dFDXs4PX01yk27Rk5-UCSYhCWh+jnyDTNNA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
Michael Barker <mikeb01@gmail.com> writes:
> I'm currently experimenting with hstore on Posgtres 9.4rc1. I've created a
> table with an hstore column, with and index on that column (tried both gin
> and btree indexes) and the explain plan says that the index is never used
> for the lookup and falls to a sequential scan every time (table has 1 000
> 000 rows). The query plans and execution time for btree index, gin index
> and unindexed are the same. Is there something I'm doing wrong or missing
> in order to get indexes to work on hstore columns?
Well, first off, a btree index is fairly useless for this query,
because btree has no concept that the hstore has any sub-structure.
A GIN index or GIST index could work though. Secondly, you have to
remember that indexable WHERE conditions in Postgres are *always* of
the form "WHERE indexed_column indexable_operator some_comparison_value".
So the trick is to recast the condition you have into something that
looks like that. Instead of
WHERE attributes->'accountId' = '1879355460'
you could do
WHERE attributes @> 'accountId=>1879355460'
(@> being the hstore containment operator, ie "does attributes contain
a pair that looks like this?") or equivalently but possibly easier to
generate,
WHERE attributes @> hstore('accountId', '1879355460')
Another possibility if you're only concerned about indexing searches
for one or a few specific keys is to use expression indexes:
CREATE INDEX ON audit ((attributes->'accountId'));
whereupon your original query works, since the left-hand side of
the '=' operator is now the indexed expression. (Here, since you
are testing plain equality on the indexed value, a btree works fine.)
You might care to read
http://www.postgresql.org/docs/9.4/static/indexes.html
to get a better handle on what Postgres indexes can and can't do.
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, mikeb01@gmail.com
Subject: Re: Query doesn't use index on hstore column
In-Reply-To: <20532.1417743170@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox