MySQL與瀚高數(shù)據(jù)庫的范圍分區(qū)語法及實例代碼
環(huán)境
系統(tǒng)平臺:Microsoft Windows (64-bit) 10
版本:5.6.4
文檔用途
當表中的數(shù)據(jù)量不斷增大,查詢數(shù)據(jù)的速度就會變慢,應(yīng)用程序的性能就會下降,這時就應(yīng)該考慮對表進行分區(qū)。
表進行分區(qū)后,邏輯上表仍然是一張完整的表,只是將表中的數(shù)據(jù)分類存放到多個表空間的物理文件上,
這樣查詢數(shù)據(jù)時,不至于每次都掃描整張表。
分區(qū)類型分為范圍分區(qū)(range partition)、列表分區(qū)(list partition)、哈希分區(qū)(hash partition)等。
本次主要介紹MySQL和瀚高數(shù)據(jù)庫的范圍分區(qū)。
MySQL的范圍分區(qū):
含義:基于屬于一個給定連續(xù)區(qū)間的列值,把多行分配給分區(qū)。主鍵、約束、索引在分區(qū)表中創(chuàng)建。
關(guān)鍵字:RANGE、VALUES LESS THAN
RANGE又可分為原生RANGE和RANGE COLUMNS。
對于原生RANGE分區(qū),分區(qū)字段必須是整型或者轉(zhuǎn)換為整型,如果分區(qū)字段是日期類型的字段,那么就必須將日期類型的字段轉(zhuǎn)換成整型類型。對于日期類型的轉(zhuǎn)換,優(yōu)化器只支持year(),to_days,to_seconds,unix_timestamp()函數(shù)的轉(zhuǎn)換,其他的并不支持,也就是說,在按日期字段分區(qū)的時候,如果不是使用上述幾個函數(shù)轉(zhuǎn)換的,查詢優(yōu)化器將無法對相關(guān)查詢進行優(yōu)化。
對于RANGE COLUMNS分區(qū),不可以使用表達式,只能使用列名;接受一個或多個字段的列表;分區(qū)列是不限制于數(shù)字列的;字符串、DATE和DATETIME列也可以使用在分區(qū)列。
注:分區(qū)字段必須包含在主鍵內(nèi)
語法:
原生RANGE分區(qū)語法:
C
REATE TABLE table_name(column_name data_type)
PARTITION BY RANGE(整型column_list|整型轉(zhuǎn)換函數(shù)(column_list)) (
PARTITION partition_name VALUES LESS THAN (value_list)[,
PARTITION partition_name VALUES LESS THAN (value_list)][,
...]
)
column_list:
column_name[, column_name][, ...]
value_list:
value[, value][, ...]column_list是一個或多個列名, value_list是和column_list相對應(yīng)的一個或多個值
RANGE COLUMNS分區(qū)語法:
CREATE TABLE table_name(column_name data_type)
PARTITION BY RANGE COLUMNS(column_list) (
PARTITION partition_name VALUES LESS THAN (value_list)[,
PARTITION partition_name VALUES LESS THAN (value_list)][,
...]
)
column_list:
column_name[, column_name][, ...]
value_list:
value[, value][, ...]column_list是一個或多個列名, value_list是和column_list相對應(yīng)的一個或多個值
注:在value_list中有一個最大值MAXVALUE,可以創(chuàng)建MAXVALUE的分區(qū),存儲超出范圍的數(shù)據(jù)
刪除分區(qū)語法:
ALTER TABLE table_name DROP PARTITION partition_name;
瀚高數(shù)據(jù)庫的范圍分區(qū):
含義:支持單列、多列分區(qū),例如:RANGE(x,y)
關(guān)鍵字:RANGE、FOR VALUES FROM(……) TO(……)
注:主鍵、約束、索引等不能在分區(qū)表中創(chuàng)建,目前只能在各個分區(qū)中創(chuàng)建。分區(qū)表只是一個表結(jié)構(gòu)。
語法:
-- 主表
CREATE TABLE table_name ( column_name data_type )
PARTITION BY RANGE ( { column_name } [, ... ] )
-- 子表
CREATE TABLE table_name
PARTITION OF parent_table
FOR VALUES
FROM ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] )
TO ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] )注:MINVALUE是最小值,MAXVALUE是最大值,為了防止插入范圍以外的數(shù)據(jù)時報錯,可以創(chuàng)建兩個分區(qū),
分別是MINVALUE和MAXVALUE的分區(qū)。分區(qū)字段的值不能為NULL。
刪除分區(qū)語法:
-- 把分區(qū)修改成普通表 ALTER TABLE table_name DETACH PARTITION partition_name; -- 刪除分區(qū) DROP TABLE partition_name;
詳細信息
下面通過實例進行詳細說明:
MySQL的范圍分區(qū)實例:
測試環(huán)境:Win10+MySQL5.7
- 原生RANGE分區(qū)實例
- 使用整型字段score作為分區(qū)key
create table student(
sid integer auto_increment,
sname varchar(20),
score integer,
birthday DATETIME,
ssex varchar(10),
primary key(sid,score) -- score是分區(qū)字段,必須作為主鍵
)
partition by range(score)(
partition p0 values less than(10),-- 分區(qū)p0
partition p1 values less than(20),-- 分區(qū)p1
partition p2 values less than(30),-- 分區(qū)p2
partition p3 values less than(40),-- 分區(qū)p3
partition p4 values less than(50),-- 分區(qū)p4
partition p5 values less than(60),-- 分區(qū)p5
partition p6 values less than(70),-- 分區(qū)p6
partition p7 values less than(80) -- 分區(qū)p7
);- 使用日期類型字段birthday作為分區(qū)key,需要轉(zhuǎn)換成整型值使用
create table student_birthday(
sid integer auto_increment,
sname varchar(20),
score integer,
birthday DATETIME,
ssex varchar(10),
primary key(sid,birthday)
)
partition by range(year(birthday))(
partition p1981 values less than(1983),-- 分區(qū)p1981
partition p1983 values less than(1985),-- 分區(qū)p1983
partition p1985 values less than(1987),-- 分區(qū)p1985
partition p1987 values less than(1989) -- 分區(qū)p1987
);注:以year(birthday)表達式(計算學生的出生日期)作為范圍分區(qū)依據(jù),需要注意的是表達式必須有返回值。
- 插入數(shù)據(jù)
-- student表中插入數(shù)據(jù)
insert into student(sname,score,birthday,ssex)values('小趙',5,'19830101','男');
insert into student(sname,score,birthday,ssex)values('小錢',10,'19830101','女');
insert into student(sname,score,birthday,ssex)values('小孫',15,'19840101','男');
insert into student(sname,score,birthday,ssex)values('小李',25,'19850101','女');
insert into student(sname,score,birthday,ssex)values('小周',35,'19860101','男');
insert into student(sname,score,birthday,ssex)values('小吳',45,'19870101','女');
insert into student(sname,score,birthday,ssex)values('小鄭',55,'19870101','男');
insert into student(sname,score,birthday,ssex)values('小王',75,'19840101','男');
insert into student(sname,score,birthday,ssex)values('小馮',79,'19850101','女');
-- student_birthday表中插入數(shù)據(jù)
insert into student_birthday(sname,score,birthday,ssex)values('小趙',5,'19830101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小錢',10,'19830101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小孫',15,'19840101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小李',25,'19850101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小周',35,'19860101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小吳',45,'19870101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小鄭',55,'19870101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小王',75,'19840101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小馮',79,'19850101','女');
4) 檢索各個分區(qū)中的記錄數(shù)
-- student表中各分區(qū)的記錄數(shù)
SELECT table_name,partition_name,partition_description,table_rows FROM information_schema.partitions
WHERE partition_name IS NOT NULL and table_schema = 'testpar'
and table_name='student';
注:table_rows是每個分區(qū)的記錄數(shù)
-- student_birthday表中各分區(qū)的記錄數(shù) SELECT table_name,partition_name,partition_description,table_rows FROM information_schema.partitions WHERE partition_name IS NOT NULL and table_schema = 'testpar' and table_name='student_birthday';

