X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id EABDF534DA for ; Tue, 29 Mar 2005 11:59:07 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 95256-09 for ; Tue, 29 Mar 2005 10:59:03 +0000 (GMT) Received: from mx5.net.nih.gov (mx5.net.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id 0088A5349D for ; Tue, 29 Mar 2005 11:59:02 +0100 (BST) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j2TAx3au030088 for ; Tue, 29 Mar 2005 05:59:04 -0500 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j2TAx3Kd030078; Tue, 29 Mar 2005 05:59:03 -0500 In-Reply-To: <424928D8.7060705@numerixtechnology.de> References: <42487A6D.6020506@numerixtechnology.de> <1112047463.22988.25.camel@state.g2switchworks.com> <424928D8.7060705@numerixtechnology.de> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <140ac5a92c5f5fc2e38bd10a17e7d70f@mail.nih.gov> Content-Transfer-Encoding: 7bit Cc: Scott Marlowe , pgsql-sql@postgresql.org From: Sean Davis Subject: Re: cost of CREATE VIEW ... AS SELECT DISTINCT Date: Tue, 29 Mar 2005 05:59:01 -0500 To: mailreg@numerixtechnology.de X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/373 X-Sequence-Number: 20852 On Mar 29, 2005, at 5:07 AM, T E Schmitz wrote: > Hello Scott, > > Scott Marlowe wrote: >> On Mon, 2005-03-28 at 15:43, T E Schmitz wrote: >>> How expensive would it be to maintain the following VIEW: >>> >>> CREATE VIEW origin AS SELECT DISTINCT origin FROM transaktion >>> >>> if there is in index on transaktion.origin; the table transaktion >>> has thousands of records and there are only a few distinct origin? >> The cost will only be encurred when running the view. if you want >> materialized views (which WOULD have maintenance costs) you'll have to > > Thank you for the pointer - that might come in handy for another > scenario. > >> The cost of executing that view should be the same as the cost of >> running the query by hand. > > I did an EXPLAIN ANALYZE and a sequential scan was carried out despite > the index I had on the column. Maybe this is because I only have very > few records in my test DB. > > Would the "SELECT DISTINCT origin" always cause a sequential table > scan regardless whether there is an index on the origin column or not? > I think you are right. If this is expensive and run often, you could always normalize further and create a table of "unique_origin" that would have only unique origins and set transaktion to have a foreign key referring to the unique_origin table and then just query the unique_origin table when you need to do the query above. Sean