Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 7A0E92E003B for ; Fri, 23 Nov 2007 09:29:49 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 54183-06 for ; Fri, 23 Nov 2007 09:29:37 -0400 (AST) Received: from bay0-omc2-s36.bay0.hotmail.com (bay0-omc2-s36.bay0.hotmail.com [65.54.246.172]) by postgresql.org (Postfix) with ESMTP id 6B75E2E0033 for ; Fri, 23 Nov 2007 09:29:42 -0400 (AST) Received: from BAY113-W15 ([65.54.168.115]) by bay0-omc2-s36.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.3959); Fri, 23 Nov 2007 05:29:41 -0800 Message-ID: X-Originating-IP: [131.111.10.176] From: Dean Rasheed To: Subject: Performance problem with UNION ALL view and domains Date: Fri, 23 Nov 2007 13:29:40 +0000 Importance: Normal Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 23 Nov 2007 13:29:41.0218 (UTC) FILETIME=[E7041420:01C82DD4] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/279 X-Sequence-Number: 27694 Hi, I am having a performance problem trying to query a view which is a UNION ALL of 2 tables. I have narrowed the problem down to my use of DOMAINS in the underlying table. So in the test-case below, when the column "a" is of domain type foo_text, the query runs slowly using the following plan: Subquery Scan foo_v (cost=3D0.00..798.00 rows=3D100 width=3D64) (actual t= ime=3D0.049..24.763 rows=3D2 loops=3D1) Filter: (a =3D (('foo34'::text)::foo_text)::text) -> Append (cost=3D0.00..548.00 rows=3D20000 width=3D20) (actual time= =3D0.007..20.338 rows=3D20000 loops=3D1) -> Subquery Scan "*SELECT* 1" (cost=3D0.00..274.00 rows=3D10000 = width=3D20) (actual time=3D0.006..7.341 rows=3D10000 loops=3D1) -> Seq Scan on foo (cost=3D0.00..174.00 rows=3D10000 width= =3D20) (actual time=3D0.004..2.366 rows=3D10000 loops=3D1) -> Subquery Scan "*SELECT* 2" (cost=3D0.00..274.00 rows=3D10000 = width=3D10) (actual time=3D0.009..6.536 rows=3D10000 loops=3D1) -> Seq Scan on foo (cost=3D0.00..174.00 rows=3D10000 width= =3D10) (actual time=3D0.007..2.746 rows=3D10000 loops=3D1) Total runtime: 24.811 ms However, when the column type is text, the query runs fast as I would expect, using the PK index: Result (cost=3D0.00..16.55 rows=3D2 width=3D64) (actual time=3D0.015..0.0= 25 rows=3D2 loops=3D1) -> Append (cost=3D0.00..16.55 rows=3D2 width=3D64) (actual time=3D0.01= 4..0.023 rows=3D2 loops=3D1) -> Index Scan using foo_pkey on foo (cost=3D0.00..8.27 rows=3D1 = width=3D20) (actual time=3D0.014..0.014 rows=3D1 loops=3D1) Index Cond: (a =3D (('foo34'::text)::foo_text)::text) -> Index Scan using foo_pkey on foo (cost=3D0.00..8.27 rows=3D1 = width=3D10) (actual time=3D0.007..0.008 rows=3D1 loops=3D1) Index Cond: (a =3D (('foo34'::text)::foo_text)::text) Total runtime: 0.065 ms (PostgreSQL 8.2.5) Any ideas? Thanks, Dean CREATE OR REPLACE FUNCTION setup() RETURNS void AS $$ DECLARE val int; BEGIN DROP TABLE IF EXISTS foo CASCADE; DROP DOMAIN IF EXISTS foo_text; CREATE DOMAIN foo_text text;-- CONSTRAINT tt_check CHECK (VALUE LIKE 'foo= %'); CREATE TABLE foo ( a foo_text PRIMARY KEY, b text ); val :=3D 0; WHILE val < 10000 LOOP INSERT INTO foo VALUES('foo'||val, 'bar'||val); val :=3D val+1; END LOOP; CREATE VIEW foo_v AS (SELECT a,b from foo) UNION ALL (SELECT a,NULL::text AS b FROM foo); END; $$ LANGUAGE plpgsql; SELECT setup(); ANALYZE foo; EXPLAIN ANALYZE SELECT * FROM foo_v WHERE a=3D'foo34'::foo_text; _________________________________________________________________ Feel like a local wherever you go. http://www.backofmyhand.com=