选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

SQL Server Indexed Views and PostgreSQL Materialized Views

聚焦模式
SQL Server Indexed Views and PostgreSQL Materialized Views - SQL Server to Aurora PostgreSQL Migration Playbook
此页面尚未翻译为您的语言。 请求翻译
Feature compatibility AWS SCT / AWS DMS automation level AWS SCT action code index Key differences

Two star feature compatibility

No automation

N/A

Different paradigm and syntax will require rewriting the application.

SQL Server Usage

The first index created on a view must be a clustered index. Subsequent indexes can be non-clustered indexes. For more information, see Clustered and nonclustered indexes described in the SQL Server documentation.

Before creating an index on a view, the following requirements must be met:

  • The WITH SCHEMABINDING option must be used when creating the view.

  • Verify the SET options are correct for all existing tables referenced in the view and for the session. Find the link at the end of this section for required values.

  • Ensure that a clustered index on the view is exists.

Note

You can’t use indexed views with temporal queries (FOR SYSTEM_TIME).

Examples

Set the required SET options, create a view with the WITH SCHEMABINDING option, and create an index on this view.

SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
  QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO

CREATE VIEW Sales.Ord_view
WITH SCHEMABINDING
AS
  SELECT SUM(Price*Qty*(1.00-Discount)) AS Revenue,
    OrdTime, ID, COUNT_BIG(*) AS COUNT
  FROM Sales.OrderDetail AS ordet, Sales.OrderHeader AS ordhead
  WHERE ordet.SalesOrderID = ordhead.SalesOrderID
  GROUP BY OrdTime, ID;
GO

CREATE UNIQUE CLUSTERED INDEX IDX_V1
  ON Sales.Ord_view (OrdTime, ID);
GO

For more information, see Create Indexed Views in the SQL Server documentation.

PostgreSQL Usage

PostgreSQL doesn’t support indexed views, but does provide similar functionality with materialized views. You can run queries associated with materialized views, and populate the view data with the REFRESH command.

The PostgreSQL implementation of materialized views has three primary limitations:

  • You can refresh PostgreSQL materialized views either manually or using a job running the REFRESH MATERIALIZED VIEW command. To refresh materialized views automatically, create a trigger.

  • PostgreSQL materialized views only support complete or full refresh.

  • DML on materialized views isn’t supported.

In some cases, when the tables are big, full REFRESH can cause performance issues. In this case, you can use triggers to sync between one table to the new table. You can use the new table as an indexed view.

Examples

The following example creates a materialized view named sales_summary using the sales table as the source.

CREATE MATERIALIZED VIEW sales_summary AS
SELECT seller_no,sale_date,sum(sale_amt)::numeric(10,2) as sales_amt
FROM sales
WHERE sale_date < CURRENT_DATE
GROUP BY seller_no, sale_date
ORDER BY seller_no, sale_date;

The following example runs a manual refresh of the materialized view:

REFRESH MATERIALIZED VIEW sales_summary;
Note

The materialized view data isn’t refreshed automatically if changes occur to its underlying tables. For automatic refresh of materialized view data, a trigger on the underlying tables must be created.

Creating a Materialized View

When you create a materialized view in PostgreSQL, it uses a regular database table underneath. You can create database indexes on the materialized view directly and improve performance of queries that access the materialized view.

Example

The following example creates an index on the sellerno and sale_date columns of the sales_summary materialized view.

CREATE UNIQUE INDEX sales_summary_seller
ON sales_summary (seller_no, sale_date);

Summary

Feature Indexed views Materialized view

Create materialized view

SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL,
  ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO

CREATE VIEW Sales.Ord_view WITH SCHEMABINDING
  AS SELECT SUM(Price*Qty*(1.00-Discount)) AS Revenue,
  OrdTime, ID, COUNT_BIG(*) AS
    COUNT FROM Sales.OrderDetail AS ordet,
  Sales.OrderHeader AS ordhead
    WHERE ordet.SalesOrderID = ordhead.SalesOrderID
    GROUP BY OrdTime, ID;
GO

CREATE UNIQUE CLUSTERED INDEX IDX_V1 ON Sales.Ord_view (OrdTime, ID);
GO
CREATE MATERIALIZED VIEW mv1 AS SELECT * FROM employees;

Indexed refreshed

Automatic

Manual. You can automate refreshes using triggers.

Create a trigger that initiates a refresh after every DML command on the underlying tables:

CREATE OR REPLACE FUNCTION
refresh_mv1()
returns trigger language plpgsql as
$$ begin
refresh materialized view mv1;
return null;
end $$;

Create the refresh_mv1 trigger after insert, update, delete, or truncate on employees. For each statement, run the refresh_mv1(); procedure.

DML

Supported

Not Supported

For more information, see Materialized Views in the PostgreSQL documentation.

本页内容

隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。