Partitioning in ClickHouse helps manage large datasets efficiently. Let’s see strategies to apply partitioning with Spring Boot applications.
1. Create a Partitioned Table
CREATE TABLE orders (
id UInt32,
region String,
created_at DateTime,
amount Float32
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(created_at)
ORDER BY id;
Data is split by month.
2. Insert Data from Spring Boot
jdbcTemplate.update(
"INSERT INTO orders (id, region, created_at, amount) VALUES (?,?,?,?)",
101, "US-East", LocalDateTime.now(), 250.0
);
Data goes into the correct partition automatically.
3. Query Specific Partitions
List<Map<String,Object>> results = jdbcTemplate.queryForList(
"SELECT * FROM orders WHERE created_at >= '2025-08-01' AND created_at < '2025-09-01'"
);
Only the targeted partitions are scanned.
4. Alternative Partition Strategies
- By region:
PARTITION BY region for geographically distributed data.
- By year:
PARTITION BY toYYYY(created_at) for long history storage.
- By custom keys: user-defined hash or categorical fields.
5. Expose Partitioned Data APIs
@GetMapping("/api/orders/month/{month}")
public List<Map<String,Object>> getMonthlyOrders(@PathVariable String month) {
return jdbcTemplate.queryForList(
"SELECT * FROM orders WHERE toYYYYMM(created_at)=?", month
);
}
Provides fast, partition-aware queries.
image quote pre code