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 56961DAABE for ; Fri, 28 Oct 2005 21:04:30 -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 97599-09 for ; Sat, 29 Oct 2005 00:04:27 +0000 (GMT) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.197]) by svr1.postgresql.org (Postfix) with ESMTP id 184C2DA3EA for ; Fri, 28 Oct 2005 21:04:28 -0300 (ADT) Received: by xproxy.gmail.com with SMTP id s14so383810wxc for ; Fri, 28 Oct 2005 17:04:33 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=PQZ29jsfgywZ4X/WX5jTImfChkcatvU+oyjTBXfeetMG0rulFtMRkfLUiVV8wP9SlA7oOgDDlLfEwJhrwJKJNZTAJlPIr73seKaC6qOZSp031QUg8qOMD+x91tjXu7cKkFd1gnly1asEsZXLAMfM5wn3RgnJIP8Wttv5WdLuY9s= Received: by 10.65.236.1 with SMTP id n1mr500742qbr; Fri, 28 Oct 2005 17:04:32 -0700 (PDT) Received: by 10.65.157.7 with HTTP; Fri, 28 Oct 2005 17:04:32 -0700 (PDT) Message-ID: Date: Fri, 28 Oct 2005 17:04:32 -0700 From: Collin Peters Subject: Re: Simple query: how to optimize Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.475 required=5 tests=[AWL=0.286, MISSING_HEADERS=0.189] X-Spam-Score: 0.475 X-Spam-Level: X-Archive-Number: 200510/447 X-Sequence-Number: 15215 A little bit more on my last post that I forget to mention. The two queries run at the same speed and have the same plan only if I have an index on the user_activity.user_id column. Otherwise they run at different speeds. The query you gave me actually runs slower without the index. All this is making my head spin!! :O On 10/28/05, Collin Peters wrote: > These two queries execute at exactly the same speed. When I run run > EXPLAIN on them both they return the *exact* same query plan as well. > I find this strange... but it is also kind of what I expected from > reading up on various things. I am under the impression the > postgresql will break up your query and run it as it sees best. So > in the case of these two queries... it seems it is actually almost > converting one into the other. Maybe I am wrong. > > Is there a good resource list somewhere for postgresql query > optimization? There are entire books devoted to the subject for > oracle but I can't find more than a few small articles on postgresql > query optimizations on the web. > > Regards, > Collin > > On 10/28/05, Roger Hand wrote: > > > SELECT u.user_id, MAX(ua.activity_date) > > > FROM pp_users u > > > LEFT OUTER JOIN user_activity ua ON (u.user_id =3D ua.user_id AND > > > ua.user_activity_type_id =3D 7) > > > WHERE u.userstatus_id <> 4 > > > AND age(u.joined_date) < interval '30 days' > > > GROUP BY u.user_id > > > > You're first joining against the entire user table, then filtering out = the users > > you don't need. > > > > Instead, filter out the users you don't need first, then do the join: > > > > SELECT users.user_id, MAX(ua.activity_date) > > FROM > > (SELECT u.user_id > > FROM pp_users u > > WHERE u.userstatus_id <> 4 > > AND age(u.joined_date) < interval '30 days' > > ) users > > LEFT OUTER JOIN user_activity ua > > ON (users.user_id =3D ua.user_id > > AND ua.user_activity_type_id =3D 7) > > GROUP BY users.user_id > > > > (disclaimer: I haven't actually tried this sql) > > >