Materialized view: Difference between revisions
need ref |
→External links: Added content Tags: Mobile edit Mobile web edit |
||
(15 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|In databases, cached query results}} |
|||
{{Use dmy dates|date=July 2021}} |
{{Use dmy dates|date=July 2021}} |
||
In [[computing]], a '''materialized view''' is a [[database]] object that contains the results of a [[Query (databases)|query]]. For example, it may be a local copy of data located remotely, or may be a subset of the rows and/or columns of a table or [[Join (SQL)|join]] result, or may be a summary using an [[aggregate function]]. |
In [[computing]], a '''materialized view''' is a [[database]] object that contains the results of a [[Query (databases)|query]]. For example, it may be a local copy of data located remotely, or may be a subset of the rows and/or columns of a table or [[Join (SQL)|join]] result, or may be a summary using an [[aggregate function]]. |
||
Line 4: | Line 5: | ||
The process of setting up a materialized view is sometimes called '''materialization'''.<ref name="Date2006">Compare: {{cite book|author= C.J. Date|title= The Relational Database Dictionary: A Comprehensive Glossary of Relational Terms and Concepts, with Illustrative Examples|url= https://books.google.com/books?id=vWKClUCN2HYC&pg=PA59|date= 28 August 2006|publisher= "O'Reilly Media, Inc."|isbn= 978-1-4493-9115-7|page= 59 | access-date = 2016-10-26 |quote= materialization[:] A somewhat unsophisticated technique for implementing operations on views according to which (a) the relational expression that defines the view is evaluated at the time the operation is invoked, (b) the view is thereby materialized, and (c) the operation in question is then executed against the relation so materialized.}}</ref> This is a form of [[Cache (computing)|caching]] the results of a query, similar to [[memoization]] of the value of a function in functional languages, and it is sometimes described as a form of [[precomputation]].<ref name="MortonOsborne2013">{{cite book|author1= Karen Morton|author2= Kerry Osborne|author3= Robyn Sands |author4= Riyaj Shamsudeen |author5= Jared Still|title= Pro Oracle SQL|url= https://books.google.com/books?id=XUPXAQAAQBAJ&pg=PA48|date= 28 October 2013|publisher= Apress|isbn=978-1-4302-6220-6|page= 48}}</ref><ref name="AufaureZimányi2012">{{cite book|author1= Marie-Aude Aufaure|author2= Esteban Zimányi|title= Business Intelligence: First European Summer School, EBISS 2011, Paris, France, July 3-8, 2011, Tutorial Lectures|url= https://books.google.com/books?id=UWtes499ZaUC&pg=PA43|date= 16 January 2012|publisher= Springer Science & Business Media|isbn= 978-3-642-27357-5|page= 43}}</ref> As with other forms of precomputation, database users typically use materialized views for performance reasons, i.e. as a form of optimization.<ref name="Gonzales2003">{{cite book|author= Michael L. Gonzales|title= IBM Data Warehousing: with IBM Business Intelligence Tools|url= https://books.google.com/books?id=7ZepRMDSsf8C&pg=PA214|date= 25 February 2003|publisher= John Wiley & Sons|isbn= 978-0-471-45736-7|page= 214}}</ref> |
The process of setting up a materialized view is sometimes called '''materialization'''.<ref name="Date2006">Compare: {{cite book|author= C.J. Date|title= The Relational Database Dictionary: A Comprehensive Glossary of Relational Terms and Concepts, with Illustrative Examples|url= https://books.google.com/books?id=vWKClUCN2HYC&pg=PA59|date= 28 August 2006|publisher= "O'Reilly Media, Inc."|isbn= 978-1-4493-9115-7|page= 59 | access-date = 2016-10-26 |quote= materialization[:] A somewhat unsophisticated technique for implementing operations on views according to which (a) the relational expression that defines the view is evaluated at the time the operation is invoked, (b) the view is thereby materialized, and (c) the operation in question is then executed against the relation so materialized.}}</ref> This is a form of [[Cache (computing)|caching]] the results of a query, similar to [[memoization]] of the value of a function in functional languages, and it is sometimes described as a form of [[precomputation]].<ref name="MortonOsborne2013">{{cite book|author1= Karen Morton|author2= Kerry Osborne|author3= Robyn Sands |author4= Riyaj Shamsudeen |author5= Jared Still|title= Pro Oracle SQL|url= https://books.google.com/books?id=XUPXAQAAQBAJ&pg=PA48|date= 28 October 2013|publisher= Apress|isbn=978-1-4302-6220-6|page= 48}}</ref><ref name="AufaureZimányi2012">{{cite book|author1= Marie-Aude Aufaure|author2= Esteban Zimányi|title= Business Intelligence: First European Summer School, EBISS 2011, Paris, France, July 3-8, 2011, Tutorial Lectures|url= https://books.google.com/books?id=UWtes499ZaUC&pg=PA43|date= 16 January 2012|publisher= Springer Science & Business Media|isbn= 978-3-642-27357-5|page= 43}}</ref> As with other forms of precomputation, database users typically use materialized views for performance reasons, i.e. as a form of optimization.<ref name="Gonzales2003">{{cite book|author= Michael L. Gonzales|title= IBM Data Warehousing: with IBM Business Intelligence Tools|url= https://books.google.com/books?id=7ZepRMDSsf8C&pg=PA214|date= 25 February 2003|publisher= John Wiley & Sons|isbn= 978-0-471-45736-7|page= 214}}</ref> |
||
Materialized views |
Materialized views that store data based on remote tables were also known as [[Snapshot (computer storage)#In databases|snapshot]]s<ref> {{cite book|author= C.J. Date|title= The Relational Database Dictionary: A Comprehensive Glossary of Relational Terms and Concepts, with Illustrative Examples|url= https://books.google.com/books?id=vWKClUCN2HYC&pg=PA59|date= 28 August 2006|publisher= "O'Reilly Media, Inc."|isbn= 978-1-4493-9115-7|page= 59 | access-date = 2016-10-26 | quote = materialized view[:] Deprecated term for a snapshot. [...] The problem is [...] that (as the definition indicates) snapshots have come to be known, at least in some circles, not as snapshots at all but as materialized views. But snapshots aren't views; views are virtual and snapshots aren't, and 'materialized view' is a contradiction in terms (at least as far as the model is concerned). Worse yet, the unqualified term ''view'' is often taken to mean a materialized view specifically, and thus we're in danger of no longer having a good term for a view in the original sense.}}</ref> (deprecated [[Oracle Database|Oracle]] terminology). |
||
In any [[database management system]] following the [[relational model]], a [[view (database)|view]] is a virtual [[table (database)|table]] representing the result of a [[database]] [[Information retrieval|query]]. Whenever a query or an update addresses an ordinary view's virtual table, the DBMS converts these into queries or updates against the underlying base tables. A materialized view takes a different approach: the query result is [[database cache|cached]] as a concrete ("materialized") table (rather than a view as such) that may be updated from the original base tables from time to time. This enables much more efficient access, at the cost of extra storage and of some data being potentially out-of-date. Materialized views find use especially in [[data warehousing]] scenarios, where frequent queries of the actual base tables can be expensive.{{fact}} |
In any [[database management system]] following the [[relational model]], a [[view (database)|view]] is a virtual [[table (database)|table]] representing the result of a [[database]] [[Information retrieval|query]]. Whenever a query or an update addresses an ordinary view's virtual table, the DBMS converts these into queries or updates against the underlying base tables. A materialized view takes a different approach: the query result is [[database cache|cached]] as a concrete ("materialized") table (rather than a view as such) that may be updated from the original base tables from time to time. This enables much more efficient access, at the cost of extra storage and of some data being potentially out-of-date. Materialized views find use especially in [[data warehousing]] scenarios, where frequent queries of the actual base tables can be expensive.{{fact|date=July 2021}} |
||
In a materialized view, [[index (database)|indexes]] can be built on any column. In contrast, in a normal view, it's typically only possible to exploit indexes on columns that come directly from (or have a mapping to) indexed columns in the base tables; often this functionality is not offered at all. |
In a materialized view, [[index (database)|indexes]] can be built on any column. In contrast, in a normal view, it's typically only possible to exploit indexes on columns that come directly from (or have a mapping to) indexed columns in the base tables; often this functionality is not offered at all. |
||
Line 24: | Line 25: | ||
===PostgreSQL=== |
===PostgreSQL=== |
||
In [[PostgreSQL]], version 9.3 and newer natively support materialized views.<ref> |
In [[PostgreSQL]], version 9.3 and newer natively support materialized views.<ref>{{Cite web |title=Materialized Views - PostgreSQL wiki |url=https://wiki.postgresql.org/wiki/Materialized_Views |access-date=2022-11-29 |website=wiki.postgresql.org}}</ref> In version 9.3, a materialized view is not auto-refreshed, and is populated only at time of creation (unless <code>WITH NO DATA</code> is used). It may be refreshed later manually using <code>REFRESH MATERIALIZED VIEW</code>.<ref>{{Cite web |date=2022-11-10 |title=CREATE MATERIALIZED VIEW |url=https://www.postgresql.org/docs/15/sql-creatematerializedview.html |access-date=2022-11-29 |website=PostgreSQL Documentation |language=en}}</ref> In version 9.4, the refresh may be concurrent with selects on the materialized view if <code>CONCURRENTLY</code> is used.<ref>{{Cite web |date=2020-02-13 |title=REFRESH MATERIALIZED VIEW |url=https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html |access-date=2022-11-29 |website=PostgreSQL Documentation |language=en}}</ref> |
||
Example syntax to create a materialized view in PostgreSQL: |
Example syntax to create a materialized view in PostgreSQL: |
||
Line 35: | Line 36: | ||
===SQL Server=== |
===SQL Server=== |
||
Microsoft SQL Server differs from other RDBMS by the way of implementing materialized view via a concept known as "Indexed Views". The main difference is that such views do not require a refresh because they are in fact always synchronized to the original data of the tables that compound the view. To achieve this, it is necessary that the lines of origin and destination are "deterministic" in their mapping which limits the types of possible queries to do this. This mechanism has been realised since the 2000 version of SQL Server. |
Microsoft SQL Server differs from other RDBMS by the way of implementing materialized view via a concept known as "Indexed Views". The main difference is that such views do not require a refresh because they are in fact always synchronized to the original data of the tables that compound the view. To achieve this, it is necessary that the lines of origin and destination are "deterministic" in their mapping, which limits the types of possible queries to do this. This mechanism has been realised since the 2000 version of SQL Server. |
||
Example syntax to create a materialized view in SQL Server: |
Example syntax to create a materialized view in SQL Server: |
||
Line 51: | Line 52: | ||
===Stream processing frameworks=== |
===Stream processing frameworks=== |
||
[[Apache Kafka]] (since v0.10.2), [[Apache Spark]] (since v2.0), |
[[Apache Kafka]] (since v0.10.2), [[Apache Spark]] (since v2.0), Apache Flink, [[Kinetica_(software)|Kinetica DB]],<ref>{{cite web |title=Materialized Views |url=https://docs.kinetica.com/7.1/concepts/materialized_views/ |access-date=28 December 2022}}</ref> Materialize,<ref>{{cite web |title=CMU DB Talk: Building Materialize |url=https://materialize.com/blog-cmudb/ |access-date=30 March 2022}}</ref> and RisingWave<ref>{{Cite web |date=2022-04-28 |title=Is RisingWave the Next Apache Flink? |url=https://www.singularity-data.com/blog/is-risingwave-the-next-apache-flink |access-date=2022-06-30 |website=www.singularity-data.com |language=en}}</ref> all support materialized views on streams of data. |
||
===Others=== |
===Others=== |
||
Materialized views are also supported in [[Sybase]] [[SQL Anywhere]].<ref>[http://www.ianywhere.com/developer/product_manuals/sqlanywhere/1000/en/html/dbugen10/ug-workingwdb-s-3142433.html Materialized Views – Sybase SQL Anywhere] {{Webarchive|url=https://web.archive.org/web/20091214083022/http://www.ianywhere.com/developer/product_manuals/sqlanywhere/1000/en/html/dbugen10/ug-workingwdb-s-3142433.html |date=2009-12-14 }}. Ianywhere.com. Retrieved on 2012-02-09.</ref> In [[IBM |
Materialized views are also supported in [[Sybase]] [[SQL Anywhere]].<ref>[http://www.ianywhere.com/developer/product_manuals/sqlanywhere/1000/en/html/dbugen10/ug-workingwdb-s-3142433.html Materialized Views – Sybase SQL Anywhere] {{Webarchive|url=https://web.archive.org/web/20091214083022/http://www.ianywhere.com/developer/product_manuals/sqlanywhere/1000/en/html/dbugen10/ug-workingwdb-s-3142433.html |date=2009-12-14 }}. Ianywhere.com. Retrieved on 2012-02-09.</ref> In [[IBM Db2]], they are called "materialized query tables".<ref>[http://www.microsoft.com/technet/prodtechnol/sql/2005/impprfiv.mspx Improving Performance with SQL Server 2005 Indexed Views]. Microsoft.com. Retrieved on 2012-02-09.</ref> [[ClickHouse]] supports materialized views that automatically refresh on merges. <ref>[https://clickhouse.yandex/docs/en/operations/table_engines/materializedview/ ClickHouse Documentation MaterializedView]. Clickhouse.yandex. Retrieved on 2019-09-05.</ref> [[MySQL]] doesn't support materialized views natively, but workarounds can be implemented by using triggers or stored procedures <ref>[http://www.shinguz.ch/MySQL/mysql_mv.html Implementing materialized views in MySQL]. Shinguz.ch (2006-11-06). Retrieved on 2012-02-09.</ref> or by using the open-source application [[Flexviews]].<ref>[http://flexviews.sourceforge.net/index.html Flexviews for MySQL – incrementally refreshable materialized views w/ MySQL]. Flexviews.sourceforge.net. Retrieved on 2012-02-09.</ref> Materialized views can be implemented in [[Amazon DynamoDB]] using data modification events captured by DynamoDB Streams. |
||
Google announced in 8 April 2020<ref>{{cite web | url=https://cloud.google.com/bigquery/docs/release-notes#April_08_2020 | title=Release notes | date=8 April 2020 | publisher=Google.com | accessdate=21 July 2021}}</ref> the availability of materialized views for BigQuery<ref>[https://cloud.google.com/bigquery/docs/materialized-views-intro#monitoring_materialized_views Google BigQuery Materialized Views documentation] Google.com Retrieved on 2020-05-20.</ref> as a beta release. |
Google announced in 8 April 2020<ref>{{cite web | url=https://cloud.google.com/bigquery/docs/release-notes#April_08_2020 | title=Release notes | date=8 April 2020 | publisher=Google.com | accessdate=21 July 2021}}</ref> the availability of materialized views for BigQuery<ref>[https://cloud.google.com/bigquery/docs/materialized-views-intro#monitoring_materialized_views Google BigQuery Materialized Views documentation] Google.com Retrieved on 2020-05-20.</ref> as a beta release. |
||
Line 65: | Line 66: | ||
*[http://download.oracle.com/docs/cd/B10501_01/server.920/a96568/rarmviea.htm#94135 Oracle9i Replication Management API Reference Release 2 (9.2)] |
*[http://download.oracle.com/docs/cd/B10501_01/server.920/a96568/rarmviea.htm#94135 Oracle9i Replication Management API Reference Release 2 (9.2)] |
||
*[https://web.archive.org/web/20110303073052/http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6002.htm#SQLRF01302 Materialized Views in Oracle 11.2] |
*[https://web.archive.org/web/20110303073052/http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6002.htm#SQLRF01302 Materialized Views in Oracle 11.2] |
||
*[https://archive.today/20130103084125/http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2z10.doc.intro/src/tpc/db2z_typesoftables.htm Materialized query tables in |
*[https://archive.today/20130103084125/http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2z10.doc.intro/src/tpc/db2z_typesoftables.htm Materialized query tables in Db2] |
||
*[http://www.coding-dude.com/wp/databases/creating-mysql-materialized-views/ Creating Materialized Views In MySQL] |
*[http://www.coding-dude.com/wp/databases/creating-mysql-materialized-views/ Creating Materialized Views In MySQL] |
||
*[https://datamaster.cloud/the-journey-of-data-optimization-a-story-of-materialized-views-in-e-commerce/ Optimizing Data with Materialized Views: An E-Commerce Story] |
|||
{{Databases}} |
{{Databases}} |
Latest revision as of 04:06, 17 October 2024
In computing, a materialized view is a database object that contains the results of a query. For example, it may be a local copy of data located remotely, or may be a subset of the rows and/or columns of a table or join result, or may be a summary using an aggregate function.
The process of setting up a materialized view is sometimes called materialization.[1] This is a form of caching the results of a query, similar to memoization of the value of a function in functional languages, and it is sometimes described as a form of precomputation.[2][3] As with other forms of precomputation, database users typically use materialized views for performance reasons, i.e. as a form of optimization.[4]
Materialized views that store data based on remote tables were also known as snapshots[5] (deprecated Oracle terminology).
In any database management system following the relational model, a view is a virtual table representing the result of a database query. Whenever a query or an update addresses an ordinary view's virtual table, the DBMS converts these into queries or updates against the underlying base tables. A materialized view takes a different approach: the query result is cached as a concrete ("materialized") table (rather than a view as such) that may be updated from the original base tables from time to time. This enables much more efficient access, at the cost of extra storage and of some data being potentially out-of-date. Materialized views find use especially in data warehousing scenarios, where frequent queries of the actual base tables can be expensive.[citation needed]
In a materialized view, indexes can be built on any column. In contrast, in a normal view, it's typically only possible to exploit indexes on columns that come directly from (or have a mapping to) indexed columns in the base tables; often this functionality is not offered at all.
Implementations
[edit]Oracle
[edit]Materialized views were implemented first by the Oracle Database: the Query rewrite feature was added from version 8i.[6]
Example syntax to create a materialized view in Oracle:
CREATE MATERIALIZED VIEW MV_MY_VIEW
REFRESH FAST START WITH SYSDATE
NEXT SYSDATE + 1
AS SELECT * FROM <table_name>;
PostgreSQL
[edit]In PostgreSQL, version 9.3 and newer natively support materialized views.[7] In version 9.3, a materialized view is not auto-refreshed, and is populated only at time of creation (unless WITH NO DATA
is used). It may be refreshed later manually using REFRESH MATERIALIZED VIEW
.[8] In version 9.4, the refresh may be concurrent with selects on the materialized view if CONCURRENTLY
is used.[9]
Example syntax to create a materialized view in PostgreSQL:
CREATE MATERIALIZED VIEW MV_MY_VIEW
[ WITH (storage_parameter [= value] [, ... ]) ]
[ TABLESPACE tablespace_name ]
AS SELECT * FROM <table_name>;
SQL Server
[edit]Microsoft SQL Server differs from other RDBMS by the way of implementing materialized view via a concept known as "Indexed Views". The main difference is that such views do not require a refresh because they are in fact always synchronized to the original data of the tables that compound the view. To achieve this, it is necessary that the lines of origin and destination are "deterministic" in their mapping, which limits the types of possible queries to do this. This mechanism has been realised since the 2000 version of SQL Server.
Example syntax to create a materialized view in SQL Server:
CREATE VIEW MV_MY_VIEW
WITH SCHEMABINDING
AS
SELECT COL1, SUM(COL2) AS TOTAL
FROM <table_name>
GROUP BY COL1;
GO
CREATE UNIQUE CLUSTERED INDEX XV
ON MV_MY_VIEW (COL1);
Stream processing frameworks
[edit]Apache Kafka (since v0.10.2), Apache Spark (since v2.0), Apache Flink, Kinetica DB,[10] Materialize,[11] and RisingWave[12] all support materialized views on streams of data.
Others
[edit]Materialized views are also supported in Sybase SQL Anywhere.[13] In IBM Db2, they are called "materialized query tables".[14] ClickHouse supports materialized views that automatically refresh on merges. [15] MySQL doesn't support materialized views natively, but workarounds can be implemented by using triggers or stored procedures [16] or by using the open-source application Flexviews.[17] Materialized views can be implemented in Amazon DynamoDB using data modification events captured by DynamoDB Streams. Google announced in 8 April 2020[18] the availability of materialized views for BigQuery[19] as a beta release.
References
[edit]- ^ Compare: C.J. Date (28 August 2006). The Relational Database Dictionary: A Comprehensive Glossary of Relational Terms and Concepts, with Illustrative Examples. "O'Reilly Media, Inc.". p. 59. ISBN 978-1-4493-9115-7. Retrieved 26 October 2016.
materialization[:] A somewhat unsophisticated technique for implementing operations on views according to which (a) the relational expression that defines the view is evaluated at the time the operation is invoked, (b) the view is thereby materialized, and (c) the operation in question is then executed against the relation so materialized.
- ^ Karen Morton; Kerry Osborne; Robyn Sands; Riyaj Shamsudeen; Jared Still (28 October 2013). Pro Oracle SQL. Apress. p. 48. ISBN 978-1-4302-6220-6.
- ^ Marie-Aude Aufaure; Esteban Zimányi (16 January 2012). Business Intelligence: First European Summer School, EBISS 2011, Paris, France, July 3-8, 2011, Tutorial Lectures. Springer Science & Business Media. p. 43. ISBN 978-3-642-27357-5.
- ^ Michael L. Gonzales (25 February 2003). IBM Data Warehousing: with IBM Business Intelligence Tools. John Wiley & Sons. p. 214. ISBN 978-0-471-45736-7.
- ^ C.J. Date (28 August 2006). The Relational Database Dictionary: A Comprehensive Glossary of Relational Terms and Concepts, with Illustrative Examples. "O'Reilly Media, Inc.". p. 59. ISBN 978-1-4493-9115-7. Retrieved 26 October 2016.
materialized view[:] Deprecated term for a snapshot. [...] The problem is [...] that (as the definition indicates) snapshots have come to be known, at least in some circles, not as snapshots at all but as materialized views. But snapshots aren't views; views are virtual and snapshots aren't, and 'materialized view' is a contradiction in terms (at least as far as the model is concerned). Worse yet, the unqualified term view is often taken to mean a materialized view specifically, and thus we're in danger of no longer having a good term for a view in the original sense.
- ^ Oracle8i Tuning Release 8.1.5. Ecst.csuchico.edu. Retrieved on 2012-02-09.
- ^ "Materialized Views - PostgreSQL wiki". wiki.postgresql.org. Retrieved 29 November 2022.
- ^ "CREATE MATERIALIZED VIEW". PostgreSQL Documentation. 10 November 2022. Retrieved 29 November 2022.
- ^ "REFRESH MATERIALIZED VIEW". PostgreSQL Documentation. 13 February 2020. Retrieved 29 November 2022.
- ^ "Materialized Views". Retrieved 28 December 2022.
- ^ "CMU DB Talk: Building Materialize". Retrieved 30 March 2022.
- ^ "Is RisingWave the Next Apache Flink?". www.singularity-data.com. 28 April 2022. Retrieved 30 June 2022.
- ^ Materialized Views – Sybase SQL Anywhere Archived 2009-12-14 at the Wayback Machine. Ianywhere.com. Retrieved on 2012-02-09.
- ^ Improving Performance with SQL Server 2005 Indexed Views. Microsoft.com. Retrieved on 2012-02-09.
- ^ ClickHouse Documentation MaterializedView. Clickhouse.yandex. Retrieved on 2019-09-05.
- ^ Implementing materialized views in MySQL. Shinguz.ch (2006-11-06). Retrieved on 2012-02-09.
- ^ Flexviews for MySQL – incrementally refreshable materialized views w/ MySQL. Flexviews.sourceforge.net. Retrieved on 2012-02-09.
- ^ "Release notes". Google.com. 8 April 2020. Retrieved 21 July 2021.
- ^ Google BigQuery Materialized Views documentation Google.com Retrieved on 2020-05-20.