# Materialized views

Store a query result and refresh it when the source changes.

Source: https://particledb.ai/sql/materialized-views/

Store a query result and refresh it when the source changes.

## Example

```sql
CREATE TABLE docs_sales (id BIGINT PRIMARY KEY, amount BIGINT);
INSERT INTO docs_sales VALUES (1, 10), (2, 20);
CREATE MATERIALIZED VIEW docs_sales_total AS SELECT SUM(amount) AS total FROM docs_sales;
```

```sql
SELECT total FROM docs_sales_total;
INSERT INTO docs_sales VALUES (3, 30);
REFRESH MATERIALIZED VIEW docs_sales_total;
SELECT total FROM docs_sales_total;
```

## Behavior

The first total is 30 and the refreshed total is 60. A materialized view is not automatically current with every write. Choose a refresh policy that matches the application and test failure behavior. Ordinary views and materialized views have different storage and refresh behavior.

## Related

[SQL reference](/sql/overview/) · [Application guide](/guides/relational-data/) · [Troubleshooting](/guides/troubleshooting/)
