Re: Performance difference when using views

From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Alvaro Nunes Melo <al_nunes(at)atua(dot)com(dot)br>
Cc: Pgsql-Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Performance difference when using views
Date: 2004-11-01 22:28:50
Message-ID: 1099348130.2709.105.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Mon, 2004-11-01 at 21:40, Alvaro Nunes Melo wrote:
> Hi,
>
> I have some views that are used to make some queries simplest. But when
> I use them there is a performance loss, because the query don't use
> indexes anymore. Below I'm sending the query with and without the view,
> its execution times, explains and the view's body. I didn't understood
> the why the performance is so different (20x in seconds, 1000x in page
> reads) if the queries are semantically identical.
>
> Shouldn't I use views in situations like this? Is there some way to use
> the view and the indexes?
>
> --------------
> -- View body
> --------------
>
> CREATE VIEW vw_test AS
> SELECT e.person_id, ci.city_id, ci.city_name, s.state_id,
> s.state_acronym
> FROM address a
> LEFT OUTER JOIN zip zp ON a.zip_code_id = zp.zip_code_id
> LEFT OUTER JOIN city ci ON ci.city_id = zp.city_id
> LEFT OUTER JOIN state s ON ci.state_id = s.state_id
> WHERE a.adress_type = 2;
>
> ---------------------
> -- Without the view
> ---------------------
>
> SELECT p.person_id, ci.city_id, ci.city_name, s.state_id,
> s.state_acronym
> FROM person p
> LEFT OUTER JOIN address e USING (person_id)
> LEFT OUTER JOIN zip zp ON a.zip_code_id = zp.zip_code_id
> LEFT OUTER JOIN city ci ON ci.city_id = zp.city_id
> LEFT OUTER JOIN state u ON ci.state_id = s.state_id
> WHERE a.adress_type = 2
> AND p.person_id = 19257;
>

Try this....

SELECT p.person_id, ci.city_id, ci.city_name, s.state_id,
s.state_acronym
FROM person p
LEFT OUTER JOIN ( address a
LEFT OUTER JOIN zip zp ON a.zip_code_id = zp.zip_code_id
LEFT OUTER JOIN city ci ON ci.city_id = zp.city_id
LEFT OUTER JOIN state u ON ci.state_id = s.state_id )
USING (person_id)
WHERE a.adress_type = 2
AND p.person_id = 19257;

Which should return the same answer, and also hopefully the same plan.

--
Best Regards, Simon Riggs

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Neil Conway 2004-11-02 01:35:55 Re: Speeding up Gist Index creations
Previous Message Tom Lane 2004-11-01 22:08:28 Re: Performance difference when using views