pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jim C. Nasby <jnasby@pervasive.com>
To: K C Lau <kclau60@netvigator.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: SELECT LIMIT 1 VIEW Performance Issue
Date: Tue, 4 Oct 2005 16:31:54 -0500
Message-ID: <20051004213154.GC40138@pervasive.com> (raw)
In-Reply-To: <6.2.1.2.0.20050923155952.05889a50@localhost>
References: <6EE64EF3AB31D5448D0007DD34EEB3417DD406@Herge.rcsinc.local>
	<6.2.1.2.0.20050923155952.05889a50@localhost>

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



view thread (27+ messages)  latest in thread

Message-ID: <20051004213154.GC40138@pervasive.com>
Permalink:  ../20051004213154.GC40138@pervasive.com/
Also on:    postgresql.org/message-id/20051004213154.GC40138@pervasive.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: jnasby@pervasive.com, kclau60@netvigator.com
  Subject: Re: SELECT LIMIT 1 VIEW Performance Issue
  In-Reply-To: <20051004213154.GC40138@pervasive.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox