按指定年份删除mysql分区表

111 篇文章 1 订阅
69 篇文章 0 订阅
#delete table partitions in terms with table name and date
#!/bin/bash
#define config file and get password
CONFIG_FILE=/home/xx/conf/yy.cfg
DBPWD=`grep "dbpwd" $CONFIG_FILE|awk -F "= " '{printf $2}'|awk -F'#'  '{print $1}'`
#define tables which contain partitions there are 35 tables
table_list=(wdd_flow_osi wdd_flow_appid wdd_audit_ics wdd_access wdd_flow_broadcast wdd_flow_ip wdd_flow_proto wdd_flow_packetsize wdd_flow_diagnosis wdd_ssnptr_end sendout_ftp_files wdd_sr_resource wdd_ip_command_count wdd_ip_command_day wdd_accessgrade_stat wdd_appid_session wdd_session_login attachment_info attachment_counter attachment_url_info attack_roadmap_node_attachment sandbox_suspicious_url wdd_access_combined_map wdd_access_counter wdd_access_ip_host wdd_access_maininfo wdd_access_sync_waf wdd_cloud_file wdd_file_thread_degree wdd_sr_alarm_ipinfo wdd_access_audit_map wdd_access_file wdd_audit wdd_ssnptr_start wdd_flow_coll)
#delete partitions between the min year and max year
function delete_table_partitions_between_years()
{
    data_base=$1
    table_name=$2
    min_year=$3
    max_year=$4
    select_partitions_sql=`echo "SELECT PARTITION_NAME from information_schema.PARTITIONS WHERE TABLE_SCHEMA='$data_base' and TABLE_NAME='$table_name'"|mysql -u wdd -p$DBPWD $data_base`
    echo "table $table_name partitions as following:"
    echo $select_partitions_sql
    partition_array=(${select_partitions_sql// / })
    #get every partition
    for partition in ${partition_array[@]}
    do
        if [ $partition = PARTITION_NAME ];then #ignore PARTITION_NAME
            continue
        fi
        echo $partition
        year=`echo $partition | awk '{print substr($1,2,4)}'` #get the year from partition
        echo $year
        if [[ $year -le $max_year && $year -ge $min_year ]];then    #between the years
            delete_partitions_sql=`echo "ALTER TABLE $table_name DROP PARTITION $partition"|mysql -u wdd -p$DBPWD $data_base`
            echo $delete_partitions_sql
            echo $?
        fi
    done
}
for table_name in ${table_list[@]}
do
    delete_table_partitions_between_years wdd $table_name 2017 2019
done

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值