|
| 1 | +# TTL |
| 2 | + |
| 3 | +Source: <https://clickhouse.com/docs/guides/developer/ttl> |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +TTL refers to the capability of having rows or columns moved, deleted, or rolled up after a certain interval of time has passed. |
| 8 | +Use cases: |
| 9 | +- Removing old data: no surprise, you can delete rows or columns after a specified time interval. |
| 10 | +- Moving data between disks: after a certain amount of time, you can move data between storage volumes - useful for deploying a hot/warm/cold architecture. |
| 11 | +- Data rollup: rollup your older data into various useful aggregations and computations before deleting it. |
| 12 | + |
| 13 | +For example: |
| 14 | + |
| 15 | +```sql |
| 16 | +CREATE TABLE example1 ( |
| 17 | + timestamp DateTime, |
| 18 | + x UInt32 TTL timestamp + INTERVAL 1 MONTH, |
| 19 | + y String TTL timestamp + INTERVAL 1 DAY, |
| 20 | + z String |
| 21 | +) |
| 22 | +ENGINE = MergeTree |
| 23 | +ORDER BY tuple() |
| 24 | +``` |
| 25 | + |
| 26 | +When the interval lapses, the column expires. ClickHouse replaces the column with the default value of its data types. If all the column values in the data part expire, ClickHouse deletes this column from the data part in the system. |
| 27 | + |
| 28 | +## Removing rows |
| 29 | + |
| 30 | +```sql |
| 31 | +CREATE TABLE customers ( |
| 32 | +timestamp DateTime, |
| 33 | +name String, |
| 34 | +balance Int32, |
| 35 | +address String |
| 36 | +) |
| 37 | +ENGINE = MergeTree |
| 38 | +ORDER BY timestamp |
| 39 | +TTL timestamp + INTERVAL 12 HOUR -- <-- TTL rule at the table level |
| 40 | +-- TTL time + INTERVAL 1 MONTH DELETE WHERE event != 'error', |
| 41 | +-- time + INTERVAL 6 MONTH DELETE WHERE event = 'error' |
| 42 | +``` |
| 43 | + |
| 44 | +## Removing columns |
| 45 | + |
| 46 | +```sql |
| 47 | +ALTER TABLE customers |
| 48 | +MODIFY COLUMN balance Int32 TTL timestamp + INTERVAL 2 HOUR, |
| 49 | +MODIFY COLUMN address String TTL timestamp + INTERVAL 2 HOUR |
| 50 | +``` |
| 51 | + |
| 52 | +## Implementing a rollup |
| 53 | + |
| 54 | +## Implementing a hot/warm/cold architecture |
| 55 | + |
| 56 | +To move the data around as it gets older. |
| 57 | + |
| 58 | +1. The `TO DISK` and `TO VOLUME` options refer to the names of disks or volumes defined in your ClickHouse configuration files. |
| 59 | + |
| 60 | +```xml |
| 61 | +<!-- /etc/clickhouse-server/config.d/ --> |
| 62 | +<clickhouse> |
| 63 | + <storage_configuration> |
| 64 | + <disks> |
| 65 | + <default> |
| 66 | + </default> |
| 67 | + <hot_disk> |
| 68 | + <path>./hot/</path> |
| 69 | + </hot_disk> |
| 70 | + <warm_disk> |
| 71 | + <path>./warm/</path> |
| 72 | + </warm_disk> |
| 73 | + <cold_disk> |
| 74 | + <path>./cold/</path> |
| 75 | + </cold_disk> |
| 76 | + </disks> |
| 77 | + <policies> |
| 78 | + <default> |
| 79 | + <volumes> |
| 80 | + <default> |
| 81 | + <disk>default</disk> |
| 82 | + </default> |
| 83 | + <hot_volume> |
| 84 | + <disk>hot_disk</disk> |
| 85 | + </hot_volume> |
| 86 | + <warm_volume> |
| 87 | + <disk>warm_disk</disk> |
| 88 | + </warm_volume> |
| 89 | + <cold_volume> |
| 90 | + <disk>cold_disk</disk> |
| 91 | + </cold_volume> |
| 92 | + </volumes> |
| 93 | + </default> |
| 94 | + </policies> |
| 95 | + </storage_configuration> |
| 96 | +</clickhouse> |
| 97 | +``` |
| 98 | + |
| 99 | +2. Checkout the disks: |
| 100 | + |
| 101 | +```sql |
| 102 | +SELECT name, path, free_space, total_space |
| 103 | +FROM system.disks |
| 104 | +``` |
| 105 | + |
| 106 | +```text |
| 107 | +┌─name────────┬─path───────────┬───free_space─┬──total_space─┐ |
| 108 | +│ cold_disk │ ./data/cold/ │ 179143311360 │ 494384795648 │ |
| 109 | +│ default │ ./ │ 179143311360 │ 494384795648 │ |
| 110 | +│ hot_disk │ ./data/hot/ │ 179143311360 │ 494384795648 │ |
| 111 | +│ warm_disk │ ./data/warm/ │ 179143311360 │ 494384795648 │ |
| 112 | +└─────────────┴────────────────┴──────────────┴──────────────┘ |
| 113 | +``` |
| 114 | + |
| 115 | +3. Verify the volumes: |
| 116 | + |
| 117 | +```sql |
| 118 | +SELECT |
| 119 | + volume_name, |
| 120 | + disks |
| 121 | +FROM system.storage_policies |
| 122 | +``` |
| 123 | + |
| 124 | +```text |
| 125 | +┌─volume_name─┬─disks─────────┐ |
| 126 | +│ default │ ['default'] │ |
| 127 | +│ hot_volume │ ['hot_disk'] │ |
| 128 | +│ warm_volume │ ['warm_disk'] │ |
| 129 | +│ cold_volume │ ['cold_disk'] │ |
| 130 | +└─────────────┴───────────────┘ |
| 131 | +``` |
| 132 | + |
| 133 | +4. Add a `TTL` rule that moves the data between the hot, warm and cold volumes: |
| 134 | + |
| 135 | +```sql |
| 136 | +ALTER TABLE my_table |
| 137 | + MODIFY TTL |
| 138 | + trade_date TO VOLUME 'hot_volume', |
| 139 | + trade_date + INTERVAL 2 YEAR TO VOLUME 'warm_volume', |
| 140 | + trade_date + INTERVAL 4 YEAR TO VOLUME 'cold_volume'; |
| 141 | +``` |
| 142 | + |
| 143 | +5. The new `TTL` rule should materialize, but you can force it to make sure: |
| 144 | + |
| 145 | +```sql |
| 146 | +ALTER TABLE my_table |
| 147 | + MATERIALIZE TTL |
| 148 | +``` |
| 149 | + |
| 150 | +6. Verify your data has moved. |
| 151 | + |
| 152 | +```sql |
| 153 | +Using the system.parts table, view which disks the parts are on for the crypto_prices table: |
| 154 | + |
| 155 | +SELECT |
| 156 | + name, |
| 157 | + disk_name |
| 158 | +FROM system.parts |
| 159 | +WHERE (table = 'my_table') AND (active = 1) |
| 160 | +``` |
| 161 | + |
| 162 | +```text |
| 163 | +┌─name────────┬─disk_name─┐ |
| 164 | +│ all_1_3_1_5 │ warm_disk │ |
| 165 | +│ all_2_2_0 │ hot_disk │ |
| 166 | +└─────────────┴───────────┘ |
| 167 | +``` |
0 commit comments