X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 4DB3FD1B521 for ; Tue, 11 Nov 2003 23:44:36 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 55539-10 for ; Tue, 11 Nov 2003 19:44:07 -0400 (AST) Received: from davinci.ethosmedia.com (server228.ethosmedia.com [209.128.84.228]) by svr1.postgresql.org (Postfix) with ESMTP id 0FE20D1B54F for ; Tue, 11 Nov 2003 19:44:04 -0400 (AST) Received: from [66.219.92.2] (HELO temoku) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.0.2) with ESMTP id 3902881; Tue, 11 Nov 2003 15:44:51 -0800 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: Troels Arvin , pgsql-sql@postgresql.org Subject: Re: Quota query with decent performance? Date: Tue, 11 Nov 2003 15:41:34 -0800 User-Agent: KMail/1.4.3 References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200311111541.34660.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/131 X-Sequence-Number: 15946 Troels, Thank you for contacting us before publishing your results. Please ignore= =20 any list-trolls who criticize your methodology; there are a few cranks on= =20 every list. The important thing is your contacted us. > In MSSQL and DB2 there are very efficient facilities for such queries, but > I can't find any well-performing declarative methods for PostgreSQL. I > have tried a couple of different strategies, and I currently get the best > results from a correlated subquery like >=20 > SELECT * FROM person AS px > WHERE ( > SELECT COUNT(*) > FROM person AS py > WHERE py.age < px.age > ) < 3; >=20 > When my base table has 4000 rows, my query takes 27 seconds in PostgreSQL > 7.2.3 (PIII 1000MHz) which is clearly unacceptable,=20 Well, first off 7.2.3 is a two-year old version. There have been performa= nce=20 improvements specificially on these sorts of issues since then. Please use= =20 at least 7.3.4, and we would prefer that you use 7.4RC2, which is available= =20 now. 7.4, in particular, introduces significant improvements in "group by"= =20 queries. If for some reason you have to compare against this old version,= =20 please be fair to us and note somewhere that you were using an old version = of=20 PostgreSQL. Second, the query you post is one "SQL Standard" way, which is good for=20 portability but not for speed. Frankly, I'm not convinced that it's even t= he=20 best SQL standard way. On the other databases, you seem happy to use=20 non-SQL-standard syntax, so let me give you one such solution in PostgreSQL: SELECT * FROM person WHERE person.age >=3D (SELECT p2.age from person p2 order by p2.age DESC LIMIT 1 OFFSET 2) also try: SELECT *=20 FROM person,=20 (SELECT p2.age from person p2 order by p2.age DESC LIMIT 1 OFFSET 2) as prank WHERE person.age >=3D prank.age This should give you all of the rows whose ages are in the top 3 ranks much= =20 faster. --=20 -Josh Berkus Aglio Database Solutions San Francisco