1. 程式人生 > >mysql 普通錶轉分割槽表

mysql 普通錶轉分割槽表

1.按照現有表結構,建立一個臨時表,用於分割槽錶轉換
mysql> desc ClientActionTrack;
+---------------+---------------+------+-----+---------+----------------+
| Field         | Type          | Null | Key | Default | Extra          |
+---------------+---------------+------+-----+---------+----------------+
| sn            | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| clientSn      | int(11)       | YES  |     | NULL    |                |
| ip            | varchar(32)   | YES  |     | NULL    |                |
| url           | varchar(1000) | YES  |     | NULL    |                |
| httpMethod    | varchar(100)  | YES  |     | NULL    |                |
| requestParams | text          | YES  |     | NULL    |                |
| requestHeader | varchar(2000) | YES  |     | NULL    |                |
| pageUrl       | varchar(500)  | YES  |     | NULL    |                |
| sessionId     | varchar(100)  | YES  |     | NULL    |                |
| startTime     | datetime      | YES  |     | NULL    |                |
| completeTime  | datetime      | YES  |     | NULL    |                |
+---------------+---------------+------+-----+---------+----------------+
11 rows in set (0.01 sec)

mysql>  select clientSn,httpMethod,startTime from ClientActionTrack limit 5;
+----------+------------+---------------------+
| clientSn | httpMethod | startTime           |
+----------+------------+---------------------+
|     NULL | POST       | 2015-09-14 10:54:57 |
|     NULL | POST       | 2015-09-14 10:54:57 |
|     NULL | POST       | 2015-09-14 10:54:57 |
|     NULL | POST       | 2015-09-14 10:54:57 |
|     NULL | POST       | 2015-09-14 10:54:57 |
+----------+------------+---------------------+
5 rows in set (0.00 sec)


mysql> CREATE TABLE `tmp01` (
    ->   `sn` bigint(20) NOT NULL AUTO_INCREMENT,
    ->   `clientSn` int(11) DEFAULT NULL,
    ->   `ip` varchar(32) DEFAULT NULL,
    ->   `url` varchar(1000) DEFAULT NULL COMMENT 'request url',
    ->   `httpMethod` varchar(100) DEFAULT NULL COMMENT 'http method',
    ->   `requestParams` text COMMENT '請求引數',
    ->   `requestHeader` varchar(2000) DEFAULT NULL COMMENT '請求頭資訊',
    ->   `pageUrl` varchar(500) DEFAULT NULL COMMENT '頁面Url',
    ->   `sessionId` varchar(100) DEFAULT NULL COMMENT 'session的Id',
    ->   `startTime` datetime DEFAULT NULL COMMENT '訪問開始時間',
    ->   `completeTime` datetime DEFAULT NULL COMMENT '訪問完成時間',
    ->   PRIMARY KEY (`sn`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=302753 DEFAULT CHARSET=utf8 COMMENT='使用者訪問記錄表'
    -> partition by RANGE (startTime)  (PARTITION  p20150101 values less than (20150102));  
	
ERROR 1659 (HY000): Field 'startTime' is of a not allowed type for this type of partitioning

mysql> select min(startTime),max(startTime) from ClientActionTrack;
+---------------------+---------------------+
| min(startTime)      | max(startTime)      |
+---------------------+---------------------+
| 2015-09-14 10:54:57 | 2015-10-27 15:12:00 |
+---------------------+---------------------+
1 row in set (0.97 sec)

CREATE TABLE `tmp01` (
  `sn` bigint(20) NOT NULL AUTO_INCREMENT,
  `clientSn` int(11) DEFAULT NULL,
  `ip` varchar(32) DEFAULT NULL,
  `url` varchar(1000) DEFAULT NULL COMMENT 'request url',
  `httpMethod` varchar(100) DEFAULT NULL COMMENT 'http method',
  `requestParams` text COMMENT '請求引數',
  `requestHeader` varchar(2000) DEFAULT NULL COMMENT '請求頭資訊',
  `pageUrl` varchar(500) DEFAULT NULL COMMENT '頁面Url',
  `sessionId` varchar(100) DEFAULT NULL COMMENT 'session的Id',
  `startTime` datetime DEFAULT NULL COMMENT '訪問開始時間',
  `completeTime` datetime DEFAULT NULL COMMENT '訪問完成時間',
  PRIMARY KEY (`sn`,`startTime`)
) ENGINE=InnoDB AUTO_INCREMENT=302753 DEFAULT CHARSET=utf8 COMMENT='使用者訪問記錄表'
partition by RANGE (TO_DAYS(startTime))  (PARTITION  p20150914 values less than (736221)); 

主鍵列增加 資料被放大

2.批量生成分割槽表
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
date=`echo $1 | tr -d '-'`
date1=`echo $2 | tr -d '-'`
date2=`echo $1`
date_end=`get_date $2 +1 | tr -d '-'`
while :
    do
      #echo $date
      date3=`get_date $date +1`
      var=`perl ./get_date.pl $date3`
     echo "alter table tmp01 add partition (partition p$date values less than ($var));"
      date2=`get_date $date +1`
      date=`echo $date2 | tr -d '-'`
      #echo $date3
if [ "$date" = "$date_end" ]
       then
        exit
fi
done
You have mail in /var/spool/mail/oracle
oadb01:/home/oracle/mysql> sh ./p1.sh 20150914 20151231 >p1.sql


3.mysql> insert into tmp01 select * from ClientActionTrack;  ---插入資料到臨時表


4.刪除舊錶,rename tmp01為ClientActionTrack;
mysql> drop table ClientActionTrack;
Query OK, 0 rows affected (0.24 sec)

mysql> rename table  tmp01 to ClientActionTrack;

5.訪問分割槽:


以前的查詢方式:
select min(startTime),max(startTime) from ClientActionTrack where starttime >='2015-10-02 00:00:00' and starttime<='2015-10-02 23:59:59'

改造成分割槽後:

SELECT min(startTime),max(startTime) FROM ClientActionTrack PARTITION (p20151002);