Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B85129FB1CC for ; Tue, 9 Jan 2007 08:41:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 85525-02 for ; Tue, 9 Jan 2007 08:41:01 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mxfep02.bredband.com (mxfep02.bredband.com [195.54.107.73]) by postgresql.org (Postfix) with ESMTP id C6B949FA4B6 for ; Tue, 9 Jan 2007 08:41:05 -0400 (AST) Received: from ironport.bredband.com ([195.54.107.82] [195.54.107.82]) by mxfep03.bredband.com with ESMTP id <20070109123544.FSLF2708.mxfep03.bredband.com@ironport.bredband.com> for ; Tue, 9 Jan 2007 13:35:44 +0100 Received: from ua-83-227-204-174.cust.bredbandsbolaget.se (HELO zigo.dhs.org) ([83.227.204.174]) by ironport.bredband.com with ESMTP; 09 Jan 2007 13:35:40 +0100 Received: from zigo.dhs.org (zigo [127.0.0.1]) by zigo.dhs.org (Postfix) with ESMTP id 967CB8640; Tue, 9 Jan 2007 13:35:36 +0100 (CET) Received: from 192.121.104.48 (SquirrelMail authenticated user db) by zigo.dhs.org with HTTP; Tue, 9 Jan 2007 13:35:36 +0100 (CET) Message-ID: <37813.192.121.104.48.1168346136.squirrel@zigo.dhs.org> In-Reply-To: <71E37EF6B7DCC1499CEA0316A256832802B3EB8A@loki.wc.globexplorer.net> References: <71E37EF6B7DCC1499CEA0316A256832802B3EB8A@loki.wc.globexplorer.net> Date: Tue, 9 Jan 2007 13:35:36 +0100 (CET) Subject: Re: Horribly slow query/ sequential scan From: db@zigo.dhs.org To: "Gregory S. Williamson" Cc: pgsql-performance@postgresql.org User-Agent: SquirrelMail/1.4.8 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/104 X-Sequence-Number: 22705 I don't think I understand the idea behind this query. Do you really need billing_reports twice? > The query: > explain analyze select > w.appid,w.rate,w.is_subscribed,sum(w.hits) AS Hits ,sum(w.sius) AS IUs, > sum(w.total_amnt) AS Total,sum(w.hits) * w.rate AS ByHits, > sum(w.sius) * w.rate AS BYIUS > from bill_rpt_work w, billing_reports b > where w.report_id in > (select b.report_id from billing_reports where b.report_s_date = > '2006-09-30') > and (w.client_id = '227400001' or w.client_id = '2274000010') > group by 1,2,3 > order by 1,2,3; Maybe this is the query you want instead? select w.appid, w.rate, w.is_subscribed, sum(w.hits) AS Hits, sum(w.sius) AS IUs, sum(w.total_amnt) AS Total, sum(w.hits) * w.rate AS ByHits, sum(w.sius) * w.rate AS BYIUS from bill_rpt_work w where w.report_id in (select b.report_id from billing_reports b where b.report_s_date = '2006-09-30') and (w.client_id = '227400001' or w.client_id = '2274000010') group by 1,2,3 order by 1,2,3; /Dennis