简体   繁体   English

如何创建没有主键的 MySql 分区?

[英]How to Create MySql Partitions without primary key?

I have a database which use sequence number as its primary key.我有一个使用序列号作为主键的数据库。 Other than there is a column called "date_time" which can be duplicated.除了有一个可以复制的名为“date_time”的列。

Now I need to make partitions by using date_time as follows.现在我需要使用date_time进行分区,如下所示。

ALTER TABLE data
PARTITION BY RANGE (TO_DAYS('date_time')) (
 PARTITION p20220103 VALUES LESS THAN (TO_DAYS('2022-01-04 00:00:00')),
 PARTITION p20220104 VALUES LESS THAN (TO_DAYS('2022-01-05 00:00:00')),
 PARTITION p20220105 VALUES LESS THAN MAXVALUE
);

Since the date_time is not a primary key in data table, I couldn't create partitions.由于date_time不是data表中的主键,我无法创建分区。

ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function (prefixed columns are not considered).

How should I create partitions without adding date_time as a primary key?我应该如何创建分区而不添加date_time作为主键?

You cannot.你不能。 The rule is simple, stated in https://dev.mysql.com/doc/refman/8.0/en/partitioning-limitations-partitioning-keys-unique-keys.html :规则很简单,在https://dev.mysql.com/doc/refman/8.0/en/partitioning-limitations-partitioning-keys-unique-keys.ZFC35FDC70AD5FC69D236EZ878中说明:

Every unique key on the table must use every column in the table's partitioning expression.表上的每个唯一键都必须使用表分区表达式中的每一列。

If the table has a primary key or unique key but that key does not include the column(s) in the partitioning expression, then it cannot enforce uniqueness when you insert a new row without checking every partition for duplicates.如果表有一个主键或唯一键,但该键不包括分区表达式中的列,那么当您插入新行而不检查每个分区是否有重复项时,它不能强制唯一性。

The only way around this, to allow a column like your date_time to be the partitioning expression, is to define the table with no primary or unique key .解决此问题的唯一方法是允许像date_time这样的列作为分区表达式,即定义没有主键或唯一键的表

This has its own hazards.这有其自身的危害。 You may need a unique key so you can address rows individually to update or delete them.您可能需要一个唯一键,以便您可以单独处理行以更新或删除它们。 Also row-based replication becomes very inefficient if your table has no primary key.如果您的表没有主键,基于行的复制也会变得非常低效。

This usually means you cannot partition the table by date_time , or even that you cannot partition the table at all.这通常意味着您无法按date_time对表进行分区,甚至根本无法对表进行分区。 But this isn't always a bad thing.但这并不总是一件坏事。 Partitioning doesn't necessarily give a great benefit.分区不一定会带来很大的好处。 Partitioning can even cause more complexity, because you may have queries that would be bound to search every partition anyway.分区甚至会导致更多的复杂性,因为您可能有查询,无论如何都要搜索每个分区。

Partitioning is not a cure-all, and frequently is a liability.分区不是万能的,而且经常是一种负担。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM