PostgresSQL - Materialized Views

1. Principle

Cache query results to greatly improve view query speed

2. Disadvantages

The results need to be regularly updated to the cache

3. Create

3.1 After creating the materialized view, you can query the data

create materialized view view_name as query_sql;

3.2 After creating a materialized view, the view cannot be queried (because there is no data)

create materialized view view_name as query_sql with no data;

4. Refresh data

4.1. Direct Refresh

refresh materialized view view_name;

4.2, compare and update

Principle: A temporary updated version of the materialized view will be created, the updated version and the original view will be compared, and insert and update will be executed

Requirements:

  • Materialized views must have unique indexes . Create an index statement as follows:
create unique index index_name on view_name (列名1,列名2...);
  • It can only be used in PostgreSQL9.4 and above .

Refresh statement:

refresh materialized view concurrently view_name;

5. Delete

drop materialized view if exists view_name;

おすすめ

転載: blog.csdn.net/hutuyaoniexi/article/details/128038802