mysql分表技巧 MySQL分表操作的方法分析

2021-10-17 15:12:23 字數 4173 閱讀 4448

本節內容:

mysql分表

一般來說,當資料庫的資料超過了100w記錄時就應該考慮分表或者分割槽了。

本文介紹下mysql分表的一些方法。

首先,需要想好到底分多少個表,前提當然是滿足應用。

這裡使用了乙個比較簡單的分表方法,就是根據自增id的尾數來分,也就是說分0-9一共10個表,其取值也很好做,就是對10進行取模。

另外,還可以根據某一字段的md5值取其中幾位進行分表,這樣的話,可以分的表就很多了。

建立表:

複製** **示例:

create table `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`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 `ttlsa_com`.`create_id` (

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

) engine = myisam

即需要插入資料時,必須由這個表來產生id值,php**:

複製** **示例:

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

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

* 用於根據id獲取表名

function get_table_name($id) {

return 'article_'.intval($id)%10;

說明:先獲取id,然後根據id獲取應該插入到哪個表,然後就很簡單了。

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

對於使用者表,建個最少列最基本資訊的使用者名稱,比如使用者id,使用者名稱,密碼。使用者的其他資訊分布到以使用者id分表的表上。

怎麼分表如何分表以業務需求而定。

參考:

mysql分表準則 Mysql分表準則

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

mysql分表的原則 Mysql分表準則

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

mysql分表分庫實現 MySql分表分庫思路

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