將mysql非分割槽錶轉換為分割槽表
檢視錶的分佈狀況
mysql> select count(*) from justin;
+----------+
| count(*) |
+----------+
| 5845246 |
+----------+
1 row in set (0.00 sec)
mysql> select month(create_time),count(*) from justin group by month(create_time);
+-----------------------+----------+
| month(create_time) | count(*) |
+-----------------------+----------+
| 1 | 1128520 |
| 11 | 1574965 |
| 12 | 3141750 |
+-----------------------+----------+
3 rows in set (6.93 sec)
考慮以create_time為分割槽鍵建立分割槽表
第一步 建立中間表,以主鍵id和分割槽列為聯合主鍵
CREATE TABLE `temp_justin` (
`id` bigint(1) NOT NULL AUTO_INCREMENT COMMENT '流水號,自增',
`create_time` datetime DEFAULT NULL COMMENT '訂單日誌建立時間(建立索引)',
PRIMARY KEY (`id`,`create_time`),
) ENGINE=MyISAM AUTO_INCREMENT=6000000 DEFAULT CHARSET=utf8;
表已經存在580多萬記錄並且不斷在增長,因此中間表初始的id值設定成6000000
增加分割槽,以月為單位
alter table temp_justin partition by range(to_days(create_time))
(
partition p1012 values less than (to_days('2011-01-01')),
partition p1101 values less than (to_days('2011-02-01')),
partition p1102 values less than (to_days('2011-03-01')),
partition p1103 values less than (to_days('2011-04-01')),
partition p1104 values less than (to_days('2011-05-01')),
partition p1105 values less than (to_days('2011-06-01')),
partition p1106 values less than (to_days('2011-07-01')),
partition p1107 values less than (to_days('2011-08-01')),
partition p1108 values less than (to_days('2011-09-01')),
partition p1109 values less than (to_days('2011-10-01')),
partition p11010 values less than (to_days('2011-11-01')),
partition p11011 values less than (to_days('2011-12-01')),
partition p11012 values less than (to_days('2012-01-01'))
);
第二步 重新命名錶
Alter table justin rename to justin_bak_110113;
Alter table temp_justin rename to justin;
第三步 同步資料
Insert into justin select * from temp_justin;
表裡已經存在將近600萬條記錄,如此批次匯入資料會對資料庫效能影響很大。
每一萬條提交一次,sleep 2s ,53萬資料總耗時2 min 39.67 sec。
mysql> create procedure cp_data()
-> begin
-> declare i int;
-> set i=0;
-> while i<60 do
-> insert into justin
-> select * from justin_bak_110113
-> where id >= i*10000 and id -> set i=i+1;
-> select sleep(2);
-> end while;
-> end||
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql> delete from justin;
-> ||
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call cp_data();
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2 min 39.67 sec)
Query OK, 0 rows affected (2 min 39.67 sec)
mysql> select count(*) from justin;
+----------+
| count(*) |
+----------+
| 525031 |
+----------+
1 row in set (0.00 sec)
檢視執行計劃,使用了分割槽掃描
mysql> explain
-> select count(*) from justin where create_time
-> <='2011-01-13' and create_time>'2011-01-04';
+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+--------------------------+
| 1 | SIMPLE | justin | index | NULL | PRIMARY | 16 | NULL | 525031 | Using where; Using index |
+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+--------------------------+
1 row in set (0.00 sec)
mysql> explain
-> partitions
-> select count(*) from justin where create_time
-> <='2011-01-13' and create_time>'2011-01-04';
+----+-------------+--------------------+-------------+-------+---------------+---------+---------+------+--------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+-------------+-------+---------------+---------+---------+------+--------+--------------------------+
| 1 | SIMPLE | justin | p1012,p1101 | index | NULL | PRIMARY | 16 | NULL | 525031 | Using where; Using index |
+----+-------------+--------------------+-------------+-------+---------------+---------+---------+------+--------+--------------------------+
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15480802/viewspace-689399/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 將非分割槽錶轉換為分割槽表
- 將一個非分割槽錶轉換為分割槽表
- 【分割槽】如何將一個普通錶轉換為分割槽表
- 將普通錶轉換為分割槽表
- Oracle 將普通錶轉換為分割槽表Oracle
- 非分割槽錶轉換成分割槽表
- 非分割槽錶轉換為分割槽表和partition indexIndex
- 普通錶轉換為分割槽表
- 堆錶轉換成分割槽表
- 非分割槽錶轉換成分割槽表以及注意事項
- ORACLE 11g 範圍分割槽錶轉換INTERVAL分割槽表Oracle
- ORACLE將普通錶轉變為分割槽表方法Oracle
- Oracle線上將普通錶轉分割槽表Oracle
- Oracle Database將普通錶轉換為分割槽表遇到的問題OracleDatabase
- 使用dbms_redefinition包線上把大資料量非分割槽錶轉換為分割槽表大資料
- 批次轉換分割槽表為普通表
- 批量轉換分割槽表為普通表
- Oracle 12C新特性-線上把非分割槽錶轉為分割槽表Oracle
- 使用DBMS_REDEFINITION在事務處理過程中將一個非分割槽錶轉換為分割槽表
- ORACLE普通錶轉換成分割槽表的操作Oracle
- 普通錶轉換分割槽表-線上重定義
- 利用線上重定義功能將普通錶轉換為分割槽表問題
- Oracle 12.2之後ALTER TABLE .. MODIFY轉換非分割槽表為分割槽表Oracle
- 【MYSQL】 分割槽表MySql
- MySQL 分割槽表MySql
- MySQL分割槽表MySql
- 線上重定義 ?普通錶轉換成分割槽表
- 普通錶轉換成分割槽表的四種方法
- oracle將表配置為分割槽表Oracle
- MySQL普通錶轉換成分割槽表的兩種方法舉例MySql
- 線上表格重定義研究,比如將普通錶轉換為分割槽表等
- MySQL 分割槽表 partition線上修改分割槽欄位MySql
- ORACLE 普通錶轉換成分割槽表(線上重定義)Oracle
- MySQL 分割槽表探索MySql
- mysql 分表 分割槽MySql
- mysql 分割槽表用法MySql
- MySQL表分割槽管理MySql
- ORACLE將不同表改為分割槽表Oracle