Choosing Primary Key Index
The primary index is the most critical component in a Primary Key table. The primary key index which is used to store the mapping between the primary key values and the locations of the data rows identified by the primary key values. Currently, we support three types of primary key index:- Full in-memory primary key index.
- Local disk based persistent primary key index.
- Cloud native persistent primary key index.
- No dependency on local disk capacity.
- No need to rebuild indexes after data shard rebalance.
Choosing the Primary key
The primary key usually does not help accelerate queries. You can specify a column different from the primary key as the sort key using theORDER BY clause to speed up queries. Therefore, when selecting a primary key, you only need to consider the uniqueness during data import and update processes.
The larger the primary key, the more memory, I/O, and other resources it consumes. Therefore, it is generally recommended to avoid selecting too many or overly large columns as the primary key. The default maximum size for the primary key is 128 bytes, controlled by the primary_key_limit_size parameter in be.conf.
You can increase primary_key_limit_size to select a larger primary key, but be aware that this will result in higher resource consumption.
How much storage and memory space will a persistent index occupy?
Formula for storage space cost
(key size + 8 bytes) * row count * 50%
Note
50% is the estimated compression efficiency, the actual compression effect depends on the data itself.
Formula for memory cost
min(l0_max_mem_usage * tablet cnt, update_memory_limit_percent * BE process memory);
Memory usage
The memory used by Primary Key table can be monitored by mem_tracker:update item in mem_tracker record whole memory used by Primary Key table, such as primary key index, delete vector and so on.
You can also monitor this update item via metrics monitor service. For example, in Grafana, you can check update item via (item in the red box):

l0_max_mem_usage controls the max memory usage of persistent primary key index per tablet. transaction_apply_worker_count and transaction_publish_version_worker_count both control the max thread number which can be used for handling upsert and delete in primary key table.
But you need to remember, reducing l0_max_mem_usage may increase I/O pressure, while decreasing transaction_apply_worker_count or transaction_publish_version_worker_count could slow down data ingestion.
Tradeoff between compaction resource, data freshness and query latency
Compared to tables in other models, primary key tables require additional operations for primary key index lookups and delete vector generation during data import, updates, and deletions, which introduces extra resource overhead. Therefore, you need to make trade-offs among these three factors:- Compaction resources limitation.
- Data freshness
- Query latency
Data freshness & Query latency
If you want to get better data freshness and also better query latency, that means you will introduce high frequency writes, and also want to make sure they can be compacted as soon as possible. Then you will need more compaction resource to handle these writes:compact_threads and update_compaction_num_threads_per_disk, or decrease update_compaction_per_tablet_min_interval_seconds to introduce more compaction resource to handle high frequency writes.
How do you know whether current compaction resource and setting can handle current high frequency writes? You can observe it in the following ways:
-
For shared-data cluster, if compaction cannot keep up with the ingestion rate, it may lead to ingestion slowdown or even write failure errors and ingestion stop.
a. Ingestion slowdown.
You can use
show proc /transactions/{db_name}/running';to check current running transactions, and if there is any slowdown message like :
- For shared-nothing cluster, there is no ingestion slowdown strategy, if the compaction can’t catch up current high frequency writes. Ingestion will fail and return error message:
Data freshness & Compaction resources limitation
If you have limited compaction resources but still need to maintain sufficient data freshness, this means you’ll need to sacrifice some query latency. You can make these config changes to achieve that:- Shared-data cluster
lake_ingest_slowdown_threshold parameter controls the threshold for triggering ingestion slowdown. When a partition’s compaction score exceeds this threshold, the system will begin to slowdown data ingestion. Similarly, lake_compaction_score_upper_bound determines the threshold for triggering ingestion stoppage.
- Shared-nothing cluster
tablet_max_versions determines the threshold for triggering ingestion stoppage.
By increasing these configurations, the system can accommodate more small data files and reduce compaction frequency, but this will also impact query latency.