天天看點

mysql 分表和分區_mysql分表和分區簡述

1)分表

目的:提升對海量資料的進行存取操作的效率

選擇合适的分表政策,确定分表政策後,當進行資料存取操作時,需求确定要到那張表裡去查詢資料

分表字段:網際網路的系統使用使用者id字段

資料是放到哪個表:分表的字段%分表的數量

政策:

根據範圍分區(表ID 1~200 db1 表ID 201~200 db2 表ID m~n dbn)

範圍應該連續但是不連續,使用PAPRTION BY RANGE VALUES LESS THAN關鍵字

不使用COLUMNS關鍵字時,RANGE中必須為整數字段名或傳回确定整數字段的函數

使用columns關鍵字,可定義非integer範圍及多列範圍,columns中隻能是列名,多列範圍必須呈遞增趨勢

哈希拆分(表 ID%db數量 db取模)

根據配置檔案來分表

範圍分區例子:

1)根據數值字段分區

CREATE TABLE test (

id INT NOT NULL,

testid INT NOT NULL

)

PARTITION BY RANGE (testid) (

PARTITION p0 VALUES LESS THAN (100000),

PARTITION p1 VALUES LESS THAN (200000),

PARTITION p2 VALUES LESS THAN (300000),

PARTITION p3 VALUES LESS THAN MAXVALUE

);

2)根據時間字段(timestamp)分區

create table testb (

id bigint unsigned not null,

updated_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

)

partition by range (unix_timestamp(updated_time) ) (

partition p0 values less than (unix_timestamp(‘2017-01-01 00:00:00‘)),

partition p1 values less than (unix_timestamp(‘2017-03-01 00:00:00‘)),

partition p2 values less than (unix_timestamp(‘2017-06-01 00:00:00‘)),

partition p3 values less than (maxvalue)

);

3)根據時間字段(date datetime)分區

create table testc (

id bigint unsigned not null,

updated_time datetime not null

)

partition by range columns(updated_time) (

partition p0 values less than (‘2017-01-01 00:00:00‘),

partition p1 values less than (‘2017-03-01 00:00:00‘),

partition p2 values less than (‘2017-06-01 00:00:00‘),

partition p3 values less than (maxvalue)

);

4)根據多個字段分區

create table testd (

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by range columns(region,statu) (

partition p0 values less than (1000100,1),

partition p1 values less than (2000100,2),

partition p2 values less than (3000100,3),

partition p3 values less than (4000100,4),

partition p4 values less than (maxvalue,maxvalue)

);

list分區

根據具體數值分區,每個分區值不能重複,使用partiton by list values in關鍵字

不使用COLUMNS關鍵字時,list中必須為整數字段名或傳回确定整數字段的函數

create table teste(

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by list columns(statu) (

partition p0 values in (1,3,5),

partition p1 values in (2,4,6),

partition p2 values in (7,8,9),

partition p3 values in (0,10)

);

hash分區

在确定的數目分區中平均分布,hash必須為整數字段名或傳回确定整數字段的函數

create table testf(

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by hash(statu)

partitions 4;

create table testg(

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by linear hash(statu)

partitions 4;

key分區

表中存在主鍵或唯一索引時,可忽略key中的列名

create table testh1(

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by key(statu)

partitions 4;

create table testh2(

id bigint unsigned not null,

region int not null,

statu smallint not null

)

partition by linear key(statu)

partitions 4;

子分區

對分區表中每個分區再進行分區,每個分區的子分區數必須相同,子分區在全表中名字唯一

CREATE TABLE testf1 (id int, updated_time date)

PARTITION BY RANGE(year(updated_time) )

SUBPARTITION BY hash(TO_DAYS(updated_time))

SUBPARTITIONS 1 (

PARTITION p0 VALUES LESS THAN (1949),

PARTITION p1 VALUES LESS THAN (1959),

PARTITION p2 VALUES LESS THAN (1969),

PARTITION p3 VALUES LESS THAN (1979),

PARTITION p4 VALUES LESS THAN (1989),

PARTITION p5 VALUES LESS THAN (1999),

PARTITION p6 VALUES LESS THAN (2009),

PARTITION p7 VALUES LESS THAN (2019),

PARTITION p8 VALUES LESS THAN MAXVALUE

);

2)分庫

目的:降低對海量資料的進行高并發現象的幾率

資料是放到哪個庫:分庫的字段%資料庫的數量

3)分表後分庫

目的:同時提升存儲效率和降低高并發現象

中間變量:關鍵字段(userid)%(資料庫數量*每個庫的表數量)

庫序号:取整:中間變量/每個庫的表數量

表序号:取餘:中間變量/每個庫的表數量

假設資料庫有100個,每1個庫有256張表,使用者的userid=28800,按照上面政策,則:

中間變量:28800%(100*256)=1

庫序号:取整 1/1024=0

表序号:取餘 1/1024=1

是以,userid=28800的記錄,會被存儲到底0個庫第1個表中。

4)空節點查詢

一般查詢:查詢->到各個層級去查詢,所有結果都不存在->再到資料庫中查詢->傳回結果給前端

避免空節點查詢:查詢->直接到映射表(存在的記錄)查詢->再各個層級進行查詢->再到資料庫中查詢

節點容災或過載保護:當查詢資料庫時超過門檻值後,資料庫拒絕提供服務,将多餘的請求分發到其他閑的節點上。

mysql分表和分區簡述

标簽:請求   字段   服務   資料庫   使用者   current   索引   stat   存在

本條技術文章來源于網際網路,如果無意侵犯您的權益請點選此處回報版權投訴

本文系統來源:http://www.cnblogs.com/NiceTime/p/6662933.html