如何基于char列对MySQL表进行分区?

时间:2022-03-15 22:26:06

Is it possible to partition based on char column?

是否可以基于char列进行分区?

After reviewing the MySQL 5.1 documentation it appears that only integer types can be used.

在查看MySQL 5.1文档后,似乎只能使用整数类型。

Is this correct? Or can I use some function to convert the char into an integer?

它是否正确?或者我可以使用一些函数将char转换为整数?

The char field in question contains a unique identifier.

有问题的char字段包含唯一标识符。

2 个解决方案

#1


15  

Partitioning in MySQL 5.1 can only deal with integer columns (Source). You can only use a few partitioning functions on non-integer columns. For example:

MySQL 5.1中的分区只能处理整数列(Source)。您只能在非整数列上使用一些分区函数。例如:

CREATE TABLE ti (id INT, amount DECIMAL(7,2), tr_date DATE)
   ENGINE=INNODB
   PARTITION BY HASH( MONTH(tr_date) )
   PARTITIONS 6;

You can also use key partitioning in MySQL 5.1, as long as the primary key includes all the columns in the table's partitioning function:

您也可以在MySQL 5.1中使用密钥分区,只要主键包含表的分区功能中的所有列:

CREATE TABLE k1 (
   id CHAR(3) NOT NULL PRIMARY KEY,
   value int
)
PARTITION BY KEY(id)
PARTITIONS 10;

On the other hand, in MySQL 5.5, you can use range column partitioning or list column partitioning on a wide variety of data types, including character-based columns.

另一方面,在MySQL 5.5中,您可以对各种数据类型(包括基于字符的列)使用范围列分区或列表列分区。

List Columns Example:

列表列示例:

CREATE TABLE expenses (
   expense_date DATE NOT NULL,
   category VARCHAR(30),
   amount DECIMAL (10,3)
);

ALTER TABLE expenses
PARTITION BY LIST COLUMNS (category)
(
   PARTITION p01 VALUES IN ('lodging', 'food'),
   PARTITION p02 VALUES IN ('flights', 'ground transportation'),
   PARTITION p03 VALUES IN ('leisure', 'customer entertainment'),
   PARTITION p04 VALUES IN ('communications'),
   PARTITION p05 VALUES IN ('fees')
);

Range Columns Example:

范围列示例:

CREATE TABLE range_test (
   code CHAR(3),
   value INT
)
PARTITION BY RANGE COLUMNS(code) (
   PARTITION p0 VALUES LESS THAN ('MMM'),
   PARTITION p1 VALUES LESS THAN ('ZZZ')
);

Further reading:

#2


0  

What do you hope to gain by PARTITIONing? HASH and LIST are unlikely to provide any performance gain. It is rarely useful to partition a table with less than a million rows.

您希望通过PARTITIONing获得什么? HASH和LIST不太可能提供任何性能提升。对具有少于一百万行的表进行分区很少有用。

Other comments on partitioning: http://mysql.rjweb.org/doc.php/partitionmaint

关于分区的其他评论:http://mysql.rjweb.org/doc.php/partitionmaint

5.6 and 5.7 have relaxed some of the restrictions mention in the other answers, but still partitioning is rarely better than indexing.

5.6和5.7放松了其他答案中提到的一些限制,但仍然分区很少比索引更好。

#1


15  

Partitioning in MySQL 5.1 can only deal with integer columns (Source). You can only use a few partitioning functions on non-integer columns. For example:

MySQL 5.1中的分区只能处理整数列(Source)。您只能在非整数列上使用一些分区函数。例如:

CREATE TABLE ti (id INT, amount DECIMAL(7,2), tr_date DATE)
   ENGINE=INNODB
   PARTITION BY HASH( MONTH(tr_date) )
   PARTITIONS 6;

You can also use key partitioning in MySQL 5.1, as long as the primary key includes all the columns in the table's partitioning function:

您也可以在MySQL 5.1中使用密钥分区,只要主键包含表的分区功能中的所有列:

CREATE TABLE k1 (
   id CHAR(3) NOT NULL PRIMARY KEY,
   value int
)
PARTITION BY KEY(id)
PARTITIONS 10;

On the other hand, in MySQL 5.5, you can use range column partitioning or list column partitioning on a wide variety of data types, including character-based columns.

另一方面,在MySQL 5.5中,您可以对各种数据类型(包括基于字符的列)使用范围列分区或列表列分区。

List Columns Example:

列表列示例:

CREATE TABLE expenses (
   expense_date DATE NOT NULL,
   category VARCHAR(30),
   amount DECIMAL (10,3)
);

ALTER TABLE expenses
PARTITION BY LIST COLUMNS (category)
(
   PARTITION p01 VALUES IN ('lodging', 'food'),
   PARTITION p02 VALUES IN ('flights', 'ground transportation'),
   PARTITION p03 VALUES IN ('leisure', 'customer entertainment'),
   PARTITION p04 VALUES IN ('communications'),
   PARTITION p05 VALUES IN ('fees')
);

Range Columns Example:

范围列示例:

CREATE TABLE range_test (
   code CHAR(3),
   value INT
)
PARTITION BY RANGE COLUMNS(code) (
   PARTITION p0 VALUES LESS THAN ('MMM'),
   PARTITION p1 VALUES LESS THAN ('ZZZ')
);

Further reading:

#2


0  

What do you hope to gain by PARTITIONing? HASH and LIST are unlikely to provide any performance gain. It is rarely useful to partition a table with less than a million rows.

您希望通过PARTITIONing获得什么? HASH和LIST不太可能提供任何性能提升。对具有少于一百万行的表进行分区很少有用。

Other comments on partitioning: http://mysql.rjweb.org/doc.php/partitionmaint

关于分区的其他评论:http://mysql.rjweb.org/doc.php/partitionmaint

5.6 and 5.7 have relaxed some of the restrictions mention in the other answers, but still partitioning is rarely better than indexing.

5.6和5.7放松了其他答案中提到的一些限制,但仍然分区很少比索引更好。