php mysql新聞表模板 新聞資料庫分表案例

2021-10-18 12:38:46 字數 4496 閱讀 3870

新聞資料庫分表案例目錄:[-]netkillermysql手札mysqlmariadb...mr.neochan,陳景峰(bg7nyt)4.16.3.新聞資料庫分表案例netkillermysql手札mysqlmariadb...mr.neoc

新聞資料庫分表案例

目錄:[ ,

+86 13113668890

+86 755 29812080

文件始創於2010-11-18

我的系列文件

netkiller architect 手札netkiller developer 手札netkiller php 手札netkiller python 手札netkiller testing 手札netkiller cryptography 手札

netkiller linux 手札netkiller centos 手札netkiller freebsd 手札netkiller security 手札netkiller version 手札netkiller web 手札

netkiller monitoring 手札netkiller storage 手札netkiller mail 手札netkiller shell 手札netkiller network 手札netkiller database 手札

netkiller postgresql 手札netkiller mysql 手札netkiller nosql 手札netkiller ldap 手札netkiller cisco ios 手札netkiller h3c 手札

netkiller ********** 手札netkiller docbook 手札netkiller 開源軟體 手札

4.16.3. 新聞資料庫分表案例

這裡我通過乙個新聞**為例,解決分表的問題

避免開發中經常拼接表,我採用乙個一勞永逸的方法,建立乙個 news 表使用黑洞引擎,然後通過出發器將資料分流到匹配的表中。同時採用uuid替代數字序列,可以保證未來數年不會出現id用盡。create table if not exists `news` (

`uuid` varchar(36) not null comment '唯一id',

`title` varchar(50) not null comment '新聞標題',

`body` text not null comment '新聞正文',

`ctime` timestamp not null default '0000-00-00 00:00:00' comment '建立時間',

`mtime` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',

`atime` timestamp not null default '0000-00-00 00:00:00' comment '訪問時間',

primary key (`uuid`)

) engine=blackhole default charset=utf8;

該錶僅僅用於舉例,結構比較簡單。接下來建立年份分表,你也可以每個月乙個表,根據你的許下靈活調整。表結構與上面的news表相同,注意 engine=innodb。create table if not exists `news_2012` (

`uuid` varchar(36) not null comment '唯一id',

`title` varchar(50) not null comment '新聞標題',

`body` text not null comment '新聞正文',

`ctime` timestamp not null default '0000-00-00 00:00:00' comment '建立時間',

`mtime` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',

`atime` timestamp not null default '0000-00-00 00:00:00' comment '訪問時間',

primary key (`uuid`)

) engine=innodb default charset=utf8 comment='news 表';

create table if not exists `news_2013` (

`uuid` varchar(36) not null comment '唯一id',

`title` varchar(50) not null comment '新聞標題',

`body` text not null comment '新聞正文',

`ctime` timestamp not null default '0000-00-00 00:00:00' comment '建立時間',

`mtime` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',

`atime` timestamp not null default '0000-00-00 00:00:00' comment '訪問時間',

primary key (`uuid`)

) engine=innodb default charset=utf8 comment='news 表';

uuid 索引表,主要的功能是通過uuid查詢出該記錄在那張表中。更好的方案是將資料放入solr中處理,包括標題與內容搜尋等等。create table `news_index` (

`uuid` varchar(36) not null,

`tbl_name` varchar(10) not null,

primary key (`uuid`)

comment='news uuid 索引表'

collate='utf8_general_ci'

engine=innodb;

news_insert 過程,用於向目標表中插入資料,可以單獨call 但不建議。因為insert 遠比 call 更通用,要考慮移植性與通用性delimiter //

create definer=`neo`@`%` procedure `news_insert`(in `uuid` varchar(36), in `title` varchar(50), in `body` text, in `ctime` timestamp)

begin

if year(ctime) = '2012' then

insert into news_2012(uuid,title,body,ctime) values(uuid,title, body, ctime);

end if;

if year(ctime) = '2013' then

insert into news_2013(uuid,title,body,ctime) values(uuid,title, body, ctime);

end if;

insert into news_index values(uuid, year(ctime));

end//

delimiter ;

插入觸發器,負責獲取 uuid 然後呼叫儲存過程set @oldtmp_sql_mode=@@sql_mode, sql_mode='';

delimiter //

create trigger `news_before_insert` before insert on `news` for each row begin

if new.uuid is null or new.uuid = '' or length(new.uuid) != 36 then

set new.uuid=uuid();

end if;

call news_insert(new.uuid,new.title,new.body,new.ctime);

end//

delimiter ;

set sql_mode=@oldtmp_sql_mode;

這個觸發器使用者保護表中的 uuid 值不被修改。set @oldtmp_sql_mode=@@sql_mode, sql_mode='';

php mysql新聞發布系統(三)

index.php created by phpstorm.user administrator date 2019 7 8 time 16 04 搜尋 新聞id 新聞標題 關鍵字作者 發布時間 新聞內容 操作 include dbconfig.php 匯入配置檔案 設定中文格式,解決亂碼的三種方式...

php mysql新聞發布系統(六)

search.php created by phpstorm.user administrator date 2019 7 12 time 21 12 header content type text html charset utf 8 新聞id 新聞標題 關鍵字作者 發布時間 新聞內容 操作 i...

php mysql新聞發布系統(一)

提前說 編輯器使用phpstorm 1 專案製作歷時15天,從頭開始自學php,純屬小白,有任何錯誤或者問題可以聯絡作者。3 專案只完成了基本的增刪查改,沒有頁面優化,存在一些未解決的小問題。4 基本結構 mysql 建立乙個資料庫 命名newdb 乙個基本表 命名news 7個頁面 dbconfi...