X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AAA8853531 for ; Tue, 26 Apr 2005 19:33:00 -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 36197-10 for ; Tue, 26 Apr 2005 22:32:56 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.196]) by svr1.postgresql.org (Postfix) with ESMTP id 0789B5355E for ; Tue, 26 Apr 2005 19:32:54 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id 68so106050wri for ; Tue, 26 Apr 2005 15:32:54 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=COx/EsxSqfuegzNrID7ekqipS0zrUIiYXYhT4tHhjE/r+nd1opcgO1FlpsXk0m13xRnO2fT9/7wxAibpkVGxG8j20WE6aH32z4FUC/j5OvMc1Y6bV8cJztgKj9AI5m5NlnkWda3fa6UY3ILV+KGsU+lVIGZPvOpqjdvEyrnG/ww= Received: by 10.54.79.8 with SMTP id c8mr177778wrb; Tue, 26 Apr 2005 15:32:54 -0700 (PDT) Received: by 10.54.22.6 with HTTP; Tue, 26 Apr 2005 15:32:54 -0700 (PDT) Message-ID: Date: Tue, 26 Apr 2005 17:32:54 -0500 From: Matthew Nuzum Reply-To: newz@bearfruit.org To: pgsql-performance@postgresql.org Subject: Re: speed up query with max() and odd estimates In-Reply-To: <20050426204853.GA29424@uio.no> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <20050426204853.GA29424@uio.no> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.318 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200504/711 X-Sequence-Number: 12108 On 4/26/05, Steinar H. Gunderson wrote: > On Tue, Apr 26, 2005 at 03:16:57PM -0500, Matthew Nuzum wrote: > > Seq Scan on usage_access (cost=3D0.00..1183396.40 rows=3D12713851 > > width=3D116) (actual time=3D481796.22..481839.43 rows=3D3343 loops=3D1) >=20 > That's a gross misestimation -- four orders of magnitude off! >=20 > Have you considering doing this in two steps, first getting out whatever > comes from the subquery and then doing the query?=20 Well, I don't know if the estimates are correct now or not, but I found that your suggestion of doing it in two steps helped a lot. For the archives, here's what made a drastic improvement: This batch program had an overhead of 25 min to build hash tables using the sql queries. It is now down to about 47 seconds. The biggest improvements (bringing it down to 9 min) were to get rid of all instances of `select max(field) from ...` and replacing them with `select field from ... order by field desc limit 1` Then, to get it down to the final 47 seconds I changed this query: SELECT client,max(atime) as atime from usage_access where atime >=3D (select atime - '1 hour'::interval from usage_access order by atime desc limit 1) group by client; To these three queries: SELECT atime - '1 hour'::interval from usage_access order by atime desc lim= it 1; SELECT client, atime into temporary table recent_sessions from usage_access where atime >=3D '%s'; SELECT client, max(atime) as atime from recent_sessions group by client; Thanks for the help. --=20 Matthew Nuzum www.bearfruit.org