Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WmFU0-0005Ob-Bj for pgsql-performance@arkaria.postgresql.org; Mon, 19 May 2014 04:47:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WmFTz-0001Oi-Kw for pgsql-performance@arkaria.postgresql.org; Mon, 19 May 2014 04:47:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WmFTy-0001Ob-D5 for pgsql-performance@postgresql.org; Mon, 19 May 2014 04:47:26 +0000 Received: from 222-154-234-164.adsl.xtra.co.nz ([222.154.234.164] helo=zimbra.mccarthy.co.nz) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WmFTu-0000JE-FD for pgsql-performance@postgresql.org; Mon, 19 May 2014 04:47:25 +0000 Received: from localhost (localhost [127.0.0.1]) by zimbra.mccarthy.co.nz (Postfix) with ESMTP id A7BB1110 for ; Mon, 19 May 2014 16:47:18 +1200 (NZST) Received: from zimbra.mccarthy.co.nz ([127.0.0.1]) by localhost (zimbra.mccarthy.co.nz [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id JlkWbyobUi2k for ; Mon, 19 May 2014 16:47:14 +1200 (NZST) Received: from localhost (localhost [127.0.0.1]) by zimbra.mccarthy.co.nz (Postfix) with ESMTP id C88DF26A for ; Mon, 19 May 2014 16:47:14 +1200 (NZST) X-Virus-Scanned: amavisd-new at mccarthy.co.nz Received: from zimbra.mccarthy.co.nz ([127.0.0.1]) by localhost (zimbra.mccarthy.co.nz [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id vhYCpTzNWTAd for ; Mon, 19 May 2014 16:47:14 +1200 (NZST) Received: from zimbra.mccarthy.co.nz (localhost [127.0.0.1]) by zimbra.mccarthy.co.nz (Postfix) with ESMTP id 9ED4F110 for ; Mon, 19 May 2014 16:47:14 +1200 (NZST) Date: Mon, 19 May 2014 16:47:14 +1200 (NZST) From: Geoff Hull To: pgsql-performance@postgresql.org Message-ID: <897829134.210060.1400474834336.JavaMail.zimbra@mccarthy.co.nz> In-Reply-To: <1362375391.209823.1400474409764.JavaMail.zimbra@mccarthy.co.nz> Subject: View has different query plan than select statement MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_210059_1431240683.1400474834335" X-Originating-IP: [128.222.3.145] X-Mailer: Zimbra 8.0.5_GA_5839 (ZimbraWebClient - FF29 (Win)/8.0.5_GA_5839) Thread-Topic: View has different query plan than select statement Thread-Index: apMbdwPADn8VMr/y04QE2CCb3Sey3Q== X-Pg-Spam-Score: 1.0 (+) 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 ------=_Part_210059_1431240683.1400474834335 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit I am sending this on behalf of my colleague who tried to post to this list last year but without success, then also tried pgsql-performance-owner@postgresql.org but without getting a reply. I have recently re-tested this in P/G version 9.3.4 with the same results: Hi, I have created a table 'test_table' and index 'idx_test_table' with a view 'v_test_table'. However the query plan used by the view does not use the index but when running the select statement itself it does use the index. Given that query specific hints are not available in Postgres 9.1 how can I persuade the view to use the same query plan as the select statement? Thanks, Tim --DROP table test_table CASCADE; -- create test table CREATE TABLE test_table ( history_id SERIAL, id character varying(50) NOT NULL , name character varying(50), CONSTRAINT test_table_pkey PRIMARY KEY (history_id) ); -- create index on test table CREATE INDEX idx_test_table ON test_table (id); -- populate test table INSERT INTO test_table (id, name) SELECT *, 'Danger Mouse' FROM (SELECT md5(random()::text) from generate_series(1,10000)) q; -- collect stats ANALYZE test_table; EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM test_table WHERE id = '02b304b1c54542570d9f7bd39361f5b4'; "Index Scan using idx_test_table on test_table (cost=0.00..8.27 rows=1 width=50) (actual time=0.021..0.022 rows=1 loops=1)" " Index Cond: ((id)::text = '02b304b1c54542570d9f7bd39361f5b4'::text)" " Buffers: shared hit=3" "Total runtime: 0.051 ms" -- select statement with good plan EXPLAIN (ANALYZE, BUFFERS) SELECT id, CASE WHEN COALESCE(LAG(name) OVER (PARTITION BY id ORDER BY history_id), name || 'x') <> name then name end as name FROM test_table WHERE id = '02b304b1c54542570d9f7bd39361f5b4'; "WindowAgg (cost=8.28..8.31 rows=1 width=50) (actual time=0.050..0.051 rows=1 loops=1)" " Buffers: shared hit=3" " -> Sort (cost=8.28..8.29 rows=1 width=50) (actual time=0.039..0.039 rows=1 loops=1)" " Sort Key: history_id" " Sort Method: quicksort Memory: 25kB" " Buffers: shared hit=3" " -> Index Scan using idx_test_table on test_table (cost=0.00..8.27 rows=1 width=50) (actual time=0.030..0.031 rows=1 loops=1)" " Index Cond: ((id)::text = '02b304b1c54542570d9f7bd39361f5b4'::text)" " Buffers: shared hit=3" "Total runtime: 0.102 ms" --DROP VIEW v_test_table; CREATE OR REPLACE VIEW v_test_table AS SELECT id, CASE WHEN COALESCE(LAG(name) OVER (PARTITION BY id ORDER BY history_id), name || 'x') <> name then name end as name FROM test_table; -- Query via view with bad plan EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM v_test_table WHERE id = '02b304b1c54542570d9f7bd39361f5b4'; "Subquery Scan on v_test_table (cost=868.39..1243.39 rows=50 width=65) (actual time=26.115..33.327 rows=1 loops=1)" " Filter: ((v_test_table.id)::text = '02b304b1c54542570d9f7bd39361f5b4'::text)" " Buffers: shared hit=104, temp read=77 written=77" " -> WindowAgg (cost=868.39..1118.39 rows=10000 width=50) (actual time=26.022..32.519 rows=10000 loops=1)" " Buffers: shared hit=104, temp read=77 written=77" " -> Sort (cost=868.39..893.39 rows=10000 width=50) (actual time=26.013..27.796 rows=10000 loops=1)" " Sort Key: test_table.id, test_table.history_id" " Sort Method: external merge Disk: 608kB" " Buffers: shared hit=104, temp read=77 written=77" " -> Seq Scan on test_table (cost=0.00..204.00 rows=10000 width=50) (actual time=0.010..1.804 rows=10000 loops=1)" " Buffers: shared hit=104" "Total runtime: 33.491 ms" How can I get the view to use the same query plan as the select statement? ------=_Part_210059_1431240683.1400474834335 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
I am sending this on behalf of my colleague= who tried to post to this list last year but without success, then also tr= ied pgsql-perform= ance-owner@postgresql.org but without getting a reply.

I have recently re-tested this in P/G version 9.3.4 with the same = results:

Hi,

I have created a table 'te= st_table' and index 'idx_test_table' with a view 'v_test_table'. However th= e query plan used by the view does not use the index but when running the s= elect statement itself it does use the index. Given that query specific hin= ts are not available in Postgres 9.1 how can I persuade the view to use the= same query plan as the select statement?

Thanks,

Tim

=
--DROP table test_table CASCADE;

-- create test table
CREATE = TABLE test_table (
history_id SERIAL,
id character varying(50) NOT NU= LL ,
name character varying(50),
CONSTRAINT test_table_pkey PRIMARY K= EY (history_id)
);

-- create index on test table
CREATE INDEX = idx_test_table ON test_table (id);

-- populate test table
INSERT = INTO test_table (id, name) SELECT *, 'Danger Mouse' FROM (SELECT md5(random= ()::text) from generate_series(1,10000)) q;

-- collect stats
ANAL= YZE test_table;


EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM t= est_table
WHERE id =3D '02b304b1c54542570d9f7bd39361f5b4';

"Index= Scan using idx_test_table on test_table (cost=3D0.00..8.27 rows=3D1 width= =3D50) (actual time=3D0.021..0.022 rows=3D1 loops=3D1)"
" Index Cond: ((= id)::text =3D '02b304b1c54542570d9f7bd39361f5b4'::text)"
" Buffers: shar= ed hit=3D3"
"Total runtime: 0.051 ms"


-- select statement wit= h good plan

EXPLAIN (ANALYZE, BUFFERS)
SELECT id,
CASE WHEN CO= ALESCE(LAG(name) OVER (PARTITION BY id ORDER BY history_id), name || 'x') &= lt;> name
then name
end as name
FROM test_table
WHERE id =3D= '02b304b1c54542570d9f7bd39361f5b4';

"WindowAgg (cost=3D8.28..8.31 r= ows=3D1 width=3D50) (actual time=3D0.050..0.051 rows=3D1 loops=3D1)"
" B= uffers: shared hit=3D3"
" -> Sort (cost=3D8.28..8.29 rows=3D1 width= =3D50) (actual time=3D0.039..0.039 rows=3D1 loops=3D1)"
" Sort Key: hist= ory_id"
" Sort Method: quicksort Memory: 25kB"
" Buffers: shared hit= =3D3"
" -> Index Scan using idx_test_table on test_table (cost=3D0.00= ..8.27 rows=3D1 width=3D50) (actual time=3D0.030..0.031 rows=3D1 loops=3D1)= "
" Index Cond: ((id)::text =3D '02b304b1c54542570d9f7bd39361f5b4'::text= )"
" Buffers: shared hit=3D3"
"Total runtime: 0.102 ms"


--= DROP VIEW v_test_table;

CREATE OR REPLACE VIEW v_test_table AS
SE= LECT id,
CASE WHEN COALESCE(LAG(name) OVER (PARTITION BY id ORDER BY his= tory_id), name || 'x') <> name
then name
end as name
FROM te= st_table;


-- Query via view with bad plan

EXPLAIN (ANALYZ= E, BUFFERS)
SELECT *
FROM v_test_table
WHERE id =3D '02b304b1c5454= 2570d9f7bd39361f5b4';

"Subquery Scan on v_test_table (cost=3D868.39.= .1243.39 rows=3D50 width=3D65) (actual time=3D26.115..33.327 rows=3D1 loops= =3D1)"
" Filter: ((v_test_table.id)::text =3D '02b304b1c54542570d9f7bd39= 361f5b4'::text)"
" Buffers: shared hit=3D104, temp read=3D77 written=3D7= 7"
" -> WindowAgg (cost=3D868.39..1118.39 rows=3D10000 width=3D50) (a= ctual time=3D26.022..32.519 rows=3D10000 loops=3D1)"
" Buffers: shared h= it=3D104, temp read=3D77 written=3D77"
" -> Sort (cost=3D868.39..893.= 39 rows=3D10000 width=3D50) (actual time=3D26.013..27.796 rows=3D10000 loop= s=3D1)"
" Sort Key: test_table.id, test_table.history_id"
" Sort Meth= od: external merge Disk: 608kB"
" Buffers: shared hit=3D104, temp read= =3D77 written=3D77"
" -> Seq Scan on test_table (cost=3D0.00..204.00 = rows=3D10000 width=3D50) (actual time=3D0.010..1.804 rows=3D10000 loops=3D1= )"
" Buffers: shared hit=3D104"
"Total runtime: 33.491 ms"

How can I get the view to use the same query plan as the select statement?=


------=_Part_210059_1431240683.1400474834335--