Mysql

有沒有辦法按範圍列對 mysql 表進行分區,其中有問題的 2 列是 datetime 和 bigint?

  • March 26, 2019

我需要將一個表劃分為 4 個分區,p0 到 p3:p0 的所有行的“id”=0,p1 的所有行的“id”=1 和“doj”在 2 月之前,p2 的所有行都有“id” =1 和三月前的“doj”,與其他人分割“未來”。id 的值保證始終為 0 或 1。

我試圖想出一個解決方案,以下是我得出的結論:

create table temp
(id int not null primary key,
doj datetime not null primary key)
partition by range columns(id,month(doj))
(partition p0 values less than (1, 13),
partition p1 values less than (2, 2),
partition p2 values less than (2, 3),
partition p3 values less than(maxvalue, maxvalue));

當我嘗試執行上述 sql 命令時出現以下錯誤:

您的 SQL 語法有錯誤;檢查與您的 MySQL 伺服器版本相對應的手冊,以在第 4 行的 ‘(doj)) 附近使用正確的語法(分區 p0 值小於(1,最大值),分區 p1 值小於 t’

任何關於如何以上述方式對錶進行分區的見解將不勝感激。

使用的 MySql 版本為:5.6.19-67.0-log

解決方案可以是:

create table temp
(id int not null primary key,
doj datetime not null primary key,
doj_month tinyint) -- add field for partitioning
partition by range columns(id,doj_month)
(partition p0 values less than (1, 13),
partition p1 values less than (2, 2),
partition p2 values less than (2, 3),
partition p3 values less than(maxvalue, maxvalue));

-- create triggers for additional field calculations
create trigger tr_i_temp
for insert on temp
for each row
set NEW.doj_month = month(NEW.doj);

create trigger tr_u_temp
for update on temp
for each row
set NEW.doj_month = month(NEW.doj);

但是您必須記住,存在很多不觸發觸發器的情況(級聯操作、載入數據等)。如果您使用此類操作,您必須“手動”更新此附加欄位(例如,更新重新分配doj欄位的查詢)。

引用自:https://dba.stackexchange.com/questions/233100