Skip to content

Commit 3901ad3

Browse files
authored
improve discoverability of materialized views (#206)
1 parent 42995b1 commit 3901ad3

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

documentation/operations/design-for-performance.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@ considerations.
2020
The following section describes the underlying aspects to consider when
2121
formulating queries.
2222

23+
### Materialized views
24+
25+
[Materialized views](/docs/guides/mat-views/) are a powerful feature for optimizing query performance, especially for
26+
complex aggregate queries. They pre-compute and store query results, providing significant performance improvements for
27+
frequently executed queries.
28+
29+
Consider using materialized views when:
30+
31+
- You have complex aggregate queries with `SAMPLE BY`, `GROUP BY`, or window functions that are executed frequently
32+
- Query latency is critical and you can accept slightly stale data
33+
- You need to optimize dashboard queries or reporting workloads
34+
35+
Example of creating a materialized view for a common aggregate query:
36+
37+
```questdb-sql
38+
CREATE MATERIALIZED VIEW hourly_metrics AS
39+
SELECT
40+
timestamp_floor('h', timestamp) as hour,
41+
symbol,
42+
avg(price) as avg_price,
43+
sum(volume) as total_volume,
44+
count() as trade_count
45+
FROM trades
46+
SAMPLE BY 1h;
47+
```
48+
49+
This view automatically updates as new rows are added to the `trades` table. Queries to `hourly_metrics` are fast because
50+
they access these pre-computed results instead of scanning the full trades table.
51+
52+
For more details on implementing and managing materialized views, see
53+
the [materialized views guide](/docs/guides/mat-views/).
54+
2355
### Row serialization
2456

2557
Row serialization and deserialization has a cost on both client and server. The

documentation/reference/sql/overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ And to learn about some of our favourite, most powerful syntax:
428428
multiple series within a table
429429
- [`ASOF JOIN`](/docs/reference/sql/asof-join/) to associate timestamps between
430430
a series based on proximity; no extra indices required
431+
- [Materialized Views](/docs/guides/mat-views/) to pre-compute complex queries
432+
for optimal performance
431433

432434
Looking for visuals?
433435

documentation/reference/sql/sample-by.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,25 @@ ALIGN TO CALENDAR;
573573
| 2021-05-31T00:00:00.000000Z | 1000.5 |
574574
| 2021-06-01T00:00:00.000000Z | 8007.2 |
575575

576+
## Performance optimization
577+
578+
For frequently executed `SAMPLE BY` queries, consider using [materialized views](/docs/guides/mat-views/) to pre-compute aggregates. This can significantly improve query performance, especially for complex sampling operations on large datasets.
579+
580+
```questdb-sql
581+
CREATE MATERIALIZED VIEW hourly_metrics AS
582+
SELECT
583+
timestamp_floor('h', ts) as hour,
584+
symbol,
585+
avg(price) as avg_price,
586+
sum(volume) as total_volume
587+
FROM trades
588+
SAMPLE BY 1h;
589+
```
590+
576591
## See also
577592

578593
This section includes links to additional information such as tutorials:
579594

595+
- [Materialized Views Guide](/docs/guides/mat-views/) - Pre-compute SAMPLE BY queries for better performance
580596
- [SQL Extensions for Time-Series Data in QuestDB](/blog/2022/11/23/sql-extensions-time-series-data-questdb-part-ii/)
581-
582597
- [Three SQL Keywords for Finding Missing Data](/blog/three-sql-keywords-for-finding-missing-data/)

documentation/why-questdb.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Greatest hits include:
5858
multiple series within a table
5959
- [`ASOF JOIN`](/docs/reference/sql/asof-join/) to associate timestamps between
6060
a series based on proximity; no extra indices required
61+
- [Materialized Views](/docs/guides/mat-views/) for pre-computing and automatically
62+
refreshing complex query results to optimize performance
6163

6264
## Benefits of QuestDB {#benefits}
6365

0 commit comments

Comments
 (0)