<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>query cache result &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/query-cache-result/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 05 Dec 2016 18:12:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: How to create a Materialized View and increase the Query Performance</title>
		<link>https://www.dbrnd.com/2016/12/postgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data/</link>
					<comments>https://www.dbrnd.com/2016/12/postgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 05 Dec 2016 18:12:21 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[cache result]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[improve query performance]]></category>
		<category><![CDATA[Materialized View]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[query cache result]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3993</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data%2F&#038;title=PostgreSQL%3A%20How%20to%20create%20a%20Materialized%20View%20and%20increase%20the%20Query%20Performance" data-a2a-url="https://www.dbrnd.com/2016/12/postgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data/" data-a2a-title="PostgreSQL: How to create a Materialized View and increase the Query Performance"></a></p><p><strong>What is Materialized View?</strong></p>
<blockquote><p>It is a one type of view which contains the result of a query.</p></blockquote>
<blockquote><p>It caches the result of complex query and you can access the result data like a normal table.</p></blockquote>
<blockquote><p>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. </p></blockquote>
<blockquote><p>Only one thing you should do is: Periodically refresh your Materialized View to get newly inserted data from the base table. </p></blockquote>
<p><strong>In PostgreSQL, You can create a Materialized View and can refresh it. </strong><br />
Let me show you, full practical on this. </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">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');</pre> </p>
<p><strong>Create a materialized view to select only Male &#8211; Employee data:</strong></p>
<pre class="crayon-plain-tag">CREATE MATERIALIZED VIEW vw_EmployeeMaleData_Materialized AS 
SELECT 
	EmpID
	,EmpName
	,Gender
	,EmpSalary
	,DepartmentName
FROM tbl_EmployeeDetails
WHERE Gender = 'M';</pre> </p>
<p><strong>Select a data of this materialized view:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM vw_EmployeeMaleData_Materialized;</pre> </p>
<p><strong>Refresh a materialized view:</strong></p>
<blockquote><p>Once you create a materialized view, you should also refresh it otherwise newly inserted values of the table will not update in this view. </p>
<p>Because It acts like a physical table and once your base table update, you should refresh the data of the materialized view. </p>
<pre class="crayon-plain-tag">REFRESH MATERIALIZED VIEW vw_EmployeeMaleData_Materialized WITH DATA;</pre> </p>
</blockquote>
<p><strong>Create another non-materialized view to select only Male &#8211; Employee data:</strong></p>
<pre class="crayon-plain-tag">CREATE VIEW vw_EmployeeMaleData_Non_Materialized AS 
SELECT 
	EmpID
	,EmpName
	,Gender
	,EmpSalary
	,DepartmentName
FROM tbl_EmployeeDetails
WHERE Gender = 'M';</pre> </p>
<p><strong>Check the size of both views and find size difference between both views:</strong><br />
Non-Materialized view size is 0 byte and Materialized view size is around 8000 bytes because It stores the generated data.</p>
<pre class="crayon-plain-tag">SELECT pg_size_pretty(pg_total_relation_size('vw_EmployeeMaleData_Materialized'));
SELECT pg_size_pretty(pg_total_relation_size('vw_EmployeeMaleData_Non_Materialized'));</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/12/postgresql-how-to-create-a-materialized-view-and-increase-the-query-performance-refresh-data/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
