Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 8AA839FB531 for ; Fri, 12 Jan 2007 04:57:22 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 75058-01-6 for ; Fri, 12 Jan 2007 04:57:13 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.174]) by postgresql.org (Postfix) with ESMTP id A27A39FB3CA for ; Fri, 12 Jan 2007 04:57:01 -0400 (AST) Received: by ug-out-1314.google.com with SMTP id k3so666046ugf for ; Fri, 12 Jan 2007 00:57:00 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=tKrPU/lS2ox03G8JY/57y0zYDyvcIgmTcOUHmR6TX3xZdavckSHgfvbGBofLo4nO669/F43guI4MGRwyGVMiJ2xNB1DrCJTTbi9f5I98CR0s2+qH+6nTqnGWeEV4rS0rC6dJQ6lFJ7PrLDdJkrwBjYrYSXew/YsOn7I7nusCnd8= Received: by 10.78.97.7 with SMTP id u7mr120782hub.1168592214231; Fri, 12 Jan 2007 00:56:54 -0800 (PST) Received: by 10.78.195.10 with HTTP; Fri, 12 Jan 2007 00:56:54 -0800 (PST) Message-ID: Date: Fri, 12 Jan 2007 08:56:54 +0000 From: "Peter Childs" Subject: Re: Planner statistics, correlations Cc: pgsql-performance@postgresql.org In-Reply-To: <20070112081631.GA10636@oppetid.no> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20070112081631.GA10636@oppetid.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/198 X-Sequence-Number: 22799 On 12/01/07, Tobias Brox wrote: > We have a table with a timestamp attribute (event_time) and a state flag > which usually changes value around the event_time (it goes to 4). Now > we have more than two years of events in the database, and around 5k of > future events. > > It is important to frequently pick out "overdue events", say: > > select * from events where state<>4 and event_time > This query would usually yield between 0 and 100 rows - however, the > planner doesn't see the correlation betewen state and event_time - since > most of the events have event_time the events with state<>4 has event_time rows is closer to 5k. This matters, because I have a query with joins, > and I would really benefit from nested loops. > > (I've tried replacing "now()" above with different timestamps from the > future and the past. I'm using pg 8.2) > > Any suggestions? > Can you say what state might be rather than what it is not. I'm guess that state is an int but there is only a limited list of possible states, if you can say what it might be rather than what it is the index is more liklly to be used. Peter.