create table order_history( id bigint unsigned auto_increment primary key,order_id bigint unsigned not null,customer_id bigint unsigned not null,goods_id bigint unsigned not null,payment_id bigint unsigned not null,details text,comments text,order_date datetime not null,status tinyint);
alter table order_history algorithm=inplace, lock=none, drop primary key, add primary key (id, order_date);-- algorithm=inplace, lock=none 是 rds for mysql 5.6 支援的 online ddl 特性。-- 如果要修改主鍵,刪除主鍵和新增主鍵建議放在一條語句中,以便充分利用 5.6 版本的 online ddl 特性。alter table order_history partition by range columns (order_date) (partition p1606 values less than ('2016-07-01'),partition p1607 values less than ('2016-08-01'),partition p1608 values less than ('2016-09-01'),partition p1609 values less than ('2016-10-01'),partition p1610 values less than ('2016-11-01'),partition p1611 values less than ('2016-12-01'),partition p1612 values less than ('2017-01-01'),partition p0 values less than maxvalue);-- 將資料按月份分割槽
delimiter //drop procedure sp_order_his_rotate//create procedure sp_order_his_rotate ()begin declare todrop_par varchar(10) default null; declare toadd_par varchar(10) default null; declare toadd_day varchar(10) default null; declare last_par varchar(10) default null; declare new_par varchar(10) default null; declare v_sql varchar(200) default null; select date_format(date_sub(curdate(), interval 6 month), 'p%y%m') into todrop_par; select date_format(date_add(curdate(), interval 1 month), 'p%y%m') into toadd_par; select date_format(date_add(curdate()-day(curdate())+1,interval 2 month), '%y-%m-%d') into toadd_day; select partition_name into last_par from information_schema.partitions where table_name = 'order_history' and table_schema = 'db_name' and partition_description != 'maxvalue' order by partition_description asc limit 1; if todrop_par = last_par then select concat('alter table order_history drop partition ',todrop_par,';') into v_sql; set @v_sql=v_sql; prepare stmt from @v_sql; execute stmt; deallocate prepare stmt; end if; select partition_name into new_par from information_schema.partitions where table_name = 'order_history' and table_schema = 'db_name' and partition_description != 'maxvalue' order by partition_description desc limit 1; if toadd_par != new_par then select concat('alter table order_history reorganize partition p0 into (partition ', toadd_par, ' values less than (''', toadd_day,'''), partition p0 values less than (maxvalue));') into v_sql; set @v_sql=v_sql; prepare stmt from @v_sql; execute stmt; deallocate prepare stmt; end if;end;//delimiter ;
drop event if exists order_his_rotate;delimiter //create event order_his_rotate on schedule every 1 month starts '2016-12-28 02:00:00' on completion preserve enable dobegin call sp_order_his_rotate();end;//
delimiter ;
通過分割槽(Partition)提公升MySQL效能
相信有很多人經常會問同樣的乙個問題 當 mysql 的總記錄數超過了100萬後,會出現效能的大幅度下降嗎?答案是肯定的,但是效能下 降 的比率不一而同,要看系統的架構 應用程式 還有 包括索引 伺服器硬體等多種因素而定。當有問我這個問題的時候,我最常見的回 答 就是 分表,可以根據id區間或者時間先...
同時也希望通過分享來增
同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通過分享來增 同時也希望通...
Python通過分頁對資料進行展示
通過分頁對資料進行展示 要求 每頁顯示10條資料 讓使用者輸入要檢視的頁面 頁碼 user list for i in range 1,836 temp 資料總條數 total count len user list 每頁顯示10條 per page count 10 總頁碼數 max page n...