Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id DAFD563292A for ; Tue, 5 Jan 2010 21:34:14 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39825-03 for ; Wed, 6 Jan 2010 01:34:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f227.google.com (mail-fx0-f227.google.com [209.85.220.227]) by mail.postgresql.org (Postfix) with ESMTP id 1B0BA632D09 for ; Tue, 5 Jan 2010 21:34:03 -0400 (AST) Received: by fxm27 with SMTP id 27so2350930fxm.3 for ; Tue, 05 Jan 2010 17:34:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=muz+0QQ+P9+w9ETESjIAWZThTiLAzg2uDqihWUgMQY0=; b=OIzeW7hyPfL28lSy94ZC9DHbfEQpfXge+C9XFuKBeapz0cyuOpTLytwVp8M6v4EDqV XtgrPi6z/5JyVmN/130Df4+GJBj4BNhTzrrlt7bN6Qpnk7XBJ+o+kpIAW3K130hJZGh3 68shk24evk42oyPp1Bf1xZkEKsAkZbB1d+6h8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=gRRkcKKjwK0Rr5I9RfH95vgGCycWBYi5X4y38cr8+Pc7PSm1GTCONL/PCoeMKgZNfr fPsnjY1GtEkwWRYXQRW9zVymIc0WhRyfJnbV9wFq/FsiZLicmQiK+hpkQZKG1B8TFjC4 uotXiskXt4SSxDwJGW1ndUEINK6Impnq0VcOM= MIME-Version: 1.0 Received: by 10.239.189.12 with SMTP id r12mr369543hbh.94.1262741641721; Tue, 05 Jan 2010 17:34:01 -0800 (PST) In-Reply-To: <4B43B014.1090003@ca.com> References: <4B43B014.1090003@ca.com> Date: Tue, 5 Jan 2010 20:34:01 -0500 Message-ID: <603c8f071001051734u2d6e2686k97e7f8a92ae8c38f@mail.gmail.com> Subject: Re: query looping? From: Robert Haas To: Brian Cox Cc: "pgsql-performance@postgresql.org" Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.003 tagged_above=-10 required=5 tests=BAYES_40=-0.185, FH_DATE_PAST_20XX=3.188 X-Spam-Level: *** X-Archive-Number: 201001/51 X-Sequence-Number: 36931 On Tue, Jan 5, 2010 at 4:33 PM, Brian Cox wrote: > comparing this to the 1st explain foo output shows some minor differences in > row estimates -- but nothing, I assume, that could explain the huge time > difference. Of course, the 1st plan may not (and probably? wasn't) the plan > that was used to take 124M ms. > > Any thoughts on how to avoid this? The incorrect row estimates can only foul up the plan; they can't directly make anything slow. Comparing the two plans line by line, the only difference I see is the fast plan has: -> Seq Scan on ts_stats_transetgroup_user_weekly b (cost=0.00..23787.37 rows=89590 width=24) (actual time=0.040..295.414 rows=89758 loops=1) Filter: ((ts_interval_start_time >= '2009-12-28 00:00:00-08'::timestamp with time zone) AND (ts_interval_start_time < '2010-01-04 00:00:00-08'::timestamp with time zone)) ...while the slow one has: Index Scan using ts_stats_transetgroup_user_weekly_starttimeindex on ts_stats_transetgroup_user_weekly b (cost=0.00..1301.21 rows=898 width=24) Index Cond: ((ts_interval_start_time >= $3) AND (ts_interval_start_time < $4)) So it looks like using that index to fetch the data is a LOT slower than just scanning the whole table. In terms of fixing this problem, I have two ideas: - If you don't have any queries where this index makes things faster, then you can just drop the index. - If you have other queries where this index helps (even though it is hurting this one), then you're going to have to find a way to execute the query without using bound parameters - i.e. with the actual values in there instead of $1 through $4. That will allow the planner to see that the index scan is a loser because it will see that there are a lot of rows in the specified range of ts_interval_start_times. ...Robert