X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7CFAA52806 for ; Tue, 21 Jun 2005 12:00:31 -0300 (ADT) 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 23035-09 for ; Tue, 21 Jun 2005 15:00:13 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id 4D6B5528BB for ; Tue, 21 Jun 2005 12:00:12 -0300 (ADT) Received: (qmail 10342 invoked from network); 21 Jun 2005 17:00:26 +0200 Received: from unknown (HELO localhost) (boutiquenumerique-lists@192.168.0.4) by boutiquenumerique.com with SMTP; 21 Jun 2005 17:00:26 +0200 Date: Tue, 21 Jun 2005 17:00:10 +0200 To: "Amit V Shah" , "'pgsql-performance@postgresql.org'" Subject: Re: Do Views execute underlying query everytime ?? References: <0C072E7CC947D511AC9600A0CC7341200256CF51@vpn.tagaudit.com> From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID: In-Reply-To: <0C072E7CC947D511AC9600A0CC7341200256CF51@vpn.tagaudit.com> User-Agent: Opera M2(BETA2)/8.0 (Linux, build 987) 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: 200506/384 X-Sequence-Number: 13021 > However, I donot know if the query that creates the view is executed > everytime I select something from the view. Because if that is the case, > then I think my queries will again be slow. But if that is the way views > work, then what would be the point in creating them .. Views are more for when you have a query which keeps coming a zillion time in your application like : SELECT p.*, pd.* FROM products p, products_names pd WHERE p.id=pd.id AND pd.language=... You create a view like : CREATE VIEW products_with_name AS SELECT p.*, pd.* FROM products p, products_names pd WHERE p.id=pd.id And then you : SELECT * FROM products_with_name WHERE id=... AND language=... It saves a lot of headache and typing over and over again the same thing, and you can tell your ORM library to use them, too. But for your application, they're useless, You should create a "materialized view"... which is just a table and update it from a CRON job. You can still use a view to fill your table, and as a way to hold your query, so the cron job doesn't have to issue real queries, just filling tables from views : CREATE VIEW cached_stuff_view AS ... And once in while : BEGIN; DROP TABLE cached_stuff; CREATE TABLE cached_stuff AS SELECT * FROM cached_stuff_view; CREATE INDEX ... ON cached_stuff( ... ) COMMIT; ANALYZE cached_stuff; Or : BEGIN; TRUNCATE cached_stuff; INSERT INTO cached_stuff SELECT * FROM cached_stuff_view; COMMIT; ANALYZE cached_stuff; If you update your entire table it's faster to just junk it or truncate it then recreate it, but maybe you'd prefer TRUNCATE which saves you from having to re-create of indexes... but it'll be faster if you drop the indexes and re-create them afterwards anyway instead of them being updated for each row inserted. So I'd say DROP TABLE.