X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 711BB9FB1C8 for ; Wed, 3 May 2006 00:04:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 27444-06 for ; Wed, 3 May 2006 00:04:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id C80729FA2C7 for ; Wed, 3 May 2006 00:03:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k4333vGL009689; Tue, 2 May 2006 23:03:58 -0400 (EDT) To: "Ian Burrell" cc: pgsql-performance@postgresql.org Subject: Re: Nested loop join and date range query In-reply-to: References: Comments: In-reply-to "Ian Burrell" message dated "Tue, 02 May 2006 15:55:57 -0700" Date: Tue, 02 May 2006 23:03:57 -0400 Message-ID: <9688.1146625437@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/70 X-Sequence-Number: 18857 "Ian Burrell" writes: > We recently upgraded to PostgreSQL 8.1 from 7.4 and a few queries are > having performance problems and running for very long times. The > commonality seems to be PostgreSQL 8.1 is choosing to use a nested > loop join because it estimates there will be only be a single row. > -> Index Scan using unq_xtns_by_mso_title_wk on > xtns_by_mso_title_wk x (cost=0.00..4.12 rows=1 width=26) (actual > time=7.827..1297.681 rows=3934 loops=1) > Index Cond: ((week >= '2006-04-23 > 00:00:00'::timestamp without time zone) AND (week <= '2006-04-23 > 00:00:00'::timestamp without time zone) AND (mso_no = 50)) We've already noted that there's a problem with estimating zero-width ranges (too lazy to search the archives, but this has come up at least twice recently). Can you modify your app to generate something like week >= x and week < x+1 instead of week >= x and week <= x ? My recollection is that the fix will probably be complicated enough to not get back-patched into 8.1. BTW, AFAIK the same problem exists in 7.4. What kind of estimates/plans were you getting for this case in 7.4? regards, tom lane