Schema
Types
It is important to use the correct type for encoding columns (e.g., BIGINT, DATE, TIMESTAMP). While it is always possible to use string types (VARCHAR, etc.) to encode more specific values, this is not recommended. Strings use more space and are slower to process in operations such as filtering, join, and aggregation. When loading CSV files, you may leverage the CSV reader's auto-detection mechanism to determine the correct types for CSV inputs.
If you run in a memory-constrained environment, using smaller data types (e.g., TINYINT) can reduce the amount of memory and disk space required to complete a query. SereneDB's bitpacking compression means small values stored in larger data types will not take up larger sizes on disk, but they will take up more memory during processing – hence, using the most restrictive types possible when creating columns is necessary to reduce memory consumption.
Using Temporal Types
Encode temporal data with a native temporal type such as TIMESTAMP or DATE rather than storing it as text. Native temporal types are stored more compactly and are substantially faster to filter, aggregate and extract parts from than the equivalent string representation, and the gap grows as tables get larger.
With a TIMESTAMP column, you can operate on the value directly with the extract datetime function:
SELECT avg(extract('day' FROM creationDate)) FROM Comment;avg ----- 12
Storing the same value as a VARCHAR forces slower and more error-prone string manipulation to obtain the same result:
SELECT avg(CAST(creationDate[9:10] AS INTEGER)) FROM Comment;avg ----- 12
Joining on the Right Type
When a column holds numeric identifiers, define it with a numeric type such as BIGINT rather than VARCHAR. Joins, filters and aggregations on native numeric types are faster than the same operations on strings encoding the same values, because numeric comparisons are cheaper than string comparisons and the values are stored more compactly.
For example, a self-join on an identifier column performs best when both the join key and the referenced column are typed as BIGINT:
SELECT count(*) AS countFROM Comment c1JOIN Comment c2 ON c1.ParentCommentId = c2.id; count
-------
3Defining those same columns as VARCHAR produces identical results but materially slower joins, and the difference becomes more pronounced as the data grows.
Constraints
PRIMARY KEY, UNIQUE and FOREIGN KEY constraints are integrity tools. They guarantee that keys are unique and that references point at existing rows, and every load and update pays to keep that guarantee.
Schemas written for transactional databases such as PostgreSQL usually give every table a primary key and add unique and foreign keys out of habit. An analytical schema rarely needs them: a key can help an occasional query, but most analytical workloads gain little from one while paying for it on every write.
Declare a key when:
- your application relies on the guarantee: no duplicate keys, no rows referencing a missing parent;
- a statement requires one:
INSERT ... ON CONFLICTneeds a unique key on its conflict target.
To look rows up or search by a column, create an index on it rather than a key.
Loading Into a Table With Keys
When a table needs keys, load the data first and add the keys afterwards with ALTER TABLE: checking them once over the loaded data is cheaper than checking them on every insert.