mysql 建立分割槽索引嗎 mysql建立分割槽索引

2021-10-18 10:06:16 字數 2570 閱讀 7585

該樓層疑似違規已被系統摺疊 隱藏此樓檢視此樓

mysql建立分割槽索引

(一)分割槽表:

把所有的資料放在乙個表中,但是物理儲存資料會根據一定規則存放到不同的檔案中

(二)什麼時候使用分割槽表?

資料比較大時候,數以億記或者數以tb記的資料,如果使用索引在空間和維護消耗巨大,甚至索引沒有效果了.

例子:檢視是否支援分割槽

show variables like '%partition%';

建立乙個普通表

create table no_part_tab(

id int not null,

username varchar(50) not null,

d_date date not null

建立儲存過程插入資料

delimiter #

create procedure no_part_tab_pro()

begin

declare i int;

set i=1;

while i<=10000 do

insert into no_part_tab values(i, concat('no_part',i), adddate('2007‐01‐01', (rand(i)*36520)mod 3652));

set i=i+1;

end while;

end#

call no_part_tab_pro();

建立乙個分割槽表

create table part_tab(

id int not null,

username varchar(50) not null,

d_date date not null)

partition by range(year(d_date))(

partition p0 values less than(2007),

partition p1 values less than(2008),

partition p2 values less than(2009),

partition p3 values less than(2010),

partition p4 values less than(2011),

partition p5 values less than(2012),

partition p6 values less than(2013),

partition p7 values less than(2014),

partition p8 values less than(2015),

partition p9 values less than(2016),

partition p10 values less than maxvalue

partition p10 values less than maxvalue

建立儲存過程插入資料

delimiter #

create procedure part_tab_pro()

begin

declare i int;

set i=1;

while i<=10000 do

insert into part_tab values(i, concat('no_part',i), adddate('2007‐01‐01', (rand(i)*36520) mod 3652));

set i=i+1;

end while;

end#

call part_tab_pro();

分別查詢兩個表中資料,使用explain partitions

explain partitions select * from no_part_tab where d_date>date '2007‐01‐01' and d_date

explain partitions select * from part_tab where d_date>date '2007‐01‐01' and d_date

(三)常見的分割槽:

1.range分割槽

2.list分割槽:

create table list_tab(

id int not null,

username varchar(20) not null

partition by list(id)(

partition p0 values in(1,2,3), #區間值不能重複

partition p1 values in(4,5,6)

3.hash分割槽

create table hash_tab(

id int not null,

username varchar(20) not null

partition by hash(id)

partitions 4;

4.key分割槽

create table key_tab(

id int not null,

username varchar(20) not null

partition by key(id)

partitions 3;

mysql建立分割槽索引 mysql建立分割槽索引

該樓層疑似違規已被系統摺疊 隱藏此樓檢視此樓 mysql建立分割槽索引 一 分割槽表 把所有的資料放在乙個表中,但是物理儲存資料會根據一定規則存放到不同的檔案中 二 什麼時候使用分割槽表?資料比較大時候,數以億記或者數以tb記的資料,如果使用索引在空間和維護消耗巨大,甚至索引沒有效果了.例子 檢視是...

mysql建立最小分割槽 mysql 建立分割槽

list分割槽 鍵值通過自定義的list來確定寫入到哪個分割槽中。優勢 支援int,時間,varchar等值 劣勢 需要自己寫相應資料的從屬 寫入或者查詢到哪個分割槽 即後期若分割槽條件修改需要再配置。create table t test unid int auto increment uuid ...

mysql分割槽 索引

mysql分割槽 mysql索引 1 新增索引 create index index name on my table column name 2 根據索引查詢 具體查詢 select from table name where column 1 column 2 為column 1建立了索引 或者...