Skip to content

Commit 7620e4c

Browse files
authored
Remove PARTITION BY from mat view examples (#188)
1 parent 525ecf8 commit 7620e4c

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed

documentation/concept/mat-views.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ To create a materialize view, surround your `SAMPLE BY` or time-based `GROUP BY`
7474
query with a [`CREATE MATERIALIZED VIEW`](/docs/reference/sql/create-mat-view) statement.
7575

7676
```questdb-sql title="trades_notional_1m ddl"
77-
CREATE MATERIALIZED VIEW 'trades_notional_1m' AS (
78-
SELECT
79-
timestamp,
80-
symbol,
81-
side,
82-
sum(price * amount) AS notional
83-
FROM trades
84-
SAMPLE BY 1m
85-
) PARTITION BY DAY;
77+
CREATE MATERIALIZED VIEW 'trades_notional_1m' AS
78+
SELECT
79+
timestamp,
80+
symbol,
81+
side,
82+
sum(price * amount) AS notional
83+
FROM trades
84+
SAMPLE BY 1m;
8685
```
8786

8887
Querying a materialized view can be up to hundreds of times faster than

documentation/guides/mat-views.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ If you are unfamiliar with the OHLC concept, please see our
104104

105105
:::
106106

107-
```questdb-sql title="trades_OHLC_15m ddl"
107+
```questdb-sql title="trades_OHLC_15m DDL"
108108
CREATE MATERIALIZED VIEW 'trades_OHLC_15m'
109109
WITH BASE 'trades' REFRESH INCREMENTAL
110110
AS (
@@ -137,12 +137,20 @@ In this example:
137137
- Therefore, the materialized view will contain a summary of _all_ the base
138138
`trades` table's data.
139139

140-
:::tip
140+
Many parts of the above DDL statement are optional and can be ommited:
141141

142-
This particular example can also be written via the
143-
[compact syntax](#compact-syntax).
144-
145-
:::
142+
```questdb-sql title="trades_OHLC_15m compact DDL"
143+
CREATE MATERIALIZED VIEW 'trades_OHLC_15m' AS
144+
SELECT
145+
timestamp, symbol,
146+
first(price) AS open,
147+
max(price) as high,
148+
min(price) as low,
149+
last(price) AS close,
150+
sum(amount) AS volume
151+
FROM trades
152+
SAMPLE BY 15m;
153+
```
146154

147155
#### The view name
148156

@@ -227,15 +235,15 @@ which omits the parentheses.
227235

228236
```questdb-sql title="trades_OHLC_15m compact syntax"
229237
CREATE MATERIALIZED VIEW trades_OHLC_15m AS
230-
SELECT
231-
timestamp, symbol,
232-
first(price) AS open,
233-
max(price) as high,
234-
min(price) as low,
235-
last(price) AS close,
236-
sum(amount) AS volume
237-
FROM trades
238-
SAMPLE BY 15m;
238+
SELECT
239+
timestamp, symbol,
240+
first(price) AS open,
241+
max(price) as high,
242+
min(price) as low,
243+
last(price) AS close,
244+
sum(amount) AS volume
245+
FROM trades
246+
SAMPLE BY 15m;
239247
```
240248

241249
## Querying materialized views
@@ -508,17 +516,16 @@ This result set comprises just `14595` rows, instead of ~767 million. That's
508516
Here it is as a materialized view:
509517

510518
```questdb-sql title="LATEST ON materialized view"
511-
CREATE MATERIALIZED VIEW 'trades_latest_1d' WITH BASE 'trades' REFRESH INCREMENTAL AS (
512-
SELECT
513-
timestamp,
514-
symbol,
515-
side,
516-
last(price) AS price,
517-
last(amount) AS amount,
518-
last(timestamp) as latest
519-
FROM trades
520-
SAMPLE BY 1d
521-
) PARTITION BY DAY;
519+
CREATE MATERIALIZED VIEW 'trades_latest_1d' AS
520+
SELECT
521+
timestamp,
522+
symbol,
523+
side,
524+
last(price) AS price,
525+
last(amount) AS amount,
526+
last(timestamp) as latest
527+
FROM trades
528+
SAMPLE BY 1d;
522529
```
523530

524531
You can try this view out on our demo:

documentation/reference/sql/create-mat-view.md

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ description:
77

88
:::info
99

10-
Materialized View support is now generally available (GA) and ready for production use.
10+
Materialized View support is now generally available (GA) and ready for
11+
production use.
1112

12-
If you are using versions earlier than `8.3.1`, we suggest you upgrade at your earliest convenience.
13+
If you are using versions earlier than `8.3.1`, we suggest you upgrade at your
14+
earliest convenience.
1315

1416
:::
1517

@@ -24,16 +26,19 @@ the [introduction](/docs/concept/mat-views/) and
2426

2527
## Syntax
2628

27-
To create a materialized view, manually enter the parameters and settings:
29+
The `CREATE MATERIALIZED VIEW` statement comes in two flavors: compact and full
30+
syntax. The compact syntax can be used when the default parameters are
31+
sufficient.
2832

29-
![Flow chart showing the syntax of the CREATE MATERIALIZED VIEW keyword](/images/docs/diagrams/createMatViewDef.svg)
33+
![Flow chart showing the syntax of the compact CREATE MATERIALIZED VIEW syntax](/images/docs/diagrams/createMatViewCompactDef.svg)
3034

31-
:::tip
35+
For more on the semantics of the compact syntax, see the
36+
[materialized view guide](/docs/guides/mat-views/#compact-syntax).
3237

33-
For simple materialized views, you can alternatively use the
34-
[compact syntax](#compact-syntax).
38+
To create a materialized view with full syntax, you need to enter the following
39+
parameters and settings:
3540

36-
:::
41+
![Flow chart showing the syntax of the CREATE MATERIALIZED VIEW keyword](/images/docs/diagrams/createMatViewDef.svg)
3742

3843
## Metadata
3944

@@ -63,14 +68,13 @@ Now we can create a materialized view holding aggregated data from the base
6368
table:
6469

6570
```questdb-sql title="Hourly materialized view"
66-
CREATE MATERIALIZED VIEW trades_hourly_prices AS (
67-
SELECT
68-
timestamp,
69-
symbol,
70-
avg(price) AS avg_price
71-
FROM trades
72-
SAMPLE BY 1h
73-
) PARTITION BY HOUR;
71+
CREATE MATERIALIZED VIEW trades_hourly_prices AS
72+
SELECT
73+
timestamp,
74+
symbol,
75+
avg(price) AS avg_price
76+
FROM trades
77+
SAMPLE BY 1h;
7478
```
7579

7680
Now, we've created a materialized view that will be automatically refreshed each
@@ -96,17 +100,15 @@ one of them as the base table.
96100

97101
```questdb-sql title="Hourly materialized view with LT JOIN"
98102
CREATE MATERIALIZED VIEW trades_ext_hourly_prices
99-
WITH BASE trades
100-
AS (
101-
SELECT
102-
t.timestamp,
103-
t.symbol,
104-
avg(t.price) AS avg_price,
105-
avg(e.price) AS avg_ext_price
106-
FROM trades t
107-
LT JOIN ext_trades e ON (symbol)
108-
SAMPLE BY 1d
109-
) PARTITION BY WEEK;
103+
WITH BASE trades AS
104+
SELECT
105+
t.timestamp,
106+
t.symbol,
107+
avg(t.price) AS avg_price,
108+
avg(e.price) AS avg_ext_price
109+
FROM trades t
110+
LT JOIN ext_trades e ON (symbol)
111+
SAMPLE BY 1d;
110112
```
111113

112114
## Partitioning
@@ -156,7 +158,7 @@ depending on your needs.
156158

157159
### Examples
158160

159-
```questdb-sql title="Creating a materialized view with TTL"
161+
```questdb-sql title="Creating a materialized view with PARTITION BY and TTL"
160162
CREATE MATERIALIZED VIEW trades_hourly_prices AS (
161163
SELECT
162164
timestamp,
@@ -174,14 +176,13 @@ An optional `IF NOT EXISTS` clause may be added directly after the
174176
created only if a view with the desired view name does not already exist.
175177

176178
```questdb-sql
177-
CREATE MATERIALIZED VIEW IF NOT EXISTS trades_weekly_prices AS (
178-
SELECT
179-
timestamp,
180-
symbol,
181-
avg(price) AS avg_price
182-
FROM trades
183-
SAMPLE BY 7d
184-
) PARTITION BY YEAR;
179+
CREATE MATERIALIZED VIEW IF NOT EXISTS trades_weekly_prices AS
180+
SELECT
181+
timestamp,
182+
symbol,
183+
avg(price) AS avg_price
184+
FROM trades
185+
SAMPLE BY 7d;
185186
```
186187

187188
## Materialized view names
@@ -214,26 +215,6 @@ CREATE MATERIALIZED VIEW trades_hourly_prices AS (
214215
OWNED BY analysts;
215216
```
216217

217-
## Compact syntax
218-
219-
The `CREATE MATERIALIZED VIEW` statement also supports a compact syntax which
220-
can be used when the default parameters are sufficient.
221-
222-
![Flow chart showing the syntax of the compact CREATE MATERIALIZED VIEW syntax](/images/docs/diagrams/createMatViewCompactDef.svg)
223-
224-
```questdb-sql
225-
CREATE MATERIALIZED VIEW trades_hourly_prices AS
226-
SELECT
227-
timestamp,
228-
symbol,
229-
avg(price) AS avg_price
230-
FROM trades
231-
SAMPLE BY 1h;
232-
```
233-
234-
For more on the semantics of the compact syntax, see the
235-
[materialized view guide](/docs/guides/mat-views/#compact-syntax).
236-
237218
## SYMBOL column capacity
238219

239220
By default, SYMBOL column capacities in a materialized view are set to the same

documentation/reference/sql/refresh-mat-view.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ incremental refresh of the materialized view. Usually, incremental refresh is
2828
automatic, so this command is useful only in niche situations when incremental
2929
refresh is not working as expected, but the view is still valid.
3030

31-
When the `INTERVAL` keyword is specified, this command refreshes the data in the
32-
specified time interval only. This command is useful for a valid materialized
31+
When the `RANGE` keyword is specified, this command refreshes the data in the
32+
specified time range only. This command is useful for a valid materialized
3333
view with configured
3434
[`REFRESH LIMIT`](/docs/reference/sql/alter-mat-view-set-refresh-limit/). That's
3535
because inserted base table rows with timestamps older than the refresh limit
36-
are ignored by incremental refresh, so interval refresh may be used to
37-
recalculate materialized view on older rows. Interval refresh does not affect
36+
are ignored by incremental refresh, so range refresh may be used to
37+
recalculate materialized view on older rows. Range refresh does not affect
3838
incremental refresh, e.g. it does not update the last base table transaction
3939
used by incremental refresh.
4040

@@ -53,7 +53,7 @@ REFRESH MATERIALIZED VIEW trades_1h INCREMENTAL;
5353
```
5454

5555
```questdb-sql
56-
REFRESH MATERIALIZED VIEW trades_1h INTERVAL FROM '2025-05-05T01:00:00.000000Z' TO '2025-05-05T02:00:00.000000Z';
56+
REFRESH MATERIALIZED VIEW trades_1h RANGE FROM '2025-05-05T01:00:00.000000Z' TO '2025-05-05T02:00:00.000000Z';
5757
```
5858

5959
## See also

0 commit comments

Comments
 (0)