Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XJDxw-0001C5-5O for pgsql-performance@arkaria.postgresql.org; Mon, 18 Aug 2014 03:50:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XJDxv-0006SF-4f for pgsql-performance@arkaria.postgresql.org; Mon, 18 Aug 2014 03:50:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XJDxs-0006S2-S8 for pgsql-performance@postgresql.org; Mon, 18 Aug 2014 03:50:37 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XJDxp-00027Z-Hz for pgsql-performance@postgresql.org; Mon, 18 Aug 2014 03:50:35 +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 s7I3ng4A024244; Sun, 17 Aug 2014 23:49:42 -0400 From: Tom Lane To: Xiaoyulei cc: "pgsql-performance@postgresql.org" , yetao Subject: Re: select on index column,why PG still use seq scan? In-reply-to: References: Comments: In-reply-to Xiaoyulei message dated "Mon, 18 Aug 2014 03:19:22 -0000" Date: Sun, 17 Aug 2014 23:49:42 -0400 Message-ID: <24243.1408333782@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Xiaoyulei writes: > I create a table and insert some items. > I create index on every column. > And I execute select, I thought it should use index scan, but it is still seq scan. Why PG do not use index scan? > create table v_org_info( > org_no varchar2(8), org_nm varchar2(80), > org_no_l1 varchar2(8), org_nm_l1 varchar2(80), > org_no_l2 varchar2(8), org_nm_l2 varchar2(80) > ); There is no "varchar2" type in Postgres. I tried this example with "varchar" in place of that, but when I got to > POSTGRES=# explain analyze select a.org_nm from v_org_info a where a.org_no = 1000; I got ERROR: operator does not exist: character varying = integer LINE 1: ...ze select a.org_nm from v_org_info a where a.org_no = 1000; ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. which is certainly what I *should* get. I changed it to explain analyze select a.org_nm from v_org_info a where a.org_no = '1000'; and then I got Bitmap Heap Scan on v_org_info a (cost=4.49..74.90 rows=27 width=58) (actual t ime=0.044..0.044 rows=1 loops=1) Recheck Cond: ((org_no)::text = '1000'::text) Heap Blocks: exact=1 -> Bitmap Index Scan on idx_v_org_info_org_no (cost=0.00..4.48 rows=27 widt h=0) (actual time=0.020..0.020 rows=1 loops=1) Index Cond: ((org_no)::text = '1000'::text) Planning time: 0.481 ms Execution time: 0.104 ms which is OK, but after "ANALYZE v_org_info" I got Index Scan using idx_v_org_info_org_no on v_org_info a (cost=0.29..8.30 rows=1 width=5) (actual time=0.019..0.020 rows=1 loops=1) Index Cond: ((org_no)::text = '1000'::text) Planning time: 0.372 ms Execution time: 0.060 ms which is better. > Seq Scan on V_ORG_INFO A (cost=0.00..189.97 rows=9 width=178, batch_size=100) (actual time=0.930..18.034 rows=1 loops=1) > Filter: (INT4IN(VARCHAROUT(ORG_NO)) = 1000) > Rows Removed by Filter: 19999 > Total runtime: 18.099 ms > (4 rows) TBH, this looks like some incompetently hacked-up variant of Postgres; certainly no version ever shipped by the core project would have done this. It looks like somebody tried to make cross-type comparisons work by inserting conversion operations, but they did it in such a way that the conversions were applied to the column not the constant. An index on org_no isn't going to help you for a query on INT4IN(VARCHAROUT(ORG_NO)). (And I wonder why exactly the names are printing as upper case here ...) 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