pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John A Meinel <john@arbash-meinel.com>
To: Amit V Shah <ashah@tagaudit.com>
Cc: 'newz@bearfruit.org' <newz@bearfruit.org>
Cc: pgsql-performance@postgresql.org
Subject: Re: Do Views execute underlying query everytime ??
Date: Tue, 21 Jun 2005 10:01:13 -0500
Message-ID: <42B82BB9.8030300@arbash-meinel.com> (raw)
In-Reply-To: <0C072E7CC947D511AC9600A0CC7341200256CF52@vpn.tagaudit.com>
References: <0C072E7CC947D511AC9600A0CC7341200256CF52@vpn.tagaudit.com>

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
=:->

Attachments:

  signature.asc (application/pgp-signature, 252B) download

view thread (9+ messages)  latest in thread

Message-ID: <42B82BB9.8030300@arbash-meinel.com>
Permalink:  ../42B82BB9.8030300@arbash-meinel.com/
Also on:    postgresql.org/message-id/42B82BB9.8030300@arbash-meinel.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: john@arbash-meinel.com, ashah@tagaudit.com, newz@bearfruit.org
  Subject: Re: Do Views execute underlying query everytime ??
  In-Reply-To: <42B82BB9.8030300@arbash-meinel.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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