講講MySQL的分割槽

2021-10-09 06:36:28 字數 4346 閱讀 6051

分割槽劣勢

分割槽劣勢

隨著mysql越來越流行,mysql裡面的儲存的資料也越來越大。在日常的工作中,我們經常遇到一張表裡面儲存了上億甚至過十億的記錄。這些表裡面儲存了大量的歷史記錄。 對於這些歷史資料的清理是乙個非常頭疼事情,由於所有的資料都乙個普通的表裡。所以只能是啟用乙個或多個帶where條件的delete語句去刪除(一般where條件是時間)。 這對資料庫的造成了很大壓力。即使我們把這些刪除了,但底層的資料檔案並沒有變小。面對這類問題,最有效的方法就是在使用分割槽表。最常見的分割槽方法就是按照時間進行分割槽。 分割槽乙個最大的優點就是可以非常高效的進行歷史資料的清理。

同時,分割槽之後表資料檔案分為多個,從一定程度上也提高了效能。

從邏輯上來講,都是同乙個表,但是從物理上來看,分割槽將資料按照分割槽策略儲存到不同的表檔案中,目前mysql支援range 分割槽、list 分割槽、hash 分割槽、key 分割槽四種。

對於不同分割槽的資料,可以有不同的處理策略,以及不同的保留策略。

實現分割槽之後,就像sum、count等聚合操作,實際可以實現多分割槽的平行計算,提高效率。

mysql5.6.6之後支援指定分割槽檔案的儲存路徑,這從某種程度上,增強了分割槽的意義,實現了跨磁碟分割槽,具備更大的吞吐量。

//like below

partition by list(store_id) (

partition p1

values in (1, 3, 4, 17)

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

partition p2

values in (2, 12, 14)

index directory = '/var/orders/district2'

data directory = '/var/orders/district2',

以上都是分割槽的特性,對於企業級的生產環境db,如果是大表,建議必須使用分割槽,從而無論從可維護性、可拓展性、效能上都有較好的提公升。

講了分割槽的好處,那麼怎麼使用分割槽是乙個問題:

#by range 是一種常用的分割槽策略,可以按日期

create table employees (

id int not null,

fname varchar(30),

lname varchar(30),

hired date not null default '1970-01-01',

separated date not null default '9999-12-31',

job_code int not null,

store_id int not null

) partition by range (store_id) (

partition p0 values less than (6),

partition p1 values less than (11),

partition p2 values less than (16),

partition p3 values less than (21),

partition p3 values less than maxvalue

);#by list,通過列舉,實際應用的可能會少很多

create table employees (

id int not null,

fname varchar(30),

lname varchar(30),

hired date not null default '1970-01-01',

separated date not null default '9999-12-31',

job_code int not null,

store_id int not null

) partition by list(store_id)

partition pnorth values in (3,5,6,9,17),

partition peast values in (1,2,10,11,19,20),

partition pwest values in (4,12,13,14,18),

partition pcentral values in (7,8,15,16)

);#by hash;hash (expr) 注意expr必須是乙個整型表示式

create table employees (

id int not null,

fname varchar(30),

lname varchar(30),

hired date not null default '1970-01-01',

separated date not null default '9999-12-31',

job_code int not null,

store_id int not null

) partition by hash(store_id)

partitions 4;

# partitions num 指定分割槽的數量

#key分割槽,可以指定一列或者多列;而hash分割槽只能是一列

create table tk (

col1 int not null,

col2 char(5),

col3 date

) partition by linear key (col1)

partitions 3;

注意,分割槽限制:

mysql分割槽中如果存在主鍵或唯一鍵,則分割槽列必須包含在其中。

對於原生的range分割槽,list分割槽,hash分割槽,分割槽物件返回的只能是整數值。

分割槽字段不能為null,要不然怎麼確定分割槽範圍呢,所以盡量not null

create table employees (

id int not null,

fname varchar(30),

lname varchar(30),

hired date not null default '1970-01-01',

separated date not null default '9999-12-31',

job_code int not null,

store_id int not null

) partition by range (store_id) (

partition p0 values less than (6),

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

partition p1 values less than (11),

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

partition p2 values less than (16),

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

partition p3 values less than (21),

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

partition p3 values less than maxvalue

index directory = '/var/orders/district1'

data directory = '/var/orders/district1',

);

show table status like '%part_tab%';
ignore~留給讀者動手測試大量資料下的分割槽效能,相信能讓你更加印象深刻。

如果說有什麼劣勢,那麼我想就是:mysql分割槽中如果存在主鍵或唯一鍵,則分割槽列必須包含在其中。

這一點大大限制了分割槽的使用範圍,只能滿足基於唯一鍵需求的表。

ignore~留給讀者動手測試大量資料下的分割槽效能,相信能讓你更加印象深刻。

如果說有什麼劣勢,那麼我想就是:mysql分割槽中如果存在主鍵或唯一鍵,則分割槽列必須包含在其中。

這一點大大限制了分割槽的使用範圍,只能滿足基於唯一鍵需求的表。

mysql分割槽邊 mysql分割槽

檢視資料庫版本是否支援分割槽 分割槽的四種型別 range分割槽 範圍分割槽,根據某個欄位的值來進行分割槽,某個連續的區間來進行區分 建立表時分區create table teacher id varchar 20 not null name varchar 20 age varchar 20 bi...

mysql 分割槽的作用 MySQL分割槽的優點

mysql分割槽有多種模式,而且在mysql5.1中,支援水平分割槽,下面就為您介紹5個常見的mysql分割槽模式,希望對您有所幫助。mysql5.1中最激動人心的新特性應該就是對水平分割槽的支援了。這對mysql的使用者來說確實是個好訊息,而且她已經支援分割槽大部分模式 range 範圍 這種模式...

mysql 的分割槽

使用過oracle的分割槽 create table partition by list field value alter table truncate add partition.查了一下mysql的分割槽功能,mysql只有在5.1版本以後才支援分割槽 mysql的分割槽與oracle的分割槽...