PHP操作mysql資料庫分表的方法

2022-09-28 20:03:23 字數 4280 閱讀 5815

一般來說,當我們的資料庫的資料超過了100w記錄的時候就應該考慮分表或者分割槽了,這次我來詳細說說分表的一些方法。首先,我們需要想好到底分多少個 表,前提當然是滿足應用。這裡我使用了乙個比較簡單的分表方法,就是根據自增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 www.cppcns.com( `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` tbcdttmkxwext 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**的方法如下:

<?php functi程式設計客棧on get_ai_id()

?>

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

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

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

} /**

* 用於根據id獲取表名

*/ funct程式設計客棧ion get_table_name($id)

?>

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

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

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

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

你可以根據id分,也可以根據年,月,地區來分。要按照業務需求。

本文標題: php操作mysql資料庫分表的方法

本文位址:

mysql資料庫分表

定時執行建立cisco tunnel分表 start ciscotunnelcreatejob class com.sdwan.task.ciscotunnelpartition ciscotunnelcreatejobmethod class org.springframework.schedul...

PHP 操作mysql資料庫

insert 基本設定 mysql server name localhost mysql username 使用者名稱 mysql password 密碼 mysql database 資料庫 建立連線 conn mysql connect mysql server name,mysql user...

php操作mysql資料庫

1.連線資料庫 mysql connect servername,username,password servername 可選,規定要連線的伺服器。預設是 localhost 3306 username 可選,規定登入所使用的使用者名稱。預設值是擁有伺服器程序的使用者的名稱 password 可選...