Re: Do Views execute underlying query everytime ?? - Mailing list pgsql-performance

From Amit V Shah
Subject Re: Do Views execute underlying query everytime ??
Date
Msg-id 0C072E7CC947D511AC9600A0CC7341200256CF53@vpn.tagaudit.com
Whole thread Raw
In response to Do Views execute underlying query everytime ??  (Amit V Shah <ashah@tagaudit.com>)
Responses Re: Do Views execute underlying query everytime ??  (PFC <lists@boutiquenumerique.com>)
List pgsql-performance
First of all, thanks to everyone for helping me !

Looks like materialized views will be my answer.

Let me explain my situation a little better.

The repository table looks like this -

create table repository (statName varchar(45), statValue varchar(45),
metaData varchar(45));

MetaData is a foreign key to other tables.

The screens show something like following -

Screen 1 -
Stat1      Stat2        Stat3
Value      Value        Value
Value      Value        Value



Screen 2 -
Stat3      Stat1        Stat5
Value      Value        Value
Value      Value        Value


etc. etc.

The data is grouped based on metaData.

Updates will only occur nightly and can be controlled. But selects occur
9-5.

One of the compelling reasons I feel is that to create such tables out of
repository tables, the query would be very complicated. If I have a
materialized view, I think the information will be "cached".

Another concern I have is load. If I have lot of simultaneous users creating
such "wide tables" out of one "long table", that would generate substantial
load on the servers. ??

I like the materialized view solution better than having other tables for
each screen. (Would be nice if someone can comment on that)

So that is my situation.

Again, thanks everyone for helping
Amit

-----Original Message-----
From: John A Meinel [mailto:john@arbash-meinel.com]
Sent: Tuesday, June 21, 2005 11:01 AM
To: Amit V Shah
Cc: 'newz@bearfruit.org'; pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Do Views execute underlying query everytime ??


Amit V Shah wrote:

>After I sent out this email, I found this article from google
>
>http://jonathangardner.net/PostgreSQL/materialized_views/matviews.html
>
>Looks like we can control as to when the views refresh... I am still kind
of
>confused, and would appreciate help !!
>
>The create/drop table does sound a solution that can work, but the thing is
>I want to get manual intervention out, and besides, my work flow is very
>complex so this might not be an option for me :-(
>
>Thanks,
>Amit
>

Just to make it clear, a view is not the same as a materialized view.
A view is just a set of rules to the planner so that it can simplify
interactions with the database. A materialized view is a query which has
been saved into a table.

To set it up properly, really depends on what your needs are.

   1. How much time can elapse between an update to the system, and an
      update to the materialized views?
   2. How many updates / (sec, min, hour, month) do you expect. Is
      insert performance critical, or secondary.

For instance, if you get a lot of updates, but you can have a 1 hour lag
between the time a new row is inserted and the view is updated, you can
just create a cron job that runs every hour to regenerate the
materialized view.

If you don't get many updates, but you need them to show up right away,
then you can add triggers to the affected tables, such that
inserting/updating to a specific table causes an update to the
materialized view.

There are quite a few potential tradeoffs. Rather than doing a
materialized view, you could just improve your filters. If you are doing
a query to show people the results, you generally have some sort of
upper bound on how much data you can display. Humans don't like reading
more than 100 or 1000 rows. So create your normal query, and just take
on a LIMIT 100 at the end. If you structure your query properly, and
have appropriate indexes, you should be able to make the LIMIT count,
and allow you to save a lot of overhead of generating rows that you
don't use.

I would probably start by posting the queries you are currently using,
along with an EXPLAIN ANALYZE, and a description of what you actually
need from the query. Then this list can be quite helpful in
restructuring your query to make it faster.

John
=:->


pgsql-performance by date:

Previous
From: Yves Vindevogel
Date:
Subject: Re: Another question on indexes (drop and recreate)
Next
From: John A Meinel
Date:
Subject: Re: Another question on indexes (drop and recreate)