How to partition a MySQL table based on char column?

懵懂的女人 提交于 2020-01-22 12:15:33

问题


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.


回答1:


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:

  • MySQL 5.1 Reference Manual :: Key partitioning
  • A deep look at MySQL 5.5 partitioning enhancements
  • MySQL 5.5 Reference Manual :: Range columns partitioning
  • MySQL 5.5 Reference Manual :: List columns partitioning



回答2:


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.



来源:https://stackoverflow.com/questions/3753317/how-to-partition-a-mysql-table-based-on-char-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!