You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Applications of partitioning {#applications-of-partionning}
47
+
## Applications of partitioning {#applications-of-partitioning}
48
48
49
49
Partitioning is a powerful tool for managing large datasets in ClickHouse, especially in observability and analytics use cases. It enables efficient data life cycle operations by allowing entire partitions, often aligned with time or business logic, to be dropped, moved, or archived in a single metadata operation. This is significantly faster and less resource-intensive than row-level delete or copy operations. Partitioning also integrates cleanly with ClickHouse features like TTL and tiered storage, making it possible to implement retention policies or hot/cold storage strategies without custom orchestration. For example, recent data can be kept on fast SSD-backed storage, while older partitions are automatically moved to cheaper object storage.
The following queries contrast performance with and without projections. To disable projection use we use the setting [`optimize_use_projections`](/operations/settings/settings#optimize_use_projections), which is enabled by default.
421
+
422
+
#### Query 1. Average price per year {#average-price-projections}
423
+
424
+
```sql runnable
425
+
SELECT
426
+
toYear(date) AS year,
427
+
round(avg(price)) AS price,
428
+
bar(price, 0, 1000000, 80)
429
+
FROMuk.uk_price_paid_with_projections_v2
430
+
GROUP BY year
431
+
ORDER BY year ASC
432
+
SETTINGS optimize_use_projections=0
433
+
```
434
+
435
+
```sql runnable
436
+
SELECT
437
+
toYear(date) AS year,
438
+
round(avg(price)) AS price,
439
+
bar(price, 0, 1000000, 80)
440
+
FROMuk.uk_price_paid_with_projections_v2
441
+
GROUP BY year
442
+
ORDER BY year ASC
443
+
444
+
```
445
+
The results should be the same, but the performance better on the latter example!
446
+
447
+
448
+
#### Query 2. Average price per year in London {#average-price-london-projections}
449
+
450
+
```sql runnable
451
+
SELECT
452
+
toYear(date) AS year,
453
+
round(avg(price)) AS price,
454
+
bar(price, 0, 2000000, 100)
455
+
FROMuk.uk_price_paid_with_projections_v2
456
+
WHERE town ='LONDON'
457
+
GROUP BY year
458
+
ORDER BY year ASC
459
+
SETTINGS optimize_use_projections=0
460
+
```
461
+
462
+
463
+
```sql runnable
464
+
SELECT
465
+
toYear(date) AS year,
466
+
round(avg(price)) AS price,
467
+
bar(price, 0, 2000000, 100)
468
+
FROMuk.uk_price_paid_with_projections_v2
469
+
WHERE town ='LONDON'
470
+
GROUP BY year
471
+
ORDER BY year ASC
472
+
```
473
+
474
+
#### Query 3. The most expensive neighborhoods {#most-expensive-neighborhoods-projections}
475
+
476
+
The condition (date >= '2020-01-01') needs to be modified so that it matches the projection dimension (`toYear(date) >= 2020)`:
477
+
478
+
```sql runnable
479
+
SELECT
480
+
town,
481
+
district,
482
+
count() AS c,
483
+
round(avg(price)) AS price,
484
+
bar(price, 0, 5000000, 100)
485
+
FROMuk.uk_price_paid_with_projections_v2
486
+
WHERE toYear(date) >=2020
487
+
GROUP BY
488
+
town,
489
+
district
490
+
HAVING c >=100
491
+
ORDER BY price DESC
492
+
LIMIT100
493
+
SETTINGS optimize_use_projections=0
494
+
```
495
+
496
+
```sql runnable
497
+
SELECT
498
+
town,
499
+
district,
500
+
count() AS c,
501
+
round(avg(price)) AS price,
502
+
bar(price, 0, 5000000, 100)
503
+
FROMuk.uk_price_paid_with_projections_v2
504
+
WHERE toYear(date) >=2020
505
+
GROUP BY
506
+
town,
507
+
district
508
+
HAVING c >=100
509
+
ORDER BY price DESC
510
+
LIMIT100
511
+
```
512
+
513
+
Again, the result is the same but notice the improvement in query performance for the 2nd query.
368
514
369
-
- A complete re-ordering of the data is required. While the expression in the
370
-
projection can, in theory, use a `GROUP BY,` materialized views are more
371
-
effective for maintaining aggregates. The query optimizer is also more likely
372
-
to exploit projections that use a simple reordering, i.e., `SELECT * ORDER BY x`.
373
-
Users can select a subset of columns in this expression to reduce storage
374
-
footprint.
375
-
- Users are comfortable with the associated increase in storage footprint and
376
-
overhead of writing data twice. Test the impact on insertion speed and
377
-
[evaluate the storage overhead](/data-compression/compression-in-clickhouse).
378
515
379
516
## Related content {#related-content}
380
517
-[A Practical Introduction to Primary Indexes in ClickHouse](/guides/best-practices/sparse-primary-indexes#option-3-projections)
0 commit comments