Skip to content

Commit 70a2612

Browse files
committed
Add materialized views section to performance guide
- Introduced a new section on materialized views, explaining their purpose and benefits for optimizing analytical queries. - Added an example SQL statement for creating a materialized view to compute total sales per store. - Provided guidance on refreshing materialized views manually or via external scripts.
1 parent cb2df47 commit 70a2612

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

examples/official-site/sqlpage/migrations/69_blog_performance_guide.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ inner join store
7070
7171
#### Materialized views
7272
73+
Some analytical queries just have to compute aggregated statistics over large quantities of data.
74+
For instance, you might want to compute the total sales per store, or the total sales per product.
75+
These queries are slow to compute when there are many rows, and you might not want to run them on every request.
76+
You can use [materialized views](https://en.wikipedia.org/wiki/Materialized_view) to cache the results of these queries.
77+
Materialized views are views that are stored as regular tables in the database.
78+
79+
Depending on the database, you might have to refresh the materialized view manually.
80+
You can either refresh the view manually from inside your sql pages when you detect they are outdated,
81+
or write an external script to refresh the view periodically.
82+
83+
```sql
84+
create materialized view total_sales_per_store as
85+
select store_name, sum(sales_eur) as total_sales
86+
from sales
87+
group by store_name;
88+
```
7389
7490
### Use database indices
7591

0 commit comments

Comments
 (0)