MySQL分割槽表的一些問題

2021-10-12 00:24:16 字數 2592 閱讀 2697

mysql支援多種分割槽表。我們看到最多的是根據範圍進行分割槽,每個分割槽儲存落在某個範圍的記錄,分割槽表示式可以是列,也可以是包含列的表示式。

1. 分割槽表不能使用非分割槽的其他單獨的列做主鍵

2. 分割槽表新建時要定義好每乙個分割槽的詳細資訊

3. 分割槽表可以建立聯合主鍵,也可以不用主鍵,指定兩個列建立唯一索引(如果不建主鍵,在乙個列上建唯一索引也是不會成功的)

4. 分割槽表建立主鍵後,如果分割槽發生改變,比如新建或刪除了分割槽,插入語句不會受影響

5.  因為是聯合主鍵,如果需要單一欄位id 唯一,這時想單獨給id 設定 唯一索引是不會成功的,可以自行生成資料插入(比如uuid) 。

下面是幾種錯誤建表報錯示例:

create table sales (

dt datetime not null,

person_id varchar(64) not null

)engine=innodb default charset=utf8 partition by range (year(dt))

[sql] create table sales (

dt datetime not null,

person_id varchar(64) not null

)engine=innodb default charset=utf8 partition by range (year(dt))

[err] 1492 - for range partitions each partition must be defined

create table sales (

`id` int(20) not null auto_increment comment '主鍵',

dt datetime not null,

person_id varchar(64) not null,

primary key (`id`)

)engine=innodb default charset=utf8 partition by range (year(dt))

( partition p_2019 values less than (2019),

partition p_2020 values less than (2020));

)[sql] create table sales (

`id` int(20) not null auto_increment comment '主鍵',

dt datetime not null,

person_id varchar(64) not null,

primary key (`id`)

)engine=innodb default charset=utf8 partition by range (year(dt))

( partition p_2019 values less than (2019),

partition p_2020 values less than (2020));

[err] 1503 - a primary key must include all columns in the table's partitioning function

正確的建表語句

create table sales (

`id` int(20) not null auto_increment comment '主鍵',

dt datetime not null,

person_id varchar(64) not null,

primary key (`id`,dt)

)engine=innodb default charset=utf8 partition by range (year(dt))

( partition p_2019 values less than (2019), //注:值小於2019的存到p_2019分割槽,也就是存的資料是2023年或之前的

partition p_2020 values less than (2020),

partition p_2021 values less than (2021)

);

alter table sales  add partition (partition p_2022 values less than (2022));  //按範圍分分割槽

alter table sales  add partition (partition p6 values in ('technique leader') ); //也可以指定按具體值分分割槽

alter table sales  drop partition  p_2019

select * from sales partition(p_2019);

select * from sales  //檢視所有資料

個人覺得,分割槽表不如直接分表來的乾脆

分割槽表的一些操作

一 建立表空間 create tablespace dinya space01 datafile f user data1.dbf size 50m autoextend on next 50m maxsize 20480m extent management local create tables...

對分割槽表的一些總結

分割槽表分為 範圍分割槽 partition by range 列表分割槽 partition by list hash分割槽 partition by hash 有多少個分割槽就有多少個segment 其實hash分割槽最大的好處在於,將資料根據一定的hash演算法,均勻分布到不同的分割槽中去,避...

mysql 一些問題

1 中文亂碼 問題 推薦用 uft 8 編碼 適配一切介面,mysql中 發生中文亂碼時 開啟mysql 安裝路徑 更改後 重啟mysql 服務即可,有時也需要 重新匯入資料庫 可能是與 source 檔案時 編碼沒有設定好 client password your password port 33...