codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>41.3. Materialized Views</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="rules-views.html" title="41.2. Views and the Rule System" /><link rel="next" href="rules-update.html" title="41.4. Rules on INSERT, UPDATE, and DELETE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">41.3. Materialized Views</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="rules-views.html" title="41.2. Views and the Rule System">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="rules.html" title="Chapter 41. The Rule System">Up</a></td><th width="60%" align="center">Chapter 41. The Rule System</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="rules-update.html" title="41.4. Rules on INSERT, UPDATE, and DELETE">Next</a></td></tr></table><hr /></div><div class="sect1" id="RULES-MATERIALIZEDVIEWS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">41.3. Materialized Views <a href="#RULES-MATERIALIZEDVIEWS" class="id_link">#</a></h2></div></div></div><a id="id-1.8.6.8.2" class="indexterm"></a><a id="id-1.8.6.8.3" class="indexterm"></a><a id="id-1.8.6.8.4" class="indexterm"></a><p>3 Materialized views in <span class="productname">PostgreSQL</span> use the4 rule system like views do, but persist the results in a table-like form.5 The main differences between:6 7</p><pre class="programlisting">8CREATE MATERIALIZED VIEW mymatview AS SELECT * FROM mytab;9</pre><p>10 11 and:12 13</p><pre class="programlisting">14CREATE TABLE mymatview AS SELECT * FROM mytab;15</pre><p>16 17 are that the materialized view cannot subsequently be directly updated18 and that the query used to create the materialized view is stored in19 exactly the same way that a view's query is stored, so that fresh data20 can be generated for the materialized view with:21 22</p><pre class="programlisting">23REFRESH MATERIALIZED VIEW mymatview;24</pre><p>25 26 The information about a materialized view in the27 <span class="productname">PostgreSQL</span> system catalogs is exactly28 the same as it is for a table or view. So for the parser, a29 materialized view is a relation, just like a table or a view. When30 a materialized view is referenced in a query, the data is returned31 directly from the materialized view, like from a table; the rule is32 only used for populating the materialized view.33</p><p>34 While access to the data stored in a materialized view is often much35 faster than accessing the underlying tables directly or through a view,36 the data is not always current; yet sometimes current data is not needed.37 Consider a table which records sales:38 39</p><pre class="programlisting">40CREATE TABLE invoice (41 invoice_no integer PRIMARY KEY,42 seller_no integer, -- ID of salesperson43 invoice_date date, -- date of sale44 invoice_amt numeric(13,2) -- amount of sale45);46</pre><p>47 48 If people want to be able to quickly graph historical sales data, they49 might want to summarize, and they may not care about the incomplete data50 for the current date:51 52</p><pre class="programlisting">53CREATE MATERIALIZED VIEW sales_summary AS54 SELECT55 seller_no,56 invoice_date,57 sum(invoice_amt)::numeric(13,2) as sales_amt58 FROM invoice59 WHERE invoice_date < CURRENT_DATE60 GROUP BY61 seller_no,62 invoice_date;63 64CREATE UNIQUE INDEX sales_summary_seller65 ON sales_summary (seller_no, invoice_date);66</pre><p>67 68 This materialized view might be useful for displaying a graph in the69 dashboard created for salespeople. A job could be scheduled to update70 the statistics each night using this SQL statement:71 72</p><pre class="programlisting">73REFRESH MATERIALIZED VIEW sales_summary;74</pre><p>75</p><p>76 Another use for a materialized view is to allow faster access to data77 brought across from a remote system through a foreign data wrapper.78 A simple example using <code class="literal">file_fdw</code> is below, with timings,79 but since this is using cache on the local system the performance80 difference compared to access to a remote system would usually be greater81 than shown here. Notice we are also exploiting the ability to put an82 index on the materialized view, whereas <code class="literal">file_fdw</code> does83 not support indexes; this advantage might not apply for other sorts of84 foreign data access.85</p><p>86 Setup:87 88</p><pre class="programlisting">89CREATE EXTENSION file_fdw;90CREATE SERVER local_file FOREIGN DATA WRAPPER file_fdw;91CREATE FOREIGN TABLE words (word text NOT NULL)92 SERVER local_file93 OPTIONS (filename '/usr/share/dict/words');94CREATE MATERIALIZED VIEW wrd AS SELECT * FROM words;95CREATE UNIQUE INDEX wrd_word ON wrd (word);96CREATE EXTENSION pg_trgm;97CREATE INDEX wrd_trgm ON wrd USING gist (word gist_trgm_ops);98VACUUM ANALYZE wrd;99</pre><p>100 101 Now let's spell-check a word. Using <code class="literal">file_fdw</code> directly:102 103</p><pre class="programlisting">104SELECT count(*) FROM words WHERE word = 'caterpiler';105 106 count107-------108 0109(1 row)110</pre><p>111 112 With <code class="command">EXPLAIN ANALYZE</code>, we see:113 114</p><pre class="programlisting">115 Aggregate (cost=21763.99..21764.00 rows=1 width=0) (actual time=188.180..188.181 rows=1 loops=1)116 -> Foreign Scan on words (cost=0.00..21761.41 rows=1032 width=0) (actual time=188.177..188.177 rows=0 loops=1)117 Filter: (word = 'caterpiler'::text)118 Rows Removed by Filter: 479829119 Foreign File: /usr/share/dict/words120 Foreign File Size: 4953699121 Planning time: 0.118 ms122 Execution time: 188.273 ms123</pre><p>124 125 If the materialized view is used instead, the query is much faster:126 127</p><pre class="programlisting">128 Aggregate (cost=4.44..4.45 rows=1 width=0) (actual time=0.042..0.042 rows=1 loops=1)129 -> Index Only Scan using wrd_word on wrd (cost=0.42..4.44 rows=1 width=0) (actual time=0.039..0.039 rows=0 loops=1)130 Index Cond: (word = 'caterpiler'::text)131 Heap Fetches: 0132 Planning time: 0.164 ms133 Execution time: 0.117 ms134</pre><p>135 136 Either way, the word is spelled wrong, so let's look for what we might137 have wanted. Again using <code class="literal">file_fdw</code> and138 <code class="literal">pg_trgm</code>:139 140</p><pre class="programlisting">141SELECT word FROM words ORDER BY word <-> 'caterpiler' LIMIT 10;142 143 word144---------------145 cater146 caterpillar147 Caterpillar148 caterpillars149 caterpillar's150 Caterpillar's151 caterer152 caterer's153 caters154 catered155(10 rows)156</pre><p>157 158</p><pre class="programlisting">159 Limit (cost=11583.61..11583.64 rows=10 width=32) (actual time=1431.591..1431.594 rows=10 loops=1)160 -> Sort (cost=11583.61..11804.76 rows=88459 width=32) (actual time=1431.589..1431.591 rows=10 loops=1)161 Sort Key: ((word <-> 'caterpiler'::text))162 Sort Method: top-N heapsort Memory: 25kB163 -> Foreign Scan on words (cost=0.00..9672.05 rows=88459 width=32) (actual time=0.057..1286.455 rows=479829 loops=1)164 Foreign File: /usr/share/dict/words165 Foreign File Size: 4953699166 Planning time: 0.128 ms167 Execution time: 1431.679 ms168</pre><p>169 170 Using the materialized view:171 172</p><pre class="programlisting">173 Limit (cost=0.29..1.06 rows=10 width=10) (actual time=187.222..188.257 rows=10 loops=1)174 -> Index Scan using wrd_trgm on wrd (cost=0.29..37020.87 rows=479829 width=10) (actual time=187.219..188.252 rows=10 loops=1)175 Order By: (word <-> 'caterpiler'::text)176 Planning time: 0.196 ms177 Execution time: 198.640 ms178</pre><p>179 180 If you can tolerate periodic update of the remote data to the local181 database, the performance benefit can be substantial.182</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rules-views.html" title="41.2. Views and the Rule System">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="rules.html" title="Chapter 41. The Rule System">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="rules-update.html" title="41.4. Rules on INSERT, UPDATE, and DELETE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">41.2. Views and the Rule System </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 41.4. Rules on <code class="command">INSERT</code>, <code class="command">UPDATE</code>, and <code class="command">DELETE</code></td></tr></table></div></body></html>