CREATE TABLE
CREATE TABLE creates a table in LindormSearch, the search engine service of Lindorm.
Syntax
CREATE TABLE [ IF NOT EXISTS ] table_name
(
column_definition
( , column_definition )*
[ , PRIMARY KEY ( primary_key ) ]
)
[ PARTITION BY RANGE TIME ( time_partition_key=value ) ]
[ PARTITION BY HASH ( hash_partition_key (, hash_partition_key)* ) ]
[ PARTITIONS intValue ]
WITH [ table_options ];
column_definition ::= column_name data_type
primary_key ::= column_name [ , column_name ]
time_partition_key ::= column_name
hash_partition_key ::= column_name [ ( salt_factor=intValue ) ]IF NOT EXISTS is supported. The primary key can consist of one or more columns.Partitioning clauses
LindormSearch supports three partitioning strategies. Choose one based on your data and query patterns.
PARTITION BY RANGE TIME
Partitions data by a time interval based on a timestamp column. Use this strategy for time series data — the system automatically groups data into time-based partitions and deletes expired partitions.
PARTITION BY RANGE TIME ( time_partition_key )Configure time partitions with the following options in the WITH clause:
| Parameter | Description | Unit |
|---|---|---|
RANGE_TIME_PARTITION_START | Number of days before creating a collection to create partitions. Use this when loading historical data. Returns an error if the start time is earlier than the earliest historical record. | Days |
RANGE_TIME_PARTITION_INTERVAL | Interval at which new partitions are created. For example, '7' creates a new partition every 7 days. | Days |
RANGE_TIME_PARTITION_TTL | Retention period for each partition. Expired partitions are deleted automatically. For example, '180' retains partitions for 180 days. | Days |
RANGE_TIME_PARTITION_MAX_OVERLAP | Number of days after the current time that data writes are accepted. | Days |
RANGE_TIME_PARTITION_FIELD_TIMEUNIT | Unit of the timestamp column used for partitioning. Default: ms. Valid values: s (seconds, 10-digit timestamps) or ms (milliseconds, 13-digit timestamps). | — |
PARTITION BY HASH
Partitions data by the hash value of one or more columns. Use this strategy to prevent hot data issues when large volumes of data are written to the same partition.
PARTITION BY HASH ( hash_partition_key [ ( salt_factor=intValue ) ] [, ...] )By default, LindormSearch hashes on the primary key. To distribute data more evenly, specify a custom partition key.
Partition key limits:
Partition key values cannot be changed.
Partition key values cannot be empty.
Single-level vs. multi-level hash partitions
Single-level hash partitioning splits data into N partitions by hashing one column. If one hash key value has a disproportionately large share of rows (a hot key), all those rows still land in the same partition. This affects the read and write performance in a negative manner.
Multi-level hash partitioning uses a salt factor to further subdivide rows that share the same level-1 partition key value. This breaks hot-key concentrations into subpartitions and improves query efficiency: when a query filters on both the level-1 and level-2 columns, LindormSearch scans only one partition.
Salt factor behavior:
The number of subpartitions equals 2 to the power of salt_factor. Keep salt_factor small — if the total partition count is 16 and salt_factor exceeds 4, HASH partitioning cannot split data into subpartitions.
salt_factor | Subpartitions (given 16 total partitions) |
|---|---|
1 | 1/2 of total |
2 | 1/4 of total |
3 | 1/8 of total |
Requirements for multi-level partitions:
For 2-level HASH partitioning, specify
salt_factorfor the level-1 partition key.For 3-level HASH partitioning, specify
salt_factorfor both the level-1 and level-2 partition keys.LindormSearch supports up to three levels of hash partitions.
Combined HASH and RANGE TIME
Use both strategies together when data has both a time dimension and a hot-key risk. PARTITION BY RANGE TIME handles time-based data lifecycle; PARTITION BY HASH distributes writes evenly within each time partition.
PARTITIONS
PARTITIONS intValueSpecifies the total number of partitions.
Examples
Basic table with a composite primary key
CREATE TABLE dt (
p1 integer, p2 varchar, c1 integer, c2 integer,
CONSTRAINT pk PRIMARY KEY(p1, p2)
);Time-partitioned table (7-day interval, 90-day TTL)
Partitions data by the orderTime column. Starts creating partitions 30 days before table creation to accommodate historical data, creates a new partition every 7 days, and retains each partition for 90 days.
CREATE TABLE dt_partition (
storeId integer, goodsId varchar, goodsPrice integer, orderTime bigint,
CONSTRAINT pk PRIMARY KEY(storeId)
)
PARTITION BY RANGE TIME(orderTime) PARTITIONS 16
WITH (
RANGE_TIME_PARTITION_START='30',
RANGE_TIME_PARTITION_INTERVAL='7',
RANGE_TIME_PARTITION_TTL='90',
RANGE_TIME_PARTITION_MAX_OVERLAP='10'
);Time-partitioned table (30-day interval, second-precision timestamps)
Similar to the previous example, but with a 180-day start, a 30-day interval, a 180-day TTL, and orderTime values in seconds (10-digit timestamps).
CREATE TABLE dt_partition (
storeId integer, goodsId varchar, goodsPrice integer, orderTime bigint,
CONSTRAINT pk PRIMARY KEY(storeId)
)
PARTITION BY RANGE TIME(orderTime) PARTITIONS 16
WITH (
RANGE_TIME_PARTITION_START='180',
RANGE_TIME_PARTITION_INTERVAL='30',
RANGE_TIME_PARTITION_TTL='180',
RANGE_TIME_PARTITION_MAX_OVERLAP='10',
RANGE_TIME_PARTITION_FIELD_TIMEUNIT='s'
);Single-level hash partitioning
Distributes data across 16 partitions by hashing the storeId column.
CREATE TABLE dt_partition (
storeId integer, goodsId varchar, goodsPrice integer,
CONSTRAINT pk PRIMARY KEY(storeId)
)
PARTITION BY HASH(storeId)
PARTITIONS 16;Multi-level hash partitioning (level-2)
Adds a second level of partitioning on goodsId. Rows with the same storeId value are further distributed across subpartitions using salt_factor=2 (1/4 of total partitions per storeId value).
CREATE TABLE dt_partition (
storeId integer, goodsId varchar, goodsPrice integer,
CONSTRAINT pk PRIMARY KEY(storeId)
)
PARTITION BY HASH(storeId(salt_factor=2), goodsId)
PARTITIONS 16;Combined HASH and RANGE TIME partitioning
Partitions by time on orderTime and distributes within each time partition by hashing storeId.
CREATE TABLE dt_partition (
storeId integer, goodsId varchar, goodsPrice integer, orderTime bigint,
CONSTRAINT pk PRIMARY KEY(storeId)
)
PARTITION BY RANGE TIME(orderTime)
PARTITION BY HASH(storeId) PARTITIONS 16
WITH (
RANGE_TIME_PARTITION_START='180',
RANGE_TIME_PARTITION_INTERVAL='30',
RANGE_TIME_PARTITION_TTL='180',
RANGE_TIME_PARTITION_MAX_OVERLAP='10'
);