All Products
Search
Document Center

PolarDB:STR_HASH

Last Updated:Mar 28, 2026

STR_HASH is a partitioning function for PolarDB-X 1.0 that routes string-keyed data to a single database or table shard. It extracts a substring from the partition key value — by position or by length — and uses that substring to calculate the target shard.

Warning

STR_HASH supports point queries only. Range queries on the partition key cannot be routed to a single shard and trigger a full table scan, causing slow response times.

Limitations

  • The partition key column must be of type CHAR or VARCHAR.

  • STR_HASH parameters cannot be changed after the table is created.

  • The PolarDB-X 1.0 instance must be version 5.3.5 or later.

Syntax

STR_HASH( shardKey [, startIndex, endIndex [, valType [, randSeed ] ] ] )

Parameters

ParameterDescriptionDefault
shardKeyThe partition key column name.
startIndexStart position of the substring (0-based). Set to -1 to use the last endIndex characters.-1
endIndexEnd position of the substring (0-based, exclusive). Set to -1 to use the first startIndex characters.-1
valTypeHow the substring is interpreted for routing: 0 = string hash, 1 = integer.0
randSeedRandom seed for the string hash when valType=0. Change this only if data distribution is uneven. Default is 31; other valid values include 131, 13131, and 1313131.31

Substring extraction rules

The startIndex and endIndex parameters control which part of the partition key value is used for routing.

ConditionSubstring extracted
startIndex=j, endIndex=k (j≥0, k≥0, k>j)Characters in the range [j, k) of the original string
startIndex=-1, endIndex=k (k≥0)Last k characters. If the string is shorter than k, the entire string is used.
startIndex=k, endIndex=-1 (k≥0)First k characters. If the string is shorter than k, the entire string is used.
startIndex=-1, endIndex=-1No truncation — the entire string is used.

Examples using ABCDEFG:

ExpressionResult
[1, 5)BCDE
[2, 2)'' (empty)
[4, 100)EFG
[100, 105)'' (empty)

valType values

  • 0 (default): PolarDB-X 1.0 treats the substring as a string and applies a hash function to calculate the shard.

  • 1: PolarDB-X 1.0 treats the substring as an integer to calculate the shard. The integer value must not exceed 9223372036854775807 and cannot be a floating-point number.

Note

randSeed applies only when valType=0. If you change randSeed after creating the table, export all data and re-import it using the new partitioning algorithm to redistribute data correctly.

Use cases

STR_HASH is designed for scenarios where a string column (such as an order ID or user ID) is the partition key and point queries dominate the access pattern — for example, looking up a transaction or logistics order by ID.

When STR_HASH achieves precise routing:

The default HASH function computes a hash of the entire string value, which may map multiple distinct values to the same shard. STR_HASH lets you extract a known-numeric substring (such as the last three digits of an order ID) and treat it as an integer, guaranteeing that each distinct substring value maps to exactly one shard.

Example scenario: An internet finance application partitions data into database shards by year and month using YYYYMM, then partitions into table shards by order ID. The last three characters of each order ID are integers in the range 000999. The requirement is that each distinct three-digit suffix routes to exactly one physical table shard.

-- Routing: int(order_id[-3:]) % 1024 → one physical table shard per suffix value
CREATE TABLE test_str_hash_tb (
    id INT NOT NULL AUTO_INCREMENT,
    order_id VARCHAR(30) NOT NULL,
    create_time DATETIME DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
DBPARTITION BY YYYYMM(`create_time`)
TBPARTITION BY STR_HASH(`order_id`, -1, 3, 1) TBPARTITIONS 1024;

With 1,024 table shards and 1,000 distinct suffix values (000999), each suffix routes to exactly one shard. The default HASH function cannot guarantee this because its hash may map multiple string values to the same integer, causing one shard to serve multiple suffix values.

Examples

The following examples use a table with order_id VARCHAR(32) as the partition key, partitioned into four database shards and eight table shards (four databases, two tables each).

Use the last four characters as an integer for routing:

-- Routing: int(order_id[-4:]) % 4 → database shard; % 2 → table shard
CREATE TABLE test_str_hash_tb (
    id INT NOT NULL AUTO_INCREMENT,
    order_id VARCHAR(32) NOT NULL,
    create_time DATETIME DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
DBPARTITION BY STR_HASH(`order_id`, -1, 4, 1)
TBPARTITION BY STR_HASH(`order_id`, -1, 4, 1) TBPARTITIONS 2;

Use characters at positions 2–6 (third through seventh) as a string for routing:

-- Routing: hash(order_id[2:7]) % 4 → database shard; % 2 → table shard
CREATE TABLE test_str_hash_tb (
    id INT NOT NULL AUTO_INCREMENT,
    order_id VARCHAR(32) NOT NULL,
    create_time DATETIME DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
DBPARTITION BY STR_HASH(`order_id`, 2, 7)
TBPARTITION BY STR_HASH(`order_id`, 2, 7) TBPARTITIONS 2;

Use the first five characters as a string for routing:

-- Routing: hash(order_id[:5]) % 4 → database shard; % 2 → table shard
CREATE TABLE test_str_hash_tb (
    id INT NOT NULL AUTO_INCREMENT,
    order_id VARCHAR(32) NOT NULL,
    create_time DATETIME DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
DBPARTITION BY STR_HASH(`order_id`, 5, -1)
TBPARTITION BY STR_HASH(`order_id`, 5, -1) TBPARTITIONS 2;

FAQ

What is the difference between STR_HASH(order_id) and HASH(order_id)?

Both functions use a string column as the partition key and compute a hash to determine the target shard. The key differences are:

  • STR_HASH lets you extract a substring before hashing, so you can target a known segment of the key value (such as the last four digits of an order ID).

  • STR_HASH uses the UNI_HASH algorithm for string-based routing. HASH performs a simpler modulo operation on the full string hash.

  • When valType=1, STR_HASH treats the substring as an integer and routes deterministically — each distinct integer maps to exactly one shard. HASH does not offer this guarantee.

Use STR_HASH when you need precise one-to-one routing between a known substring value and a shard. Use HASH for general-purpose string partitioning when substring extraction is not required.