pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Igor Neyman <ineyman@perceptron.com>
To: gmb <gmbouwer@gmail.com>
To: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: View not using index
Date: Tue, 15 Sep 2015 13:29:09 +0000
Message-ID: <A76B25F2823E954C9E45E32FA49D70ECCD515FDC@mail.corp.perceptron.com> (raw)
In-Reply-To: <1442318204445-5865953.post@n5.nabble.com>
References: <1442318204445-5865953.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

CREATE TABLE detail ( invno VARCHAR, accno INTEGER, info INTEGER[] );

CREATE OR REPLACE VIEW detailview AS
( SELECT invno , accno , COALESCE( info[1],0 ) info1, COALESCE( info[2],0 ) info2, COALESCE( info[3],0 ) info3, COALESCE( info[4],0 ) info4 FROM detail );

CREATE INDEX detail_ix_info3 ON detail ( ( info[3]  ) ) WHERE  COALESCE(
info[3],0 ) = 1;

EXPLAIN SELECT * FROM detail WHERE COALESCE( info[3],0 ) =1;

                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Bitmap Heap Scan on detail  (cost=4.13..12.59 rows=4 width=68)
   Recheck Cond: (COALESCE(info[3], 0) = 1)
   ->  Bitmap Index Scan on detail_ix_info3  (cost=0.00..4.13 rows=4
width=0)
(3 rows)

EXPLAIN SELECT * FROM detailview WHERE COALESCE( info3,0 ) =1;

                       QUERY PLAN                       
--------------------------------------------------------
 Seq Scan on detail  (cost=0.00..20.38 rows=4 width=68)
   Filter: (COALESCE(COALESCE(info[3], 0), 0) = 1)
(2 rows)

This is an oversimplified example; the view in our production env provides for 20 elements in the info array column. My table in productions env contains ~10mil rows.

Is there any way in which I can force the view to use the index?
_______________________

Why are you applying "extra" COALESCE when querying the view?
Why not just:

SELECT * FROM detailview WHERE infor3 = 1;

?

Regards,
Igor Neyman
 


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (4+ messages)  latest in thread

Message-ID: <A76B25F2823E954C9E45E32FA49D70ECCD515FDC@mail.corp.perceptron.com>
Permalink:  ../A76B25F2823E954C9E45E32FA49D70ECCD515FDC@mail.corp.perceptron.com/
Also on:    postgresql.org/message-id/A76B25F2823E954C9E45E32FA49D70ECCD515FDC@mail.corp.perceptron.com

 · 

reply

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-sql@postgresql.org
  Cc: ineyman@perceptron.com, gmbouwer@gmail.com
  Subject: Re: View not using index
  In-Reply-To: <A76B25F2823E954C9E45E32FA49D70ECCD515FDC@mail.corp.perceptron.com>

* 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