Performance inside and outside view ( WAS Re: Select the max on a field )

From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Performance inside and outside view ( WAS Re: Select the max on a field )
Date: 2002-09-12 14:31:04
Message-ID: alq8f1$8rj$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


"Stephan Szabo" <sszabo(at)megazone23(dot)bigpanda(dot)com> wrote in message
news:20020912065516(dot)L45340-100000(at)megazone23(dot)bigpanda(dot)com(dot)(dot)(dot)
> 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_logs
WHERE id_user = 5430
ORDER 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-09-12 14:50:23 Re: Performance inside and outside view ( WAS Re: Select the max on a field )
Previous Message Jeff Eckermann 2002-09-12 14:03:45 Re: Select the max on a field