Performance inside and outside view ( WAS Re: Select the max on a field ) - Mailing list pgsql-sql

From Gaetano Mendola
Subject Performance inside and outside view ( WAS Re: Select the max on a field )
Date
Msg-id alq8f1$8rj$1@news.hub.org
Whole thread Raw
In response to Re: Select the max on a field  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
Responses Re: Performance inside and outside view ( WAS Re: Select the max on a field )  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
"Stephan Szabo" <sszabo@megazone23.bigpanda.com> wrote in message
news:20020912065516.L45340-100000@megazone23.bigpanda.com...
> select distinct on (att_2) * from test
> order by att_2, att_1 desc;

Yes that's right it's help me,
but here the optimizer have some problems:


CREATE VIEW last_user_logs AS
SELECT DISTINCT ON (id_user) *
FROM user_logs
ORDER BY id_user, id_user_log DESC;

and now:

# explain analyze select * from user_logs where id_user = '5430';
NOTICE:  QUERY PLAN:

Index Scan using idx_user_user_logs on user_logs  (cost=0.00..3038.59
rows=939 width=68) (actual time=0.03..0.03 rows=0 loops=1)
Total runtime: 0.12 msec

EXPLAIN

empdb=# explain analyze select * from last_user_logs where id_user = 5430;
NOTICE:  QUERY PLAN:

Subquery Scan last_user_logs  (cost=20256.12..20725.43 rows=18772 width=68)
(actual time=3526.10..3526.10 rows=0 loops=1) ->  Unique  (cost=20256.12..20725.43 rows=18772 width=68) (actual
time=3067.14..3522.54 rows=2226 loops=1)       ->  Sort  (cost=20256.12..20256.12 rows=187723 width=68) (actual
time=3067.13..3241.94 rows=187723 loops=1)             ->  Seq Scan on user_logs  (cost=0.00..3813.23 rows=187723
width=68) (actual time=0.02..1070.59 rows=187723 loops=1)
Total runtime: 3578.07 msec

EXPLAIN


if I do instead without view and
#explain analyze SELECT DISTINCT ON (id_user) * FROM user_logsWHERE id_user = 5430ORDER BY id_user, id_user_log DESC;

NOTICE:  QUERY PLAN:

Unique  (cost=3084.93..3087.27 rows=94 width=68) (actual time=0.21..0.21
rows=0 loops=1) ->  Sort  (cost=3084.93..3084.93 rows=939 width=68) (actual
time=0.21..0.21 rows=0 loops=1)       ->  Index Scan using idx_user_user_logs on user_logs
(cost=0.00..3038.59 rows=939 width=68) (actual time=0.03..0.03 rows=0
loops=1)
Total runtime: 0.31 msec

EXPLAIN


Ciao
Gaetano






pgsql-sql by date:

Previous
From: Jeff Eckermann
Date:
Subject: Re: Select the max on a field
Next
From: Tom Lane
Date:
Subject: Re: Performance inside and outside view ( WAS Re: Select the max on a field )