注:table_rows是每個分區(qū)的記錄數(shù)
- RANGE COLUMNS分區(qū)實例
- 使用日期型字段birthday作為分區(qū)key
create table student_birthday_range(
sid integer auto_increment,
sname varchar(20),
score integer,
birthday DATETIME,
ssex varchar(10),
primary key(sid,birthday)
)
partition by range columns(birthday)(
partition p19830101 values less than('19840101'),-- 分區(qū)p19830101
partition p19840101 values less than('19850101'),-- 分區(qū)p19840101
partition p19850101 values less than('19860101'),-- 分區(qū)p19850101
partition p19860101 values less than('19870101'),-- 分區(qū)p19860101
partition p19870101 values less than('19880101') -- 分區(qū)p19870101
);- 插入數(shù)據(jù)
insert into student_birthday_range(sname,score,birthday,ssex)values('小趙',5,'19830101','男');
insert into student_birthday_range(sname,score,birthday,ssex)values('小錢',10,'19830101','女');
insert into student_birthday_range(sname,score,birthday,ssex)values('小孫',15,'19840101','男');
insert into student_birthday_range(sname,score,birthday,ssex)values('小李',25,'19850101','女');
insert into student_birthday_range(sname,score,birthday,ssex)values('小周',35,'19860101','男');
insert into student_birthday_range(sname,score,birthday,ssex)values('小吳',45,'19870101','女');
insert into student_birthday_range(sname,score,birthday,ssex)values('小鄭',55,'19870101','男');
insert into student_birthday_range(sname,score,birthday,ssex)values('小王',75,'19840101','男');
insert into student_birthday_range(sname,score,birthday,ssex)values('小馮',79,'19850101','女');- 檢索各個分區(qū)的記錄數(shù)
SELECT table_name,partition_name,partition_description,table_rows FROM information_schema.partitions WHERE partition_name IS NOT NULL and table_schema = 'testpar' and table_name='student_birthday_range';

