X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 612BA9DC83A for ; Sat, 21 Jan 2006 09:39:12 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65458-03 for ; Sat, 21 Jan 2006 09:39:15 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from imsm057dat.netvigator.com (imsm057.netvigator.com [218.102.48.210]) by postgresql.org (Postfix) with ESMTP id 83C629DC816 for ; Sat, 21 Jan 2006 09:39:09 -0400 (AST) Received: from n2.netvigator.com ([218.102.95.109]) by imsm057dat.netvigator.com (InterMail vM.6.01.05.02 201-2131-123-102-20050715) with ESMTP id <20060121133912.OAEY14795.imsm057dat.netvigator.com@n2.netvigator.com>; Sat, 21 Jan 2006 21:39:12 +0800 Received: from n2.netvigator.com ([127.0.0.1]) by [127.0.0.1] with ESMTP (SpamPal v1.591) sender ; 21 Jan 2006 21:39:12 +???? Message-Id: <6.2.1.2.0.20060121211310.08cc91c8@localhost> X-Mailer: QUALCOMM Windows Eudora Version 6.2.1.2 Date: Sat, 21 Jan 2006 21:38:55 +0800 To: Tom Lane From: K C Lau Subject: Re: SELECT MIN, MAX took longer time than SELECT Cc: pgsql-performance@postgresql.org In-Reply-To: <25978.1137783074@sss.pgh.pa.us> References: <6.2.1.2.0.20060120120150.08ab7b00@localhost> <20060120172026.GF20182@pervasive.com> <25978.1137783074@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.2 required=5 tests=[AWL=-0.240, DNS_FROM_RFC_POST=1.44] X-Spam-Score: 1.2 X-Spam-Level: * X-Archive-Number: 200601/390 X-Sequence-Number: 16868 I have worked round the issue by using 2 separate queries with the LIMIT construct. LogSN and create_time are indeed directly correlated, both monotonously increasing, occasionally with multiple LogSN's having the same create_time. What puzzles me is why the query with COUNT, MIN, MAX uses idx_logtime for the scan, but the query without the COUNT uses pk_log and takes much longer. If it had chosen idx_logtime instead, then it should have returned immediately for both MIN and MAX. Best regards, KC. At 02:51 06/01/21, Tom Lane wrote: >"Jim C. Nasby" writes: > > On Fri, Jan 20, 2006 at 12:35:36PM +0800, K C Lau wrote: > > Here's the problem... the estimate for the backwards index scan is *way* > > off: > > >> -> Limit (cost=0.00..1.26 rows=1 width=4) (actual > >> time=200032.928..200032.931 rows=1 loops=1) > >> -> Index Scan Backward using pk_log on > >> log (cost=0.00..108047.11 rows=86089 width=4) (actual > >> time=200032.920..200032.920 rows=1 loops=1) > >> Filter: (((create_time)::text < '2005/10/19'::text) AND > >> (logsn IS NOT NULL)) > >> Total runtime: 200051.701 ms > >It's more subtle than you think. The estimated rowcount is the >estimated number of rows fetched if the indexscan were run to >completion, which it isn't because the LIMIT cuts it off after the >first returned row. That estimate is not bad (we can see from the >aggregate plan that the true value would have been 106708, assuming >that the "logsn IS NOT NULL" condition isn't filtering anything). > >The real problem is that it's taking quite a long time for the scan >to reach the first row with create_time < 2005/10/19, which is not >too surprising if logsn is strongly correlated with create_time ... >but in the absence of any cross-column statistics the planner has >no very good way to know that. (Hm ... but both of them probably >also show a strong correlation to physical order ... we could look >at that maybe ...) The default assumption is that the two columns >aren't correlated and so it should not take long to hit the first such >row, which is why the planner likes the indexscan/limit plan. > > regards, tom lane