Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

mysql - Partitioning BY HASH using VARCHAR field

In a given table, I am trying to apply a partitioning scheme where the field used is a VARCHAR (23), something like a Guid.

Ex: 5cb4932bfbca0c0010343e68

The idea is to create 4 partitions using hash partitioning. For this I believe that you have to use some function that converts the field into a value that always fits the partitioning clause, I don't know if this is possible in MySQL.

CREATE TABLE MyTable (
  Id char(36) NOT NULL,
  ApiId varchar(23) NOT NULL,
  Classification int(11) NOT NULL,
  PRIMARY KEY (Id)
)
PARTITION BY HASH(ApiId)
PARTITIONS 4;
question from:https://stackoverflow.com/questions/65945388/partitioning-by-hash-using-varchar-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you need to supply a hash function which returns an integer. From HASH partitioning...

To partition a table using HASH partitioning, it is necessary to append to the CREATE TABLE statement a PARTITION BY HASH (expr) clause, where expr is an expression that returns an integer.

The simplest solution is to store your ID as an integer and convert it to a hex string as needed. This is faster than storing varchar and uses less memory.

Unfortunately your ID is also too large to fit into a bigint. It's about 92 bits. Instead, use varbinary(12) (and varbinary(16) for 128 bit UUIDs).

Then you need to write a hash function to turn that varbinary value into an integer for hashing. Unfortunately I don't know how to do that in MySQL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...