3. MAXVALUE的使用
- 在表中插入一條超出范圍的數(shù)據(jù)(19880101超出分區(qū)的最大值),報下面錯誤

- 添加MAXVALUE的分區(qū)
alter table student_birthday_range add partition(partition pmaxvalue values less than maxvalue);
- 再次執(zhí)行上面的插入語句,執(zhí)行成功

- 查詢表數(shù)據(jù),19880101的數(shù)據(jù)正常插入了

瀚高數(shù)據(jù)庫的范圍分區(qū)實例:
測試環(huán)境:Win10+hgdb企業(yè)版5.6.4
- 用表字段直接作為分區(qū)key
- 直接使用字段score作為分區(qū)key
-- 創(chuàng)建分區(qū)表
create table student(
sid serial,
sname varchar(20),
score integer,
birthday timestamp(0),
ssex varchar(10)
)partition by range(score);
-- 創(chuàng)建分區(qū)
create table p0 partition of student for values from(0) to(10);
create table p1 partition of student for values from(10) to(20);
create table p2 partition of student for values from(20) to(30);
create table p3 partition of student for values from(30) to(40);
create table p4 partition of student for values from(40) to(50);
create table p5 partition of student for values from(50) to(60);
create table p6 partition of student for values from(60) to(70);
create table p7 partition of student for values from(70) to(80);
-- 各個分區(qū)添加主鍵(有約束、索引等的情況,也要添加)
alter table p0 add constraint p0_pkey_sid primary key(sid);
alter table p1 add constraint p1_pkey_sid primary key(sid);
alter table p2 add constraint p2_pkey_sid primary key(sid);
alter table p3 add constraint p3_pkey_sid primary key(sid);
alter table p4 add constraint p4_pkey_sid primary key(sid);
alter table p5 add constraint p5_pkey_sid primary key(sid);
alter table p6 add constraint p6_pkey_sid primary key(sid);
alter table p7 add constraint p7_pkey_sid primary key(sid);
-- 插入數(shù)據(jù)
insert into student(sname,score,birthday,ssex)values('小趙',5,'19830101','男');
insert into student(sname,score,birthday,ssex)values('小錢',10,'19830101','女');
insert into student(sname,score,birthday,ssex)values('小孫',15,'19840101','男');
insert into student(sname,score,birthday,ssex)values('小李',25,'19850101','女');
insert into student(sname,score,birthday,ssex)values('小周',35,'19860101','男');
insert into student(sname,score,birthday,ssex)values('小吳',45,'19870101','女');
insert into student(sname,score,birthday,ssex)values('小鄭',55,'19870101','男');
insert into student(sname,score,birthday,ssex)values('小王',75,'19840101','男');
insert into student(sname,score,birthday,ssex)values('小馮',79,'19850101','女');- 檢索student表數(shù)據(jù)

