如何根据 char 列对 MySQL 表进行分区?

发布于 2024-09-24 08:11:04 字数 129 浏览 6 评论 0原文

是否可以根据char列进行分区?

查看 MySQL 5.1 文档后发现只能使用整数类型。

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

所讨论的 char 字段包含唯一标识符。

Is it possible to partition based on char column?

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

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

The char field in question contains a unique identifier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-10-01 08:11:04

MySQL 5.1 中的分区只能处理整数列(来源)。您只能使用少数分区函数非整数列。例如:

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

您还可以在MySQL中使用键分区 5.1,只要主键包含表的分区函数中的所有列:

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

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

列表列示例:

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')
);

范围列示例:

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')
);

进一步阅读:

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:

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:

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.

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:

身边 2024-10-01 08:11:04

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

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

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

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.

Other comments on partitioning: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文