最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL與瀚高數(shù)據(jù)庫的范圍分區(qū)語法及實例代碼

 更新時間:2026年05月06日 10:52:12   作者:瀚高PG實驗室  
MySQL分區(qū)技術(shù)通過將大表分割成多個小分區(qū),提升查詢性能和管理效率,分區(qū)可獨立存儲、備份和優(yōu)化,適用于大數(shù)據(jù)集和時效性數(shù)據(jù)處理,這篇文章主要介紹了MySQL與瀚高數(shù)據(jù)庫的范圍分區(qū)語法及實例代碼的相關(guān)資料,需要的朋友可以參考下

環(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。

  1. 對于原生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)化。

  2. 對于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

  1. 原生RANGE分區(qū)實例
  1. 使用整型字段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
);
  1. 使用日期類型字段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ù),需要注意的是表達式必須有返回值。

  1. 插入數(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ù)

  1. RANGE COLUMNS分區(qū)實例
  1. 使用日期型字段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
);
  1. 插入數(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','女');
  1. 檢索各個分區(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的使用

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

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

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


    瀚高數(shù)據(jù)庫的范圍分區(qū)實例:

測試環(huán)境:Win10+hgdb企業(yè)版5.6.4

  1. 用表字段直接作為分區(qū)key
  1. 直接使用字段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','女');
  1. 檢索student表數(shù)據(jù)

  1. 使用表達式作為分區(qū)key
  1. 用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','女');
  1. 檢索student_birthday表數(shù)據(jù)

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

  2. 添加MINVALUE的分區(qū)
create table pminvalue partition of student_birthday for values from(minvalue) to(1981);
  1. 再次執(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 DBA 常用手冊小結(jié)

    MySQL DBA 常用手冊小結(jié)

    MySQL DBA 常用手冊小結(jié),使用mysql的朋友可以參考下。
    2011-11-11
  • MySQL正則表達式REGEXP使用詳解

    MySQL正則表達式REGEXP使用詳解

    MySQL中正則表達式通常被用來檢索或替換符合某個模式的文本內(nèi)容,根據(jù)指定的匹配模式匹配文中符合要求的特殊字符串,下面這篇文章主要給大家介紹了關(guān)于MySQL正則表達式REGEXP使用的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 詳解MySQL開啟遠程連接權(quán)限

    詳解MySQL開啟遠程連接權(quán)限

    這篇文章主要介紹了MySQL開啟遠程連接權(quán)限,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • MySql新手入門的基本操作匯總

    MySql新手入門的基本操作匯總

    MySQL是目前來說最好的 RDBMS應(yīng)用軟件之一,這篇文章主要給大家介紹了關(guān)于MySql基本操作的相關(guān)資料,非常適合mysql新手入門,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • MySQL中between...and的使用對索引的影響說明

    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ū)別整理

    這篇文章主要介紹了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)威示例指南

    這篇文章主要介紹了JDBC MySQL 連接 URL完整權(quán)威示例指南,本文結(jié)合實例代碼給大家講解的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • MySQL超詳細安裝配置超詳細圖文教程(親測有效)

    MySQL超詳細安裝配置超詳細圖文教程(親測有效)

    這篇文章詳細介紹了如何下載、配置和安裝MySQL,包括設(shè)置環(huán)境變量、初始化my.ini文件、開啟MySQL服務(wù)以及設(shè)置密碼,此外,還介紹了如何使用Navicat工具連接MySQL數(shù)據(jù)庫,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    mysql ID生成策略一般常用的有三種,包括自增、UUID 以及雪花算法,本文主要介紹了Mysql ID生成策略的三種方法選擇及優(yōu)缺點,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • MySQL中庫的基本操作指南(推薦!)

    MySQL中庫的基本操作指南(推薦!)

    MySQL這個數(shù)據(jù)庫是一個客戶端-服務(wù)器結(jié)構(gòu)的程序,下面這篇文章主要給大家介紹了關(guān)于MySQL中庫的基本操作指南,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02

最新評論

库车县| 韶山市| 鸡西市| 方正县| 扶余县| 牡丹江市| 塘沽区| 蒙自县| 舒兰市| 九寨沟县| 崇义县| 金昌市| 保康县| 民和| 贵定县| 桦南县| 腾冲县| 房山区| 崇阳县| 武宣县| 隆德县| 三门县| 大洼县| 金华市| 凌云县| 贡觉县| 旬邑县| 七台河市| 焦作市| 荔波县| 合阳县| 平凉市| 凉城县| 福建省| 东海县| 资兴市| 胶州市| 惠州市| 东阿县| 南平市| 宁夏|