Table Model Best Practices
Choose a table model before creating a table in VeloDB Cloud. Base the choice on whether you need raw records, updates by key, or summaries by fixed dimensions. The model also affects column design and the work needed to return correct query results.
Data model selection recommendations
The table model is fixed at creation and cannot be modified. Compare how each model handles repeated keys before choosing one.
Comparison of the three models
| Data model | Applicable scenarios | Advantages | Notes |
|---|---|---|---|
| Duplicate | Ad-hoc queries on any dimension | Not bound by aggregation model constraints; takes full advantage of columnar storage (reads only required columns) | Cannot leverage pre-aggregation (but supported through asynchronous materialized views) |
| Unique | Requires primary key uniqueness and data updates | Guarantees primary key uniqueness; supports flexible updates | Cannot leverage pre-aggregation (but supported through asynchronous materialized views) |
| Aggregate | Reporting queries with fixed patterns | Pre-aggregation significantly reduces the data scanned and computed at query time | count(*) queries are unfriendly; pay attention to semantic correctness when running aggregation queries that differ from the declared aggregation type |
Partial column update requirements
If your business requires partial column updates, see the following documents for usage recommendations:
Recommendations for column types when creating tables
When creating a table, follow these conventions for column types and ordering:
- Column order: Key columns must come before all Value columns.
- Prefer integer types: Use integer types whenever possible, because integer computation and lookup are far more efficient than for strings.
- Use the smallest sufficient integer length: When choosing among integer types of different lengths, follow the principle of "just enough".
- Choose string types to match the data: For
VARCHAR, use the smallest sufficient declared length.STRINGdoes not take a declared length and can only be used in Value columns.
Semantic differences of Key columns across models
The Duplicate, Aggregate, and Unique models all specify Key columns at table creation time, but their semantics differ:
| Model | Role of the Key columns |
|---|---|
| Duplicate | Acts only as "sort columns"; does not provide unique identification |
| Aggregate, Unique | Acts as both "sort columns" and "unique identification columns"; these are Key columns in the true sense |
Limitations of the Aggregate model
Queries on Aggregate Key tables return final aggregated values. Rows from separate load batches may remain physically separate until compaction, so the query engine combines rows with matching keys before returning results.
Example of consistency guarantees
Assume the following table schema:
| ColumnName | Type | AggregationType | Comment |
|---|---|---|---|
| user_id | LARGEINT | User ID | |
| date | DATE | Data load date | |
| cost | BIGINT | SUM | Total user spend |
The storage engine already contains data from two load batches:
batch 1
| user_id | date | cost |
|---|---|---|
| 10001 | 2017/11/20 | 50 |
| 10002 | 2017/11/21 | 39 |
batch 2
| user_id | date | cost |
|---|---|---|
| 10001 | 2017/11/20 | 1 |
| 10001 | 2017/11/21 | 5 |
| 10003 | 2017/11/22 | 22 |
The two batches contain two rows for user 10001 on 2017/11/20. Queries combine their costs and return the following logical rows:
| user_id | date | cost |
|---|---|---|
| 10001 | 2017/11/20 | 51 |
| 10001 | 2017/11/21 | 5 |
| 10002 | 2017/11/21 | 39 |
| 10003 | 2017/11/22 | 22 |
The query engine performs a final aggregation so queries return consistent results across load batches.
Semantic pitfall of inconsistent aggregation queries
When running an aggregation query on a Value column with an aggregation type different from the declared one, pay attention to semantics. For example:
SELECT MIN(cost) FROM table;
The result is 5, not 1.
Row-count query overhead
The consistency mechanism of the Aggregate model significantly reduces query efficiency in certain queries. The most typical case is the count(*) query:
SELECT COUNT(*) FROM table;
Some table models can count rows by scanning a single column. Aggregate Key tables must instead identify the distinct combinations of Key columns across load batches. For an Aggregate Key table, counting logical rows requires additional work to combine matching keys across load batches.
Using the data above as an example, the final aggregated result is:
| user_id | date | cost |
|---|---|---|
| 10001 | 2017/11/20 | 51 |
| 10001 | 2017/11/21 | 5 |
| 10002 | 2017/11/21 | 39 |
| 10003 | 2017/11/22 | 22 |
The correct result of SELECT COUNT(*) FROM table; should be 4. However:
- Scanning only the
user_idcolumn with query-time aggregation yields3(10001, 10002, 10003), which is incorrect. - Scanning only the
user_idcolumn without query-time aggregation yields5(5 rows total across both batches), which is incorrect.
To return the correct count of 4, the query engine must read both user_id and date and combine rows with matching keys. In general, COUNT(*) on an Aggregate Key table requires scanning all Key columns to identify each logical row. More Key columns increase the amount of data that must be scanned.
Optimize row-count queries
When the workload contains frequent count(*) queries, a constant-one column can reduce the work needed to count rows. The aggregation method determines what is counted. A SUM column counts input rows and is equivalent to count(*) only when aggregate keys never repeat. Modify the table schema in the previous example as follows:
| ColumnName | Type | AggregateType | Comment |
|---|---|---|---|
| user_id | BIGINT | User ID | |
| date | DATE | Data load date | |
| cost | BIGINT | SUM | Total user spend |
| count | BIGINT | SUM | Used to compute count |
When loading data, set the count column value to a constant 1. Then:
SELECT COUNT(*) FROM table;
-- equivalent to
SELECT SUM(count) FROM table;
The latter can avoid scanning all Key columns, subject to the restriction below.
Usage restriction: You must ensure that no rows with identical AGGREGATE KEY columns are loaded multiple times. Otherwise, SELECT SUM(count) FROM table; only represents the number of originally loaded rows, and is no longer equivalent to SELECT COUNT(*) FROM table;.
Alternative: For a new table that must count aggregate-key groups even when keys repeat, define the count column with REPLACE and always load the value 1. Then SELECT SUM(count) FROM table; and SELECT COUNT(*) FROM table; return the same count of logical rows. This is an alternative schema definition, not an instruction to change an existing column's aggregation method.
Merge-on-write implementation of the Unique model
The merge-on-write implementation of the Unique model does not have the limitations of the Aggregate model described above. Using the same data as before, merge-on-write adds a corresponding delete bitmap to the rowset of each load to mark which data has been overwritten.
After the first batch is loaded:
batch 1
| user_id | date | cost | delete bit |
|---|---|---|---|
| 10001 | 2017/11/20 | 50 | FALSE |
| 10002 | 2017/11/21 | 39 | FALSE |
After the second batch is loaded, the duplicate rows in the first batch are marked as deleted:
batch 1
| user_id | date | cost | delete bit |
|---|---|---|---|
| 10001 | 2017/11/20 | 50 | TRUE |
| 10002 | 2017/11/21 | 39 | FALSE |
batch 2
| user_id | date | cost | delete bit |
|---|---|---|---|
| 10001 | 2017/11/20 | 1 | FALSE |
| 10001 | 2017/11/21 | 5 | FALSE |
| 10003 | 2017/11/22 | 22 | FALSE |
At query time, all rows marked as deleted in the delete bitmap are filtered out and no data aggregation is required. The data above contains 4 valid rows, and the query result is also 4 rows. The query can therefore use the lowest-overhead approach mentioned earlier: "scan only one column to obtain the count".
For count(*) queries, merge-on-write avoids the query-time aggregation described above. The performance difference depends on the workload, Key columns, data volume, and compaction state. Measure it with representative data before using it to size a warehouse.
Duplicate model
The Duplicate model does not have the limitations of the Aggregate model described above. Because this model does not involve aggregation semantics, a count(*) query can scan any single column to obtain a semantically correct result.