下面講講關于什么是MySQL中RANGE分區(qū),文字的奧妙在于貼近主題相關。所以,閑話就不談了,我們直接看下文吧,相信看完什么是Mysql中RANGE分區(qū)這篇文章你一定會有所受益。
創(chuàng)新互聯(lián)主營米易網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件開發(fā)公司,米易h5重慶小程序開發(fā)搭建,米易網(wǎng)站營銷推廣歡迎米易等地區(qū)企業(yè)咨詢
通過范圍的方式進行分區(qū), 為每個分區(qū)給出一定的范圍, 范圍必須是連續(xù)的并且不能重復, 使用VALUES LESS THAN操作符
讓我們先來創(chuàng)建一個range分區(qū)的表
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT NOT NULL,
store_id INT NOT NULL
)
PARTITION BY RANGE (store_id) (
PARTITION p0 VALUES LESS THAN (6),
PARTITION p1 VALUES LESS THAN (11),
PARTITION p2 VALUES LESS THAN (16),
PARTITION p3 VALUES LESS THAN (21)
);
store_id 小于6的, 會被放入第一個分區(qū), 小于11的會放入第二個分區(qū)。如果我的store_id大于21怎么辦呢?
, 所以我們得修改一下這個創(chuàng)建分區(qū)的方式。
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT NOT NULL,
store_id INT NOT NULL
)
PARTITION BY RANGE (store_id) (
PARTITION p0 VALUES LESS THAN (6),
PARTITION p1 VALUES LESS THAN (11),
PARTITION p2 VALUES LESS THAN (16),
PARTITION p3 VALUES LESS THAN (21),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
);
增加了一個MAXVALUE, MAXVALUE的意思是, 大于21的數(shù)據(jù)都會放入這個分區(qū), 當然, 還有另外一種方式去避免這個問題, 就是在INSERT的時候增加IGNORE關鍵字。
分區(qū)鍵類型為時間轉(zhuǎn)時間戳
可以使用UNIX-TIMESTAMP()
CREATE TABLE quarterly_report_status (
report_id INT NOT NULL,
report_status VARCHAR(20) NOT NULL,
report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) (
PARTITION p0 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-01-01 00:00:00') ),
PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-04-01 00:00:00') ),
PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-07-01 00:00:00') ),
PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-10-01 00:00:00') ),
PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-01-01 00:00:00') ),
PARTITION p5 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-04-01 00:00:00') ),
PARTITION p6 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-07-01 00:00:00') ),
PARTITION p7 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-10-01 00:00:00') ),
PARTITION p8 VALUES LESS THAN ( UNIX_TIMESTAMP('2010-01-01 00:00:00') ),
PARTITION p9 VALUES LESS THAN (MAXVALUE)
);
除了UNIX_TIMESTAMP外,其他涉及到時間戳的表達式都是不被允許的
基于時間數(shù)字的分區(qū)
CREATE TABLE members (
firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
username VARCHAR(16) NOT NULL,
email VARCHAR(35),
joined DATE NOT NULL
)
PARTITION BY RANGE(YEAR(joined) ) (
PARTITION p0 VALUES LESS THAN (1960),
PARTITION p1 VALUES LESS THAN (1970),
PARTITION p2 VALUES LESS THAN (1980),
PARTITION p3 VALUES LESS THAN (1990),
PARTITION p4 VALUES LESS THAN MAXVALUE
);
對于以上什么是Mysql中RANGE分區(qū)相關內(nèi)容,大家還有什么不明白的地方嗎?或者想要了解更多相關,可以繼續(xù)關注我們的行業(yè)資訊板塊。