批量分表分庫

2021-09-30 01:50:24 字數 1841 閱讀 2558

儲存過程:

drop procedure if exists `create_tables`;

delimiter $$ //定義結束符

create procedure create_tables (

)begin

declare `@i` int (11)

;declare `@createsql` varchar (

2560);

declare `@j` varchar(10

);set `@i` =1;

while `@i` <

325 do

if `@i` <

10 then

set `@j` =

concat

(`@i`)

; else

set `@j` = `@i`;

end if;

-- 建立表

set @createsql

=concat

("create table if not exists record_value_"

, `@j`,

"(`id` bigint(11

) not null comment '主鍵'

, `record_time` datetime(3

) not null comment '入庫時間'

, `site_id` int(11

) not null comment '站點id'

, `device_id` int(11

) not null comment '裝置編號'

, `sensor_type` int(11

) not null comment '裝置型別'

, `wind_speed` float default null comment '風速'

, `wind_direction` float default null comment '風向'

, `temperature` float default null comment '溫度'

, `humidity` float default null comment '濕度'

, `pressure` float default null comment '壓強'

, `rainfall` float default null comment '降雨量'

, `u_speed` float default null comment 'u向速度'

, `v_speed` float default null comment 'v向速度'

, `w_speed` float default null comment 'w向速度'

, primary key (`id`) using btree,

key `index_name` (`record_time`,`wind_speed`,`wind_direction`,`temperature`,`humidity`,`pressure`,`rainfall`,`device_id`) using btree

) engine=innodb default charset=utf8;")

;prepare stmt from @createsql

;execute stmt;

set `@i` = `@i` +1;

end while;

end $$

delimiter ;

呼叫儲存過程

call create_tables()

;

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

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

MySQL範圍分表分庫 mysql 分表分庫策略

唯一id的生成 下面列舉幾種常見的唯一id生成方案,需要滿足兩大核心需求 1.全域性唯一 2趨勢有序 1.用資料庫的auto increment 自增id 來生成,每次通過寫入資料庫一條記錄,利用資料庫id自增的特性獲取唯一,有序的id。優點 使用資料庫原有的功能,相對簡單 能夠保證唯一 能夠保證遞...

mysql 分庫分表實戰 MySQL分庫分表實戰

為什麼要分庫分表 在大型 中,當使用者量以及使用者產生的業務資料量達到單庫單錶效能極限時,為了支撐業務可持續發展,對於重要的核心業務必然要進行分庫分表來儲存業務資料。對於非核心業務產生的大量資料,例如爬蟲爬取的資訊,論壇產生的資料等,可以考慮把資料儲存在像mongodb這樣的nosql儲存裡面,這些...