Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much space do VIEWS occupy in PostgreSQL?

If I store my query results as views does it take more space of my memory in comparison to a table with query results?

Another question about views is that can I have new query based on the results of a query that is stored as views?

like image 756
f.ashouri Avatar asked Nov 13 '12 09:11

f.ashouri


People also ask

How much space does view occupy in database?

For this reason, the view does not take up any disk space for data storage, and it does not create any redundant copies of data that is already stored in the tables that it references (which are sometimes called the base tables of the view).

Do views occupy space?

Views take very little space to store; the database contains only the definition of a view, not a copy of all the data that it presents. Depending on the SQL engine used, views can provide extra security.

Do views consume memory?

views do not take up RAM ("memory").

Does view store data in PostgreSQL?

A view can be accessed as a virtual table in PostgreSQL. In other words, a PostgreSQL view is a logical table that represents data of one or more underlying tables through a SELECT statement. Notice that a view does not store data physically except for a materialized view.


3 Answers

Views don't store query results, they store queries.

Some RDBMS allow the way to store query results (for some queries): this is called materialized views in Oracle and indexed views in SQL Server.

PostgreSQL does not support those (though, as @CalvinCheng mentioned, you can emulate those using triggers or rules).

Yes, you can use views in your queries. However, a view is just a convenient way to refer to a complex query by name, not a way to store its results.

like image 123
Quassnoi Avatar answered Oct 06 '22 03:10

Quassnoi


For Question 1

To answer your first question, you cannot store your query results as views but you can achieve a similar functionality using PostgreSQL's trigger feature.

PostgreSQL supports creation of views natively but not the creation of materialized views (views that store your results) - but this can be handled using triggers. See http://wiki.postgresql.org/wiki/Materialized_Views

views do not take up RAM ("memory").

For Question 2

And to answer the second question, to update a view in postgresql, you will need to use CREATE RULE - http://www.postgresql.org/docs/devel/static/sql-createrule.html

CREATE RULE defines a new rule applying to a specified table or view. CREATE OR REPLACE RULE will either create a new rule, or replace an existing rule of the same name for the same table.

like image 28
Calvin Cheng Avatar answered Oct 06 '22 03:10

Calvin Cheng


I would like to point out that as of Postgres 9.3, Materialized Views are supported

like image 43
Michael Draper Avatar answered Oct 06 '22 02:10

Michael Draper