X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 01B1BDA4A8 for ; Thu, 22 Sep 2005 05:41:09 -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 90401-02 for ; Thu, 22 Sep 2005 08:40:48 +0000 (GMT) Received: from smtp1.freeserve.com (smtp1.wanadoo.co.uk [193.252.22.158]) by svr1.postgresql.org (Postfix) with ESMTP id 831D8DA512 for ; Thu, 22 Sep 2005 05:40:48 -0300 (ADT) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf3007.me.freeserve.com (SMTP Server) with ESMTP id 444D21C0008B; Thu, 22 Sep 2005 10:40:50 +0200 (CEST) Received: from 192.168.0.102 (modem-2050.leopard.dialup.pol.co.uk [217.135.152.2]) by mwinf3007.me.freeserve.com (SMTP Server) with ESMTP id C328A1C00088; Thu, 22 Sep 2005 10:40:49 +0200 (CEST) X-ME-UUID: 20050922084049799.C328A1C00088@mwinf3007.me.freeserve.com Subject: Re: SELECT LIMIT 1 VIEW Performance Issue From: Simon Riggs To: K C Lau Cc: pgsql-performance@postgresql.org In-Reply-To: <6.2.1.2.0.20050922102035.07bcf2e0@localhost> References: <6.2.1.2.0.20050615100524.04f9d6a8@localhost> <6.2.1.2.0.20050922102035.07bcf2e0@localhost> Content-Type: text/plain Organization: 2nd Quadrant Date: Thu, 22 Sep 2005 09:40:56 +0100 Message-Id: <1127378456.4145.76.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-3) Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.052 required=5 tests=[AWL=0.002, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200509/235 X-Sequence-Number: 14543 On Thu, 2005-09-22 at 12:21 +0800, K C Lau wrote: > Investigating further on this problem I brought up in June, the following > query with pg 8.0.3 on Windows scans all 1743 data records for a player: > > esdt=> explain analyze select PlayerID,AtDate from Player a > where PlayerID='22220' and AtDate = (select b.AtDate from Player b > where b.PlayerID = a.PlayerID order by b.PlayerID desc, b.AtDate desc > LIMIT 1); > > Total runtime: 51.133 ms > > Using a static value in the subquery produces the desired result below, but > since we use views for our queries (see last part of this email), we cannot > push the static value into the subquery: > > esdt=> explain analyze select PlayerID,AtDate from Player a > where PlayerID='22220' and AtDate = (select b.AtDate from Player b > where b.PlayerID = '22220' order by b.PlayerID desc, b.AtDate desc LIMIT 1); > Total runtime: 0.149 ms > > The Player table has a primary key on PlayerID, AtDate. Is there a way to > stop the inner-most index scan looping all 1743 data records for that > player? Is that a bug or known issue? Currently the planner can't tell whether a subquery is correlated or not until it has planned the query. So it is unable to push down the qualification automatically in the way you have achieved manually. The new min() optimisation doesn't yet work with GROUP BY which is what you would use to reformulate the query that way, so no luck that way either. If you don't want to do this in a view, calculate the values for all players at once and store the values in a summary table for when you need them. Best Regards, Simon Riggs