Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 December PostgreSQL: How to create a Materialized View and increase the Query Performance

PostgreSQL: How to create a Materialized View and increase the Query Performance

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

What is Materialized View?

It is a one type of view which contains the result of a query.

It caches the result of complex query and you can access the result data like a normal table.

It requires physical space to store generated data. Once we put any complex query in Materialized View, we can access that query and data without disturbing a physical base table.

Only one thing you should do is: Periodically refresh your Materialized View to get newly inserted data from the base table.

In PostgreSQL, You can create a Materialized View and can refresh it.
Let me show you, full practical on this.

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE tbl_EmployeeDetails
(
EmpID INTEGER PRIMARY KEY
,EmpName VARCHAR(50)
,Gender CHAR(1)
,EmpSalary BIGINT
,DepartmentName VARCHAR(50)
);
 
INSERT INTO tbl_EmployeeDetails
VALUES
(1,'Anvesh','M',80000,'Sales')
,(2,'Neevan','M',90000,'Sales')
,(3,'Jenny','F',50000,'Production')
,(4,'Roy','M',60000,'Production')
,(5,'Martin','M',30000,'Research')
,(6,'Mahi','F',85000,'Research')
,(7,'Kruti','F',45000,'Research')
,(8,'Manish','M',75000,'Research');

Create a materialized view to select only Male – Employee data:

1
2
3
4
5
6
7
8
9
CREATE MATERIALIZED VIEW vw_EmployeeMaleData_Materialized AS
SELECT
EmpID
,EmpName
,Gender
,EmpSalary
,DepartmentName
FROM tbl_EmployeeDetails
WHERE Gender = 'M';

Select a data of this materialized view:

1
SELECT *FROM vw_EmployeeMaleData_Materialized;

Refresh a materialized view:

Once you create a materialized view, you should also refresh it otherwise newly inserted values of the table will not update in this view.

Because It acts like a physical table and once your base table update, you should refresh the data of the materialized view.

1
REFRESH MATERIALIZED VIEW vw_EmployeeMaleData_Materialized WITH DATA;

Create another non-materialized view to select only Male – Employee data:

1
2
3
4
5
6
7
8
9
CREATE VIEW vw_EmployeeMaleData_Non_Materialized AS
SELECT
EmpID
,EmpName
,Gender
,EmpSalary
,DepartmentName
FROM tbl_EmployeeDetails
WHERE Gender = 'M';

Check the size of both views and find size difference between both views:
Non-Materialized view size is 0 byte and Materialized view size is around 8000 bytes because It stores the generated data.

1
2
SELECT pg_size_pretty(pg_total_relation_size('vw_EmployeeMaleData_Materialized'));
SELECT pg_size_pretty(pg_total_relation_size('vw_EmployeeMaleData_Non_Materialized'));

Dec 5, 2016Anvesh Patel
PostgreSQL: Difference between pg_log, pg_clog and pg_xlog log directoriesPostgreSQL: ISN Data Types to store ISBN, ISMN, ISSN, ISBN13, UPC
Comments: 1
  1. T. Godin
    December 26, 2019 at 9:05 am

    Hello,
    because of locking original table when refreshing the materialized view,
    better use :

    REFRESH MATERIALIZED VIEW CONCURRENTLY vw_EmployeeMaleData_Materialized WITH DATA;

    CONCURRENTLY is the key to avoid locking table.

Anvesh Patel
Anvesh Patel

Database Engineer

December 5, 2016 PostgreSQLAnvesh Patel, cache result, database, database research and development, dbrnd, improve query performance, Materialized View, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, query cache result
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....