From: | "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com> |
---|---|
To: | K C Lau <kclau60(at)netvigator(dot)com> |
Cc: | pgsql-performance(at)postgresql(dot)org |
Subject: | Re: SELECT LIMIT 1 VIEW Performance Issue |
Date: | 2005-10-04 21:31:54 |
Message-ID: | 20051004213154.GC40138@pervasive.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
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(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461
From | Date | Subject | |
---|---|---|---|
Next Message | Joe | 2005-10-04 21:37:30 | Re: Comparative performance |
Previous Message | Jim C. Nasby | 2005-10-04 21:15:41 | Re: SELECT LIMIT 1 VIEW Performance Issue |