Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 0BE98632F71 for ; Thu, 15 Apr 2010 14:27:50 -0300 (ADT) 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 15680-03 for ; Thu, 15 Apr 2010 17:27:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gw.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 264C46327B3 for ; Thu, 15 Apr 2010 14:27:39 -0300 (ADT) Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Thu, 15 Apr 2010 12:27:38 -0500 Message-Id: <4BC70633020000250003095B@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Thu, 15 Apr 2010 12:27:31 -0500 From: "Kevin Grittner" To: , "Lewis Kapell" Subject: Re: Query is stuck References: <3A8C969225424C4D8E6BEE65ED8552DA0119F188@XMB-BGL-41C.cisco.com> <4BC724A7.5070301@setonhome.org> In-Reply-To: <4BC724A7.5070301@setonhome.org> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.074 tagged_above=-10 required=5 tests=AWL=-0.164, BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201004/196 X-Sequence-Number: 34290 Lewis Kapell wrote: > The fact that it is doing a sequential scan ("Seq Scan") tells us > why it takes so long. Well, that and the fact that for each row in one scan of the table, it scans the entire table again. :-( >> select report_id, dm_ip, dm_mac, dm_user, dm_os, report_time, >> sys_name, sys_user, sys_user_domain, ss_key, login_time, role_id, >> new_vlan_id from repcopy as a where report_time = (select >> max(report_time) from repcopy as b where a.dm_ip = b.dm_ip and >> a.dm_user=b.dm_user and b.ss_key != '') and report_status = 0 and >> dm_user = 'u1'; I *think* that's equivalent to the following, which might be faster: select report_id, a.dm_ip, dm_mac, dm_user, dm_os, a.report_time, sys_name, sys_user, sys_user_domain, ss_key, login_time, role_id, new_vlan_id from repcopy a join ( select dm_ip, max(report_time) as report_time from repcopy where b.ss_key != '' and b.dm_user = 'u1' group by dm_ip ) b on (b.dm_ip = a.dm_ip and b.report_time = a.report_time) where a.report_status = 0 and a.dm_user = 'u1' ; -Kevin