Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6B5D7632EA4 for ; Thu, 4 Feb 2010 17:57:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 84016-02 for ; Thu, 4 Feb 2010 21:57:19 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f220.google.com (mail-fx0-f220.google.com [209.85.220.220]) by mail.postgresql.org (Postfix) with ESMTP id 91574632C30 for ; Thu, 4 Feb 2010 17:57:23 -0400 (AST) Received: by fxm20 with SMTP id 20so3183843fxm.21 for ; Thu, 04 Feb 2010 13:57:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=y1lDhMabzaePOFiWMV8hhBh8X26ySHpsxOif4Css3A0=; b=vBsO59EGNBFPWfvIR5glXQLled1bf7gwLm1I5xE57LnFELeiXyn/uZ1NsP+//8Qjgg VMK3tDwP4ut+5srqkHsP2mCs8NZq4qR9zZpU0/oCcyy9DvzTvMEyi5co7jOcZJpS+ulw jw9lkHHkc/LBjXh0ijYqk2/R+cYGz59xhh9q0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=m3G3D7NSzLSpf7NUqgk6j+S/7qC3EOyCwT0iTP497U9+5c6jRRc+y1bIEaMlkqYCkV kyqiKvT934p1aOqyXa+1cUaB3vQohHKgQNK9SJMslGNjsZYdP1BXkGje078zBC9vPfb4 CX/eQ0XG8wCpeF7rnlTgZFW7mzNi3hR3kMNoY= MIME-Version: 1.0 Received: by 10.239.150.208 with SMTP id o16mr187614hbb.39.1265320641774; Thu, 04 Feb 2010 13:57:21 -0800 (PST) In-Reply-To: References: <603c8f071002031905w51dbea37y148284ea6546feda@mail.gmail.com> Date: Thu, 4 Feb 2010 16:57:21 -0500 Message-ID: <603c8f071002041357k72da49a7yc5ecb7a78ef6c52f@mail.gmail.com> Subject: Re: Slow query: table iteration (8.3) From: Robert Haas To: Glenn Maynard Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.475 tagged_above=-10 required=5 tests=AWL=0.124, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/94 X-Sequence-Number: 37515 On Thu, Feb 4, 2010 at 3:24 AM, Glenn Maynard wrote: > On Wed, Feb 3, 2010 at 10:05 PM, Robert Haas wrot= e: >> Rewriting it as a join will likely be faster still: >> >> SELECT r.id FROM stomp_steps s, stomp_round r WHERE (s.id IS NULL OR >> r.steps_id =3D s.id) AND ($1 IS NULL OR r.user_card_id =3D $1) ORDER BY >> r.score DESC LIMIT $2 > > That's not the same; this SELECT will only find the N highest scores, > since the LIMIT applies to the whole results. =A0Mine finds the highest > scores for each stage (steps), since the scope of the LIMIT is each > call of the function (eg. "find the top score for each stage" as > opposed to "find the top five scores for each stage"). > > That's the only reason I used a function at all to begin with--I know > no way to do this with a plain SELECT. Oh, I get it. Yeah, I don't think you can do that without LATERAL(), which we don't have, unless the window-function thing works... ...Robert