代码改变世界

IEE分月表改造

2015-05-14 23:01  AlfredZhao  阅读(608)  评论(0编辑  收藏  举报

IEE版本:5.1.40
需求:由于目前的IEE版本并不支持分区表,且删除历史数据效率很低,删除部分数据后空间释放方面也不理想。
现采用按月分表存放数据。这样卸载历史数据时,直接删除历史表即可。
改造步骤:

  1. 原表重命名为“原表名_YYYYMM”
  2. 上传月表创建脚本到服务器
  3. 附:IEE月表创建脚本

1.原表重命名为“原表名_YYYYMM”

YYYYMM代表当前年月,例如201505. IEE查询某业务用户下的表名:
select table_name from information_schema.TABLES where table_type='BASE TABLE' and table_schema='db_name' ;

注:上面的db_name 替换为你实际的数据库名字.

原表重命名:
例如,原表是my_t1重命名my_t1_201505

IEE重命名表的语法示例:

rename table my_t1 to my_t1_201505;

2.上传月表创建脚本到服务器

IEE按照月份来分表,不同月份数据入到不同的分月表中。

为了规范管理,统一将脚本上传到/root/get_ddl/路径下,
每月底在IEE服务器上执行get_ddl.sh脚本即可。

使用方法:
使用root用户
# cd /root/get_ddl
# sh get_ddl.sh
此脚本会自动获取本月相关表的表结构,在IEE库中建立下个月的表。如果新建表后有表结构修改,请在数据库中删除下月表后,重新执行脚本(请注意执行时间!)

附:IEE月表创建脚本

#!/bin/bash
#Usage: Create IEE Month Table, you can put this script in /root/get_ddl/.
#You need modify the IEE IP Address in the part of #1.1!
#ex: sh get_ddl.sh 2&> ./IEE_ddl.log
#Version: 2.0.0
#Author: O.o, Alfred Zhao
#Creation:2015-05-11

#1. Define variable.
#1.1 define your IEE IP Address.
host1="192.168.99.159"
pwd1="123321"
host2=""
pwd2=""
host3=""
pwd3=""
#1.2 define other variable.
base=`pwd`
old_date=`date +%Y%m`
new_date=`date +%Y%m --date='+1 month'`

#2. Create Folders
#Directory: $base/tmp, $base/old_table_dir, $base/new_table_dir.
if [ -d tmp ]; then 
        echo    'go on'
else
        echo    'mkdir tmp'
        mkdir tmp
fi

if [ -d old_table_dir ]; then 
        echo    'go on'
else
        echo    'mkdir old_table_dir'
        mkdir old_table_dir
fi

if [ -d new_table_dir ]; then 
        echo    'go on'
else
        echo    'mkdir new_table_dir'
        mkdir new_table_dir
fi

#3. Create tables
#3.1 create parameter file table_name.
mysql-ib <<EOF >$base/tmp/abc
select table_name from information_schema.TABLES where table_type='BASE TABLE' and table_schema='crnop' ;
exit
EOF
cat $base/tmp/abc|grep $old_date > $base/table_name

#3.2 loop execute
while read Fileline
do
     echo "use crnop;" > $base/tmp/$Fileline.sql
     echo "show create table $Fileline" >> $base/tmp/$Fileline.sql
     echo $Fileline > $base/tmp/Fileline
     mysql-ib -N < $base/tmp/$Fileline.sql > $base/old_table_dir/tmp_$Fileline.sql
     
     old_table_name=`cat $base/old_table_dir/tmp_$Fileline.sql | awk '{print $1}'`
     sed 's/\\n//g' $base/old_table_dir/tmp_$Fileline.sql |awk '{$1="";print $0}' > $base/old_table_dir/old_$Fileline.sql

     new_table_name_part=`awk -F'_' 'OFS="_"{$NF="";print}' $base/tmp/Fileline`
     new_table_name=$new_table_name_part$new_date

     echo "Old table name is :$old_table_name"
     echo "New table name is :$new_table_name"

     echo "use crnop;" > $base/new_table_dir/$new_table_name.sql
     sed "s/$old_table_name/$new_table_name/g" $base/old_table_dir/old_$Fileline.sql >> $base/new_table_dir/$new_table_name.sql

     if [ "$host1" != "" ]; then
          echo "host1: $host1"
          mysql-ib -u root -p$pwd1 -h$host1 < $base/new_table_dir/$new_table_name.sql
     else
          echo "host1 $host1 doesn't exist."
     fi

     if [ "$host2" != "" ]; then
          echo "host2: $host2"
          mysql-ib -u root -p$pwd2 -h$host2 < $base/new_table_dir/$new_table_name.sql
     else
          echo "host2 $host2 doesn't exist."
     fi

     if [ "$host3" != "" ]; then
          echo "host3: $host3"
          mysql-ib -u root -p$pwd3 -h$host3 < $base/new_table_dir/$new_table_name.sql
     else
          echo "host3 $host3 doesn't exist."
     fi
done < $base/table_name