<@U0A71G31CDV> I am evaluating schema design for m...
# questions-and-troubleshooting
m
@Rocky I am evaluating schema design for my StarRocks tables and have two questions regarding string types: 1. Storage and Performance: What is the actual difference between
STRING
and
VARCHAR(N)
regarding storage efficiency, memory usage, and scan performance, assuming the data content is identical? Does StarRocks optimize
VARCHAR(N)
differently than
STRING
during storage and column scanning? 2. Primary Key Performance: Does using
VARCHAR(N)
(with a restricted length) as a Primary Key provide significant performance advantages over
STRING
(unlimited/larger) in terms of index size, memory pressure, and key comparison speed? Specifically, is there a performance impact if the schema restricts the length, allowing for better key optimization? assuming the data content is identical?
r
Regarding your evaluation of
STRING
vs.
VARCHAR(N)
in StarRocks, here is the breakdown of performance and storage implications. 1. Storage and Performance Assuming the actual data content is identical: * Storage Efficiency: There is no difference in disk storage. Both
STRING
and
VARCHAR(N)
are variable-length types. StarRocks stores only the actual bytes of the data plus a small length header.
VARCHAR(32)
and
STRING
containing the same 10-byte string will occupy the exact same space on disk. * Memory Usage & Scan Performance: There is a significant difference in memory efficiency during query execution. * Memory Pre-allocation: In many query scenarios (such as certain scan operators, joins, or shuffles), StarRocks pre-allocates memory based on the declared length of the column rather than the actual data length. * The "STRING" Overhead: Internally, StarRocks treats
STRING
as a
VARCHAR
with a very large length (65,533 bytes or up to 1,048,576 in newer versions). If you use
STRING
for a field that only contains 20-byte addresses, StarRocks may pre-allocate memory as if the data were much larger, leading to higher memory pressure and potentially slower performance or OOMs under high concurrency. * Recommendation: Use
VARCHAR(N)
with the smallest necessary
N
to optimize memory pre-allocation during scans and shuffles. 2. Primary Key (PK) Performance Using
VARCHAR(N)
as a Primary Key (especially with a restricted length) is highly recommended over using
STRING
. * Index Size & Memory Pressure: The Primary Key index (whether in-memory or persistent) stores the mapping of key values to row locations. The Key Size is a direct factor in the formula for index overhead: * Formula:
(key size + 8 bytes) * row count
(plus some overhead). * While both types store the actual bytes, the system uses the schema's type information to manage the index structures. Using a restricted
VARCHAR(N)
ensures you stay within the default 128-byte limit for primary keys (
primary_key_limit_size
). If you use
STRING
, you risk creating an index that is unnecessarily "heavy" for the system to manage, even if the current data is small. * Key Comparison Speed: If the data content is identical, the raw CPU comparison speed of two strings is generally the same. However, using a shorter defined
VARCHAR
helps the StarRocks optimizer and execution engine make better assumptions about the memory layout, which can lead to better cache locality and overall faster index lookups during UPSERT/DELETE operations. * Best Practice: For Primary Key tables, it is always better to use the most restrictive type possible (e.g.,
INT
or
BIGINT
is best, followed by short
VARCHAR
).
STRING
should generally be avoided as a primary key unless the data is truly of unpredictable and large length (within the 128-byte PK limit). Summary Table Feature |
VARCHAR(N)
|
STRING
Storage (Disk) | Actual length (Variable) | Actual length (Variable) Memory (Query) | Pre-allocated based on
N
| Pre-allocated based on max (65,533+) PK Index Size | Optimized for
N
| Heavy (assumes large length) Scan Speed | Faster (better memory management) | Slower (potential memory pressure) Referencesfaq/operation_maintenance_faq.mdbest_practices/primarykey_table.mdfaq/table_design_faq.mdtable_design/indexes/Prefix_index_sort_key.mdadministration/management/monitoring/metrics.mdbest_practices/query_tuning/schema_tuning.md