X-Original-To: pgsql-hackers@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id D7A79476226 for ; Sat, 1 Feb 2003 15:29:46 -0500 (EST) Received: from sabre.velocet.net (sabre.velocet.net [216.138.209.205]) by postgresql.org (Postfix) with ESMTP id CFA13475E3E for ; Sat, 1 Feb 2003 15:21:33 -0500 (EST) Received: from stark.dyndns.tv (H162.C233.tor.velocet.net [216.138.233.162]) by sabre.velocet.net (Postfix) with ESMTP id 325DE138152; Sat, 1 Feb 2003 15:21:28 -0500 (EST) Received: from localhost ([127.0.0.1] helo=stark.dyndns.tv ident=foobar) by stark.dyndns.tv with smtp (Exim 3.36 #1 (Debian)) id 18f48k-0003Nc-00; Sat, 01 Feb 2003 15:21:26 -0500 To: Tom Lane Cc: Kevin Brown , "'pgsql-hackers@postgresql.org'" Subject: Re: [PERFORM] not using index for select min(...) References: <200301311531.12605.josh@agliodbs.com> <20030201040954.GK15936@perrin.int.nxad.com> <4278.1044074132@sss.pgh.pa.us> <20030201164336.GP12957@filer> <7647.1044119012@sss.pgh.pa.us> In-Reply-To: <7647.1044119012@sss.pgh.pa.us> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 01 Feb 2003 15:21:24 -0500 Message-ID: <87el6rsqez.fsf@stark.dyndns.tv> Lines: 36 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/42 X-Sequence-Number: 34984 Tom Lane writes: > Kevin Brown writes: > > Hmm...any chance, then, of giving aggregate functions a means of > > asking which table(s) and column(s) the original query referred to so > > that it could do proper optimization on its own? > > You can't usefully do that without altering the aggregate paradigm. > It won't help for min() to intuit the answer quickly if the query > plan is going to insist on feeding every row to it anyway. That just means you need some way for aggregates to declare which records they need. The only values that seem like they would be useful would be "first record" "last record" and "all records". Possibly something like "all-nonnull records" for things like count(), but that might be harder. > Don't forget that it would also need to be aware of whether there were > any WHERE clauses, joins, GROUP BY, perhaps other things I'm not > thinking of. > > In the end, the only reasonable way to handle this kind of thing is > to teach the query planner about it. Considering the small number > of cases that are usefully optimizable (basically only MIN and MAX > on a single table without any WHERE or GROUP clauses), and the ready > availability of a SQL-level workaround, it strikes me as a very > low-priority TODO item. All true, but I wouldn't be so quick to dismiss it as low-priority. In my experience I've seen the idiom "select min(foo) from bar" more times than I can count. The frequency with which this question occurs here probably is indicative of how much people expect it to work. And it's probably used by a lot of multi-database applications and in a lot of auto-matically generated code where it would be hard to hack in special purpose workarounds. -- greg