Re: SELECT LIMIT 1 VIEW Performance Issue

Поиск
Список
Период
Сортировка
От Jim C. Nasby
Тема Re: SELECT LIMIT 1 VIEW Performance Issue
Дата
Msg-id 20051004213154.GC40138@pervasive.com
обсуждение исходный текст
Ответ на Re: SELECT LIMIT 1 VIEW Performance Issue  (K C Lau <kclau60@netvigator.com>)
Список 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.541rows=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

В списке pgsql-performance по дате отправления:

Предыдущее
От: "Jim C. Nasby"
Дата:
Сообщение: Re: SELECT LIMIT 1 VIEW Performance Issue
Следующее
От: Joe
Дата:
Сообщение: Re: Comparative performance