pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Index Scan Backward
5+ messages / 2 participants
[nested] [flat]

* Index Scan Backward
@ 2003-01-27 13:26 Luca Fabbro <lfabbro@conecta.it>
  2003-01-27 14:39 ` Re: Index Scan Backward Tom Lane <tgl@sss.pgh.pa.us>
  2003-01-27 14:52 ` Re: Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
  0 siblings, 2 replies; 5+ messages in thread

From: Luca Fabbro @ 2003-01-27 13:26 UTC (permalink / raw)
  To: pgsql-admin

Hi all,
  I'm experiencing a strange problem in the usage of indexes for query 
optimization.
I'm runnig a "forum" application that uses a PostgreSQL DB.
The version on the DB is 7.2.3 but I've also tested it under 7.3.1 but I 
had no luck :( The problem is always the same.
Linux distro is Slackware 8.1
Since some days ago everithing was working fine but in this last 3 days 
something really strnge happened.
Some of the SELECT queries became really slow.
In fact all the queries like this:

SELECT p.* FROM forum_post AS p, forum_topic AS t WHERE p.topicid = t.id 
AND t.forumid = 44 ORDER BY p.id DESC LIMIT 1;

Having found that the slow slect queries where this type I've tried the EXPLAIN

explain SELECT p.* FROM forum_post AS p, forum_topic AS t WHERE p.topicid = 
t.id AND t.forumid = 44 ORDER BY p.id DESC LIMIT 1;
                                                      QUERY 
PLAN
---------------------------------------------------------------------------------------------------------------------
  Limit  (cost=0.00..1003.36 rows=1 width=454)
    ->  Nested Loop  (cost=0.00..392651.18 rows=391 width=454)
          ->  Index Scan Backward using forum_post_id_key on forum_post 
p  (cost=0.00..35615.95 rows=60668 width=450)
          ->  Index Scan using forum_topic_id_key on forum_topic 
t  (cost=0.00..5.87 rows=1 width=4)
                Index Cond: ("outer".topicid = t.id)
                Filter: (forumid = 44)
(6 rows)

It seems that the problem is in the Backward scan of the index :(

I've tried so to order the data by 'date' which is like ordering by id as 
id is a serial

                                                   QUERY 
PLAN
--------------------------------------------------------------------------------------------------------------
  Limit  (cost=1087.72..1087.72 rows=1 width=454)
    ->  Sort  (cost=1087.72..1088.70 rows=391 width=454)
          Sort Key: p.date
          ->  Nested Loop  (cost=0.00..1070.87 rows=391 width=454)
                ->  Index Scan using forum_topic_forumid on forum_topic 
t  (cost=0.00..113.40 rows=37 width=4)
                      Index Cond: (forumid = 44)
                ->  Index Scan using forum_post_topicid on forum_post 
p  (cost=0.00..25.82 rows=22 width=450)
                      Index Cond: (p.topicid = "outer".id)
(8 rows)

In this way the query is 3 time faster tha the one above wich is using index.
I do a VACUUM VERBOSE ANALYZE every night so de DB is "clean".
I've also tried to VACUUM or ANALYZE but had no benefits.

Did I miss something or is it a bug of postgres?

Thanks in advance

Ciao
	Luca




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Index Scan Backward
  2003-01-27 13:26 Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
@ 2003-01-27 14:39 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2003-01-27 14:39 UTC (permalink / raw)
  To: Luca Fabbro <lfabbro@conecta.it>; +Cc: pgsql-admin

Luca Fabbro <lfabbro@conecta.it> writes:
> It seems that the problem is in the Backward scan of the index :(

It looks like a pretty reasonable plan to me.  Could we see the output
of EXPLAIN ANALYZE, not just EXPLAIN?

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Index Scan Backward
  2003-01-27 13:26 Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
@ 2003-01-27 14:52 ` Luca Fabbro <lfabbro@conecta.it>
  2003-01-27 15:17   ` Re: Index Scan Backward Tom Lane <tgl@sss.pgh.pa.us>
  2003-01-28 08:58   ` Re: Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
  1 sibling, 2 replies; 5+ messages in thread

From: Luca Fabbro @ 2003-01-27 14:52 UTC (permalink / raw)
  To: pgsql-admin

At 09.39 27/01/2003 -0500, you wrote:
>Luca Fabbro <lfabbro@conecta.it> writes:
> > It seems that the problem is in the Backward scan of the index :(
>
>It looks like a pretty reasonable plan to me.  Could we see the output
>of EXPLAIN ANALYZE, not just EXPLAIN?

Thanks Tom for your interest.

It looks resonable also for me, but it's not too efficient.

>                         regards, tom lane

Here are the detailed explain

explain analyze SELECT p.* FROM forum_post AS p, forum_topic AS t WHERE 
p.topicid = t.id AND t.forumid = 44 ORDER BY p.id DESC LIMIT 1;
                                                                              QUERY 
PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Limit  (cost=0.00..1003.36 rows=1 width=454) (actual time=806.78..2097.61 
rows=1 loops=1)
    ->  Nested Loop  (cost=0.00..392651.18 rows=391 width=454) (actual 
time=806.77..2097.59 rows=2 loops=1)
          ->  Index Scan Backward using forum_post_id_key on forum_post 
p  (cost=0.00..35615.95 rows=60668 width=450) (actual time=0.41..1122.09 
rows=42322 loops=1)
          ->  Index Scan using forum_topic_id_key on forum_topic 
t  (cost=0.00..5.87 rows=1 width=4) (actual time=0.02..0.02 rows=0 loops=42322)
                Index Cond: ("outer".topicid = t.id)
                Filter: (forumid = 44)
  Total runtime: 2098.14 msec

explain analyze SELECT p.* FROM forum_post AS p, forum_topic AS t WHERE 
p.topicid = t.id AND t.forumid = 44 ORDER BY p.date DESC LIMIT 1;
                                                                       QUERY 
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
  Limit  (cost=1087.72..1087.72 rows=1 width=454) (actual time=1.89..1.90 
rows=1 loops=1)
    ->  Sort  (cost=1087.72..1088.70 rows=391 width=454) (actual 
time=1.89..1.89 rows=2 loops=1)
          Sort Key: p.date
          ->  Nested Loop  (cost=0.00..1070.87 rows=391 width=454) (actual 
time=0.64..1.11 rows=6 loops=1)
                ->  Index Scan using forum_topic_forumid on forum_topic 
t  (cost=0.00..113.40 rows=37 width=4) (actual time=0.27..0.28 rows=2 loops=1)
                      Index Cond: (forumid = 44)
                ->  Index Scan using forum_post_topicid on forum_post 
p  (cost=0.00..25.82 rows=22 width=450) (actual time=0.22..0.37 rows=3 loops=2)
                      Index Cond: (p.topicid = "outer".id)
  Total runtime: 2.06 msec




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Index Scan Backward
  2003-01-27 13:26 Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
  2003-01-27 14:52 ` Re: Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
@ 2003-01-27 15:17   ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2003-01-27 15:17 UTC (permalink / raw)
  To: Luca Fabbro <lfabbro@conecta.it>; +Cc: pgsql-admin

Luca Fabbro <lfabbro@conecta.it> writes:
>   Limit  (cost=0.00..1003.36 rows=1 width=454) (actual time=806.78..2097.61 
> rows=1 loops=1)
>     ->  Nested Loop  (cost=0.00..392651.18 rows=391 width=454) (actual 
> time=806.77..2097.59 rows=2 loops=1)
>           ->  Index Scan Backward using forum_post_id_key on forum_post 
> p  (cost=0.00..35615.95 rows=60668 width=450) (actual time=0.41..1122.09 
> rows=42322 loops=1)
>           ->  Index Scan using forum_topic_id_key on forum_topic 
> t  (cost=0.00..5.87 rows=1 width=4) (actual time=0.02..0.02 rows=0 loops=42322)
>                 Index Cond: ("outer".topicid = t.id)
>                 Filter: (forumid = 44)
>   Total runtime: 2098.14 msec

Hm.  So the reason this is slow is it has to go back quite far in the id
index before it finds something from forumid 44.  The system is in fact
estimating it as a moderately expensive query --- but not quite
expensive enough.  You might try raising RANDOM_PAGE_COST a little to
see if that brings the cost estimates in line with reality.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Index Scan Backward
  2003-01-27 13:26 Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
  2003-01-27 14:52 ` Re: Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
@ 2003-01-28 08:58   ` Luca Fabbro <lfabbro@conecta.it>
  1 sibling, 0 replies; 5+ messages in thread

From: Luca Fabbro @ 2003-01-28 08:58 UTC (permalink / raw)
  To: pgsql-admin

At 10.17 27/01/2003 -0500, you wrote:
> >   Limit  (cost=0.00..1003.36 rows=1 width=454) (actual 
> time=806.78..2097.61
> > rows=1 loops=1)
> >     ->  Nested Loop  (cost=0.00..392651.18 rows=391 width=454) (actual
> > time=806.77..2097.59 rows=2 loops=1)
> >           ->  Index Scan Backward using forum_post_id_key on forum_post
> > p  (cost=0.00..35615.95 rows=60668 width=450) (actual time=0.41..1122.09
> > rows=42322 loops=1)
> >           ->  Index Scan using forum_topic_id_key on forum_topic
> > t  (cost=0.00..5.87 rows=1 width=4) (actual time=0.02..0.02 rows=0 
> loops=42322)
> >                 Index Cond: ("outer".topicid = t.id)
> >                 Filter: (forumid = 44)
> >   Total runtime: 2098.14 msec
>
>Hm.  So the reason this is slow is it has to go back quite far in the id
>index before it finds something from forumid 44.  The system is in fact
>estimating it as a moderately expensive query --- but not quite
>expensive enough.  You might try raising RANDOM_PAGE_COST a little to
>see if that brings the cost estimates in line with reality.

Thanks again Tom.
         I'm trying to find out the best values to assign to the 
RANDOM_PAGE_COST and related vars to have the best performances.
You were right in fact queries where the last data insertion was not so 
"old" are quick while the "oldest" one using Backwards Indexing were really 
slow. The problem is that changing these vaules affects also queries where 
the use of Index in standard way improves a lot the speed, in changing the 
values sometimes it happens that this kind of queries are made as 
sequential scan and so they slow down and sometime the overall computation 
time is higher than the one with no indexes at all.
I'll see what I can do :)
P.S. Is there any place where the configuration vars are explained in a 
more detailed way than in the offical manual?

Thanks
         Luca




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2003-01-28 08:58 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-27 13:26 Index Scan Backward Luca Fabbro <lfabbro@conecta.it>
2003-01-27 14:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-01-27 14:52 ` Luca Fabbro <lfabbro@conecta.it>
2003-01-27 15:17   ` Tom Lane <tgl@sss.pgh.pa.us>
2003-01-28 08:58   ` Luca Fabbro <lfabbro@conecta.it>

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