Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 702A0633316 for ; Tue, 22 Dec 2009 10:13:07 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 12598-02 for ; Tue, 22 Dec 2009 14:12:45 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.10]) by mail.postgresql.org (Postfix) with ESMTP id 38F0F6335BE for ; Tue, 22 Dec 2009 10:12:37 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0Mh6iv-1NaTSo459l-00MqiF; Tue, 22 Dec 2009 15:12:32 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1NN5TT-0003O0-00 for pgsql-sql@postgresql.org; Tue, 22 Dec 2009 15:12:31 +0100 Date: Tue, 22 Dec 2009 15:12:31 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: short-cutting if sum()>constant Message-ID: <20091222141231.GE10308@a-kretschmer.de> References: <20091222133527.3007bd96@dawn.webthatworks.it> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20091222133527.3007bd96@dawn.webthatworks.it> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1+FMWHQKppXEO8/0WJuElcsLM95pv2Llmo/5/l WfTjsRzPWu3rqBzjPUVcJZNXOuF7Etee32PiR403jltngvunXB 8QLHTk4+k1Uo0vLgRBA1w== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.246 tagged_above=-10 required=5 tests=AWL=0.353, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/59 X-Sequence-Number: 33497 In response to Ivan Sergio Borgonovo : > Hi, > > I'd like to know if > > select sum(qty) from t where status=37; > > is > constant. > > qty is always >0. > > Is there a way to skip examining further rows and return a result > ASAP? I think no. But you can create a new table with 2 columns: status (primary key) and a column for the sum of qty. And you need a TRIGGER: update this table for every insert, update and delete on your table t. Now you can ask _this_ new table if the sum(qty) > your constant, this should work very fast. Just an idea... Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99