Skip to main content

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 modelApplicable scenariosAdvantagesNotes
DuplicateAd-hoc queries on any dimensionNot 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)
UniqueRequires primary key uniqueness and data updatesGuarantees primary key uniqueness; supports flexible updatesCannot leverage pre-aggregation (but supported through asynchronous materialized views)
AggregateReporting queries with fixed patternsPre-aggregation significantly reduces the data scanned and computed at query timecount(*) 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:

  1. Column order: Key columns must come before all Value columns.
  2. Prefer integer types: Use integer types whenever possible, because integer computation and lookup are far more efficient than for strings.
  3. Use the smallest sufficient integer length: When choosing among integer types of different lengths, follow the principle of "just enough".
  4. Choose string types to match the data: For VARCHAR, use the smallest sufficient declared length. STRING does 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:

ModelRole of the Key columns
DuplicateActs only as "sort columns"; does not provide unique identification
Aggregate, UniqueActs 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:

ColumnNameTypeAggregationTypeComment
user_idLARGEINTUser ID
dateDATEData load date
costBIGINTSUMTotal user spend

The storage engine already contains data from two load batches:

batch 1

user_iddatecost
100012017/11/2050
100022017/11/2139

batch 2

user_iddatecost
100012017/11/201
100012017/11/215
100032017/11/2222

The two batches contain two rows for user 10001 on 2017/11/20. Queries combine their costs and return the following logical rows:

user_iddatecost
100012017/11/2051
100012017/11/215
100022017/11/2139
100032017/11/2222

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_iddatecost
100012017/11/2051
100012017/11/215
100022017/11/2139
100032017/11/2222

The correct result of SELECT COUNT(*) FROM table; should be 4. However:

  • Scanning only the user_id column with query-time aggregation yields 3 (10001, 10002, 10003), which is incorrect.
  • Scanning only the user_id column without query-time aggregation yields 5 (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:

ColumnNameTypeAggregateTypeComment
user_idBIGINTUser ID
dateDATEData load date
costBIGINTSUMTotal user spend
countBIGINTSUMUsed 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_iddatecostdelete bit
100012017/11/2050FALSE
100022017/11/2139FALSE

After the second batch is loaded, the duplicate rows in the first batch are marked as deleted:

batch 1

user_iddatecostdelete bit
100012017/11/2050TRUE
100022017/11/2139FALSE

batch 2

user_iddatecostdelete bit
100012017/11/201FALSE
100012017/11/215FALSE
100032017/11/2222FALSE

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.

Ask AI