Mysql分表教程

2021-09-06 13:57:00 字數 4087 閱讀 3914

一般來說,當我們的資料庫的資料超過了100w記錄的時候就應該考慮分表或者分割槽了,這次我來詳細說說分表的一些方法。目前我所知道的方法都是myisam的,innodb如何做分表並且保留事務和外來鍵,我還不是很了解。

首 先,我們需要想好到底分多少個表,前提當然是滿足應用。這裡我使用了乙個比較簡單的分表方法,就是根據自增id的尾數來分,也就是說分0-9一共10個 表,其取值也很好做,就是對10進行取模。另外,還可以根據某一字段的md5值取其中幾位進行分表,這樣的話,可以分的表就很多了。

好了,先來建立表吧,**如下:

create table `test`.`article_0` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_1` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_2` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_3` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_4` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_5` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_6` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_7` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_8` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

create table `test`.`article_9` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine = myisam character set utf8 collate utf8_general_ci

好了10個表建立完畢了,需要注意的是,這裡的id不能設為自增,而且所有的表結構必須一致,包括結構,型別,長度,欄位的順序都必須一致那麼對於這個id如何取得呢?後面我會詳細說明。現在,我們需要乙個合併表,用於查詢,建立合併表的**如下:

create table `test`.`article` ( 

`id` bigint( 20 ) not null ,

`subject` varchar( 200 ) not null ,

`content` text not null ,

primary key ( `id` )

) engine=mrg_myisam default charset=utf8 insert_method=0 union  =(`article_0`,`article_1`,`article_2`,`article_3`,`article_4`,`article_5`,`article_6`,`article_7`,`article_8`,`article_9`);

注 意,合併表也必須和前面的表有相同的結構,型別,長度,包括欄位的順序都必須一致這裡的insert_method=0表示不允許對本表進行insert 操作。好了,當需要查詢的時候,我們可以只對article這個表進行操作就可以了,也就是說這個表僅僅只能進行select操作,那麼對於插入也就是 insert操作應該如何來搞呢,首先就是獲取唯一的id了,這裡就還需要乙個表來專門建立id,**如下:

create table `test`.`create_id` ( 

`id` bigint( 20 ) not null auto_increment primary key 

) engine = myisam

也就是說,當我們需要插入資料的時候,必須由這個表來產生id值,我的php**的方法如下:

<?php 

function get_ai_id()  

?>

好了,現在假設我們要插入一條資料了,應該怎麼操作呢?還是繼續看**吧:

<?php 

function new_article() (id,subject,content) values('','測試標題','測試內容')"; 

$this->db->query($sql); 

} /** 

* 用於根據id獲取表名 

*/ function get_table_name($id)  

?>

其實很簡單的,對吧,就是先獲取id,然後根據id獲取應該插入到哪個表,然後就很簡單了。

對於update的操作我想應該不需要再說了吧,無非是有了id,然後獲取表名,然後進行update操作就好了。

Mysql分表教程

一般來說,當我們的資料庫的資料超過了100w記錄的時候就應該考慮分表或者分割槽了,這次我來詳細說說分表的一些方法。目前我所知道的方法都是myisam的,innodb如何做分表並且保留事務和外來鍵,我還不是很了解。首 先,我們需要想好到底分多少個表,前提當然是滿足應用。這裡我使用了乙個比較簡單的分表方...

mysql分庫分表入門教程

1 資料庫瓶頸 1 io瓶頸 第一種 磁碟讀io瓶頸,熱點資料太多,資料庫快取放不下,每次查詢時會產生大量的io,降低查詢速度 分庫 第二種 網路io瓶頸,請求的資料太多,網路頻寬不夠 分庫 2 cpu瓶頸 第一種 sql問題,如sql中包含join,group by,order by,非索引字段條...

mysql分表準則 Mysql分表準則

mysql分表準則 在大量使用mysql時,資料量大 高訪問時,為了提高效能需要分表處理,簡介下mysql分表的標準,後續會繼續補充 環境 業務型別 oltp 硬體 cpu 8cpu 2.4ghz mem 48g 磁碟 raid5 6 sas 什麼樣的表需要拆分 根據表的體積 表的行數 訪問特點來衡...