X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CF6EBD946C for ; Tue, 4 Oct 2005 18:31:55 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91225-01 for ; Tue, 4 Oct 2005 21:31:52 +0000 (GMT) Received: from flake.decibel.org (flake.decibel.org [67.100.216.10]) by svr1.postgresql.org (Postfix) with ESMTP id 79856DA0B3 for ; Tue, 4 Oct 2005 18:31:52 -0300 (ADT) Received: by flake.decibel.org (Postfix, from userid 1001) id 0ED7F1524F; Tue, 4 Oct 2005 16:31:54 -0500 (CDT) Date: Tue, 4 Oct 2005 16:31:54 -0500 From: "Jim C. Nasby" To: K C Lau Cc: pgsql-performance@postgresql.org Subject: Re: SELECT LIMIT 1 VIEW Performance Issue Message-ID: <20051004213154.GC40138@pervasive.com> References: <6EE64EF3AB31D5448D0007DD34EEB3417DD406@Herge.rcsinc.local> <6.2.1.2.0.20050923155952.05889a50@localhost> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6.2.1.2.0.20050923155952.05889a50@localhost> X-Operating-System: FreeBSD 4.11-RELEASE-p10 i386 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 required=5 tests=[AWL=0.008] X-Spam-Level: X-Archive-Number: 200510/54 X-Sequence-Number: 14822 On Fri, Sep 23, 2005 at 04:53:55PM +0800, K C Lau wrote: > Thank you all for your suggestions. I' tried, with some variations too, but > still no success. The times given are the best of a few repeated tries on > an 8.1 beta 2 db freshly migrated from 8.0.3 on Windows. > > For reference, only the following gets the record quickly: > > esdt=> explain analyze select PlayerID,AtDate from Player a > where PlayerID='22220' and AtDate = (select b.AtDate from Player b > where b.PlayerID = '22220' order by b.PlayerID desc, b.AtDate desc LIMIT > 1); > > Index Scan using pk_player on player a (cost=0.75..4.26 rows=1 width=23) > (actual time=0.054..0.057 rows=1 loops=1) > Index Cond: (((playerid)::text = '22220'::text) AND ((atdate)::text = > ($0)::text)) > InitPlan > -> Limit (cost=0.00..0.75 rows=1 width=23) (actual > time=0.027..0.028 rows=1 loops=1) > -> Index Scan Backward using pk_player on player > b (cost=0.00..1323.05 rows=1756 width=23) (actual time=0.023..0.023 rows=1 > loops=1) > Index Cond: ((playerid)::text = '22220'::text) > Total runtime: 0.132 ms If you're doing that, you should try something like the following: decibel=# explain analyze select * from t where ctid=(select ctid from rrs order by rrs_id desc limit 1); QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------- Tid Scan on t (cost=0.44..4.45 rows=1 width=42) (actual time=0.750..0.754 rows=1 loops=1) Filter: (ctid = $0) InitPlan -> Limit (cost=0.00..0.44 rows=1 width=10) (actual time=0.548..0.549 rows=1 loops=1) -> Index Scan Backward using rrs_rrs__rrs_id on rrs (cost=0.00..3.08 rows=7 width=10) (actual time=0.541..0.541 rows=1 loops=1) Total runtime: 1.061 ms (6 rows) decibel=# select count(*) from t; count -------- 458752 Note that that's on my nice slow laptop to boot (the count took like 10 seconds). Just remember that ctid *is not safe outside of a transaction*!! So you can't do something like SELECT ctid FROM ... store that in some variable... SELECT * FROM table WHERE ctid = variable -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461