- 使用表達式作為分區(qū)key
- 用date_part(‘year’,birthday)表達式取得年作為分區(qū)key
-- 創(chuàng)建分區(qū)表,用date_part('year',birthday)表達式取得年
create table student_birthday(
sid serial,
sname varchar(20),
score integer,
birthday timestamp(0),
ssex varchar(10)
)partition by range(date_part('year',birthday));
-- 創(chuàng)建分區(qū)
create table p1981 partition of student_birthday for values from(1981) to(1983);
create table p1983 partition of student_birthday for values from(1983) to(1985);
create table p1985 partition of student_birthday for values from(1985) to(1987);
create table p1987 partition of student_birthday for values from(1987) to(1989);
-- 各個分區(qū)添加主鍵(有約束、索引等的情況,也要添加)
alter table p1981 add constraint p1981_pkey_sid primary key(sid);
alter table p1983 add constraint p1983_pkey_sid primary key(sid);
alter table p1985 add constraint p1985_pkey_sid primary key(sid);
alter table p1987 add constraint p1987_pkey_sid primary key(sid);
-- 插入數(shù)據(jù)
insert into student_birthday(sname,score,birthday,ssex)values('小趙',5,'19830101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小錢',10,'19830101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小孫',15,'19840101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小李',25,'19850101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小周',35,'19860101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小吳',45,'19870101','女');
insert into student_birthday(sname,score,birthday,ssex)values('小鄭',55,'19870101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小王',75,'19840101','男');
insert into student_birthday(sname,score,birthday,ssex)values('小馮',79,'19850101','女');- 檢索student_birthday表數(shù)據(jù)

- MINVALUE的使用
- 插入19800101的數(shù)據(jù),最小分區(qū)的最小值是1981,1980在范圍之外,報錯

- 添加MINVALUE的分區(qū)
create table pminvalue partition of student_birthday for values from(minvalue) to(1981);
- 再次執(zhí)行上面的插入語句,19800101的數(shù)據(jù)插入成功
至此,范圍分區(qū)的詳細實例介紹完畢。
總結(jié)
到此這篇關(guān)于MySQL與瀚高數(shù)據(jù)庫的范圍分區(qū)語法及實例代碼的文章就介紹到這了,更多相關(guān)MySQL與瀚高數(shù)據(jù)庫范圍分區(qū)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中between...and的使用對索引的影響說明
這篇文章主要介紹了MySQL中between...and的使用對索引的影響說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
MySQL 4.1/5.0/5.1/5.5/5.6各版本的主要區(qū)別整理
這篇文章主要介紹了MySQL 4.1/5.0/5.1/5.5/5.6各版本的主要區(qū)別整理,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08
JDBC MySQL 連接 URL完整權(quán)威示例指南
這篇文章主要介紹了JDBC MySQL 連接 URL完整權(quán)威示例指南,本文結(jié)合實例代碼給大家講解的非常詳細,感興趣的朋友跟隨小編一起看看吧2026-03-03

