Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bPdAG-0007v6-Hr for pgsql-sql@arkaria.postgresql.org; Tue, 19 Jul 2016 22:06:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bPdAG-0001y9-51 for pgsql-sql@arkaria.postgresql.org; Tue, 19 Jul 2016 22:06:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bPdAF-0001xk-Es for pgsql-sql@postgresql.org; Tue, 19 Jul 2016 22:06:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bPdAD-00049C-1u for pgsql-sql@postgresql.org; Tue, 19 Jul 2016 22:06:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u6JM6oPK016533; Tue, 19 Jul 2016 18:06:50 -0400 From: Tom Lane To: adam cc: pgsql-sql@postgresql.org Subject: Re: posgres optimizer not using the index on hstore HELP In-reply-to: <1468855122279-5912464.post@n5.nabble.com> References: <1468855122279-5912464.post@n5.nabble.com> Comments: In-reply-to adam message dated "Mon, 18 Jul 2016 08:18:42 -0700" Date: Tue, 19 Jul 2016 18:06:50 -0400 Message-ID: <16532.1468966010@sss.pgh.pa.us> X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org adam writes: > I have a simple query select on the hstore attribute "EMAIL_ADDRESS". but the > thing is i'm using a parameter in the query to specify which hstore > attribute I want to access, and when using a parameter the index which I > created is suppressed; whereas when I hard code the attribute > "EMAIL_ADDRESS" then the index is used. WFM: regression=# create extension hstore; CREATE EXTENSION regression=# create table foo(f1 hstore); CREATE TABLE regression=# create index on foo ((f1->'bar')); CREATE INDEX regression=# explain select * from foo where f1->'bar' = 'baz'; QUERY PLAN --------------------------------------------------------------------------- Bitmap Heap Scan on foo (cost=4.21..14.37 rows=7 width=32) Recheck Cond: ((f1 -> 'bar'::text) = 'baz'::text) -> Bitmap Index Scan on foo_expr_idx (cost=0.00..4.21 rows=7 width=0) Index Cond: ((f1 -> 'bar'::text) = 'baz'::text) (4 rows) regression=# prepare p as select * from foo where f1->$1 = $2; PREPARE regression=# explain execute p('bar', 'baz'); QUERY PLAN --------------------------------------------------------------------------- Bitmap Heap Scan on foo (cost=4.21..14.37 rows=7 width=32) Recheck Cond: ((f1 -> 'bar'::text) = 'baz'::text) -> Bitmap Index Scan on foo_expr_idx (cost=0.00..4.21 rows=7 width=0) Index Cond: ((f1 -> 'bar'::text) = 'baz'::text) (4 rows) As a general rule, when you haven't mentioned the PG version you're using nor provided an *exact* example of what you're doing, it's difficult to offer useful help. regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql