当前位置:   article > 正文

mysql 普通表转分区表_分区表mysql 普通表转分区表

分区表mysql 普通表转分区表
  1. 1.按照现有表结构,创建一个临时表,用于分区表转换
  2. mysql> desc ClientActionTrack;
  3. +---------------+---------------+------+-----+---------+----------------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +---------------+---------------+------+-----+---------+----------------+
  6. | sn | bigint(20) | NO | PRI | NULL | auto_increment |
  7. | clientSn | int(11) | YES | | NULL | |
  8. | ip | varchar(32) | YES | | NULL | |
  9. | url | varchar(1000) | YES | | NULL | |
  10. | httpMethod | varchar(100) | YES | | NULL | |
  11. | requestParams | text | YES | | NULL | |
  12. | requestHeader | varchar(2000) | YES | | NULL | |
  13. | pageUrl | varchar(500) | YES | | NULL | |
  14. | sessionId | varchar(100) | YES | | NULL | |
  15. | startTime | datetime | YES | | NULL | |
  16. | completeTime | datetime | YES | | NULL | |
  17. +---------------+---------------+------+-----+---------+----------------+
  18. 11 rows in set (0.01 sec)
  19. mysql> select clientSn,httpMethod,startTime from ClientActionTrack limit 5;
  20. +----------+------------+---------------------+
  21. | clientSn | httpMethod | startTime |
  22. +----------+------------+---------------------+
  23. | NULL | POST | 2015-09-14 10:54:57 |
  24. | NULL | POST | 2015-09-14 10:54:57 |
  25. | NULL | POST | 2015-09-14 10:54:57 |
  26. | NULL | POST | 2015-09-14 10:54:57 |
  27. | NULL | POST | 2015-09-14 10:54:57 |
  28. +----------+------------+---------------------+
  29. 5 rows in set (0.00 sec)
  30. mysql> CREATE TABLE `tmp01` (
  31. -> `sn` bigint(20) NOT NULL AUTO_INCREMENT,
  32. -> `clientSn` int(11) DEFAULT NULL,
  33. -> `ip` varchar(32) DEFAULT NULL,
  34. -> `url` varchar(1000) DEFAULT NULL COMMENT 'request url',
  35. -> `httpMethod` varchar(100) DEFAULT NULL COMMENT 'http method',
  36. -> `requestParams` text COMMENT '请求参数',
  37. -> `requestHeader` varchar(2000) DEFAULT NULL COMMENT '请求头信息',
  38. -> `pageUrl` varchar(500) DEFAULT NULL COMMENT '页面Url',
  39. -> `sessionId` varchar(100) DEFAULT NULL COMMENT 'session的Id',
  40. -> `startTime` datetime DEFAULT NULL COMMENT '访问开始时间',
  41. -> `completeTime` datetime DEFAULT NULL COMMENT '访问完成时间',
  42. -> PRIMARY KEY (`sn`)
  43. -> ) ENGINE=InnoDB AUTO_INCREMENT=302753 DEFAULT CHARSET=utf8 COMMENT='用户访问记录表'
  44. -> partition by RANGE (startTime) (PARTITION p20150101 values less than (20150102));
  45. ERROR 1659 (HY000): Field 'startTime' is of a not allowed type for this type of partitioning
  46. mysql> select min(startTime),max(startTime) from ClientActionTrack;
  47. +---------------------+---------------------+
  48. | min(startTime) | max(startTime) |
  49. +---------------------+---------------------+
  50. | 2015-09-14 10:54:57 | 2015-10-27 15:12:00 |
  51. +---------------------+---------------------+
  52. 1 row in set (0.97 sec)
  53. CREATE TABLE `tmp01` (
  54. `sn` bigint(20) NOT NULL AUTO_INCREMENT,
  55. `clientSn` int(11) DEFAULT NULL,
  56. `ip` varchar(32) DEFAULT NULL,
  57. `url` varchar(1000) DEFAULT NULL COMMENT 'request url',
  58. `httpMethod` varchar(100) DEFAULT NULL COMMENT 'http method',
  59. `requestParams` text COMMENT '请求参数',
  60. `requestHeader` varchar(2000) DEFAULT NULL COMMENT '请求头信息',
  61. `pageUrl` varchar(500) DEFAULT NULL COMMENT '页面Url',
  62. `sessionId` varchar(100) DEFAULT NULL COMMENT 'session的Id',
  63. `startTime` datetime DEFAULT NULL COMMENT '访问开始时间',
  64. `completeTime` datetime DEFAULT NULL COMMENT '访问完成时间',
  65. PRIMARY KEY (`sn`,`startTime`)
  66. ) ENGINE=InnoDB AUTO_INCREMENT=302753 DEFAULT CHARSET=utf8 COMMENT='用户访问记录表'
  67. partition by RANGE (TO_DAYS(startTime)) (PARTITION p20150914 values less than (736221));
  68. 主键列增加 数据被放大
  69. 2.批量生成分区表
  70. export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
  71. date=`echo $1 | tr -d '-'`
  72. date1=`echo $2 | tr -d '-'`
  73. date2=`echo $1`
  74. date_end=`get_date $2 +1 | tr -d '-'`
  75. while :
  76. do
  77. #echo $date
  78. date3=`get_date $date +1`
  79. var=`perl ./get_date.pl $date3`
  80. echo "alter table tmp01 add partition (partition p$date values less than ($var));"
  81. date2=`get_date $date +1`
  82. date=`echo $date2 | tr -d '-'`
  83. #echo $date3
  84. if [ "$date" = "$date_end" ]
  85. then
  86. exit
  87. fi
  88. done
  89. You have mail in /var/spool/mail/oracle
  90. oadb01:/home/oracle/mysql> sh ./p1.sh 20150914 20151231 >p1.sql
  91. 3.mysql> insert into tmp01 select * from ClientActionTrack; ---插入数据到临时表
  92. 4.删除旧表,rename tmp01为ClientActionTrack;
  93. mysql> drop table ClientActionTrack;
  94. Query OK, 0 rows affected (0.24 sec)
  95. mysql> rename table tmp01 to ClientActionTrack;
  96. 5.访问分区:
  97. 以前的查询方式:
  98. select min(startTime),max(startTime) from ClientActionTrack where starttime >='2015-10-02 00:00:00' and starttime<='2015-10-02 23:59:59'
  99. 改造成分区后:
  100. SELECT min(startTime),max(startTime) FROM ClientActionTrack PARTITION (p20151002);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/961059
推荐阅读
相关标签
  

闽ICP备14008679号