首先聲明下我這邊使用的mysql版本是5.7.29版本,當(dāng)然下面的問題我這邊也是基于這個版本。這里因為沒有考證其他版本是否也會有這些問題,可自行官方文檔來查閱資料
一個唯一鍵必須包含表分區(qū)函數(shù)所有的列,根據(jù)這個錯誤提示我們大概就知道怎么處理,但是why?我們還是沉下心來看下官網(wǎng)文檔
關(guān)于主鍵和唯一鍵的限制
MySQL :: MySQL 5.7 Reference Manual :: 22.6.1 Partitioning Keys, Primary Keys, and Unique Keys
his section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have.
In other words,?every unique key on the table must use every column in the table's partitioning expression. (This also includes the table's primary key, since it is by definition a unique key. This particular case is discussed later in this section.)?
這段話大致概括的意思就是說我們的分區(qū)表,主鍵和唯一鍵必須包含分區(qū)表達(dá)式的所有列。反過來講,分區(qū)表達(dá)式中的列至少有一個是聯(lián)合主鍵和聯(lián)合唯一鍵的某一列。
用官網(wǎng)的例子解釋下
這個是錯誤的創(chuàng)建語句
CREATE TABLE t1 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
UNIQUE KEY (col1, col2)
)
PARTITION BY HASH(col3)
PARTITIONS 4;
CREATE TABLE t2 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
UNIQUE KEY (col1),
UNIQUE KEY (col3)
)
PARTITION BY HASH(col1 + col3)
PARTITIONS 4;
執(zhí)行后,錯誤提示
?A PRIMARY KEY must include all columns in the table's partitioning function
根據(jù)上面官網(wǎng)大致概況的意思分析下上述分區(qū)表創(chuàng)建錯誤原因
t1表中UNIQUE KEY (col1, col2) 聯(lián)合唯一鍵沒有包含分區(qū)表達(dá)式HASH(col3) col3這一列,所以報錯
t2標(biāo)中?UNIQUE KEY (col1), UNIQUE KEY (col3) 兩個唯一鍵沒有包含分區(qū)表達(dá)式HASH(col1 + col3) 所有的列
這個是糾正后的創(chuàng)建語句
--col3是聯(lián)合唯一鍵的一列字段
CREATE TABLE t1 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
UNIQUE KEY (col1, col2, col3)
)
PARTITION BY HASH(col3)
PARTITIONS 4;
--UNIQUE KEY (col1, col3) 這個唯一鍵必須包含分區(qū)表達(dá)式所有的列字段
CREATE TABLE t2 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
UNIQUE KEY (col1, col3)
)
PARTITION BY HASH(col1 + col3)
PARTITIONS 4;
Since every primary key is by definition a unique key, this restriction also includes the table's primary key,
對于主鍵也是同樣的規(guī)則約束
這里就不用分析錯誤原因了同上面的聯(lián)合唯一鍵
CREATE TABLE t5 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
PRIMARY KEY(col1, col2)
)
PARTITION BY HASH(col3)
PARTITIONS 4;
CREATE TABLE t6 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
PRIMARY KEY(col1, col3),
UNIQUE KEY(col2)
)
PARTITION BY HASH( YEAR(col2) )
PARTITIONS 4;
完整示例演示解決過程
那么下來我們就演示一個完整的例子來如何處理這種問題
創(chuàng)建并初始化訂單表
CREATE TABLE test_order (
id INT NOT NULL ,
order_no varchar(20) not null ,
create_time DATE NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY(order_no)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test_order VALUES(1,'001','2023-01-01');
insert into test_order VALUES(2,'002','2023-02-01');
insert into test_order VALUES(3,'003','2023-03-01');
根據(jù)時間字段做分區(qū)
alter table test_order partition by range (TO_DAYS(CREATE_TIME)) (
partition p_202301 values less than (TO_DAYS('2023-02-01')),
partition p_202302 values less than (TO_DAYS('2023-03-01')),
partition p_202303 values less than (TO_DAYS('2023-04-01'))
);
?如何解決呢
--重建主鍵
alter table test_order DROP PRIMARY KEY, ADD PRIMARY key(ID,CREATE_TIME);
--重建唯一鍵(刪除原來的唯一索引,重新生成唯一鍵)
ALTER TABLE test_order DROP INDEX order_no, ADD UNIQUE KEY (`order_no`, `create_time`);
?看到這里,我們創(chuàng)建分區(qū)的sql已經(jīng)執(zhí)行成功了。
找到對應(yīng)數(shù)據(jù)目錄,我們也看到分區(qū)后數(shù)據(jù)idb文件的存儲文件,表分區(qū)創(chuàng)建好后,對應(yīng)的索引也會分區(qū)
文章來源:http://www.zghlxwxcb.cn/news/detail-736536.html
這里強(qiáng)調(diào)一點,生產(chǎn)環(huán)境如果原來的表已經(jīng)存在業(yè)務(wù)數(shù)據(jù),并且業(yè)務(wù)量很大的情況下,我們還是要停機(jī)來處理創(chuàng)建表分區(qū),這個需要注意謹(jǐn)慎操作文章來源地址http://www.zghlxwxcb.cn/news/detail-736536.html
到了這里,關(guān)于【mysql表分區(qū)系】mysql創(chuàng)建分區(qū)表提示A PRIMARY KEY must include all columns in the table‘s partitioning function的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!