search
HomeDatabaseMysql Tutorialdb2 增加删除分区

#直接删除表 if [ quot;$DEL_VALUEquot; = quot;-quot; ] then delete_tab_data=`db2 -tx q

由于在项目中在delete许多大表的数据,起初采用不写日志的方式,,后在集成测试时发现,如果有在delete过程中出现中断,这时这个表就不能再用了,必须drop后重建,风险性比较大,后来经过查找资料,请教dba后采用另一种方法,修改大表的建表语句,使之变成分区表,然后进行detach把分区数据到临时表中,删除临时表,这个就达到删除数据目的了,下面我列出具体的操作步骤及相应的shell脚本。

1.创建分区表,INCLUSIVE(包含),exclusive(不包含)

 CREATE TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" (
  "REPORT_DATE" DATE,
  "MA_ACCT_NO" VARCHAR(100),
  "TRANS_DATE" DATE,
  "ORG_UNIT_ID" VARCHAR(15),
  "ORG_PROD_ID" VARCHAR(15),
  "CURR_CD" VARCHAR(15),
  "ACCT_NO" VARCHAR(15),
  "TRANS_NUM" VARCHAR(15),
  "TRANS_NO" VARCHAR(15),
  "TRANS_DIF" VARCHAR(15),
  "DEPOSIT_CHAR" VARCHAR(15),
  "DEPOSIT_BAL" DECIMAL(18,2),
  "TRANS_AMT" DECIMAL(18,2),
  "TRANS_TYPE" CHARACTER(1),
  "FLG" CHARACTER(1),
  "RATE" DECIMAL(18,6),
  "TRXMEM" DECIMAL(4,0)
)
  IN "MA_DATA"
  INDEX IN "MA_INDEX"
  PARTITION BY RANGE ( "REPORT_DATE" NULLS LAST ) ( PARTITION PART0 STARTING '2010-12-10' INCLUSIVE  ENDING '2010-12-20' INCLUSIVE )
   ;

ALTER TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK"
  DATA CAPTURE NONE
  LOCKSIZE ROW
  APPEND OFF
  NOT VOLATILE;

COMMENT ON TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" IS '活期交易明细表';

COMMENT ON "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" (
 "REPORT_DATE" IS '数据日期',
 "MA_ACCT_NO" IS '管会账号',
 "TRANS_DATE" IS '营业日期',
 "ORG_UNIT_ID" IS '行所号',
 "ORG_PROD_ID" IS '业务品种 ',
 "CURR_CD" IS '币别',
 "ACCT_NO" IS '帐号',
 "TRANS_NUM" IS '交易序号',
 "TRANS_NO" IS '交易代号',
 "TRANS_DIF" IS '交易区别',
 "DEPOSIT_CHAR" IS '存款性质 ',
 "DEPOSIT_BAL" IS '存款余额',
 "TRANS_AMT" IS '交易金额',
 "TRANS_TYPE" IS '交易别',
 "FLG" IS '连动标志',
 "RATE" IS '汇率' );

GRANT CONTROL ON TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" TO USER "DB2INST2";

GRANT SELECT, INSERT, UPDATE, ALTER, DELETE,
    INDEX, REFERENCES ON TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" TO USER "DB2INST2" WITH GRANT OPTION;

GRANT SELECT, INSERT, UPDATE, ALTER, DELETE,
    INDEX, REFERENCES ON TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" TO USER "DB2INST2" WITH GRANT OPTION;

GRANT SELECT, INSERT, UPDATE, ALTER, DELETE,
    INDEX, REFERENCES ON TABLE "MABAS"."BAS_MID_TRANS_LIST_CK_BAK" TO USER "MAPUB" WITH GRANT OPTION;

2.增加分区,注意这里的INCLUSIVE,exclusive,这时只有2011-01-31的数据可以进行insert

alter table MABAS.BAS_MID_TRANS_LIST_CK_BAK add partition LIST_CK_bak0131  STARTING '2011-01-31' INCLUSIVE ENDING  '2011-02-01' exclusive

3.insert 数据

insert into MABAS.BAS_MID_TRANS_LIST_CK_BAK select * from MABAS.BAS_MID_TRANS_LIST_CK where report_date=date('2011-01-31');

4.转移分区到临里表

alter table MABAS.BAS_MID_TRANS_LIST_CK_BAK detach partition LIST_CK_bak0131 into MABAS.BAS_MID_TRANS_LIST_CK_BAK1

5.删除生成的分区迁移表,注意这时分区表的分区(LIST_CK_bak0131)己经不存在了,如果要insert必须新增该分区

drop table MABAS.BAS_MID_TRANS_LIST_CK_BAK1

以下为应用写的shell脚本,并参照syscat.datapartitions 进行判断,如果有则删除分区,否则进行新建,以下为具体的脚本。

# 创建人员:    姜春涛  
# 创建日期:    2011-05-21
# 脚本描述:   删除表数据通用程序
# 修改人员:   
# 修改日期:   
# 修改原因:   
# 版本说明:    v1.0
# 公司名称:    宇信易诚
. /home/odSUSEr1/.profile
#配置文件
SYSNAME=GDBMA
MADS_HOME=/home/odsuser1/gdbma/etl
#DS Config
DSConfigFile=$MADS_HOME/dsconfig_gdbma
#MARPT ETL2数据库
#DB信息
DBNAME=`awk 'FS="=" {if ($0~/^MABASDBName/) print $2}' $DSConfigFile`
DBUSR=`awk 'FS="=" {if ($0~/^MABASDBUser/) print $2}' $DSConfigFile`
DBPWD=`awk 'FS="=" {if ($0~/^MABASDBPassword/) print $2}' $DSConfigFile`
DBSCHEMA=`awk 'FS="=" {if ($0~/^MABASDBSchema/) print $2}' $DSConfigFile`
DBPWD=`$MADS_HOME/Encrypt/discrypt.sh $DBPWD`
dbname=$DBNAME
user=$DBUSR
passwd=$DBPWD
#连接数据库
db2 connect to $DBNAME user $DBUSR using $DBPWD >/dev/null
db2 set schema=$DBSCHEMA;
#传递参数
JOB_NAME=$1
DELETE_DATE=$2
#DELETE_DBSCHEMA=$1
#DELETE_TAB=$2
#DELETE_COL=$3
#DELETE_TYPE=$4
#DELETE_DATE=$5
#DELETE_VALUE=$6

delete_tab="select SCH_NAME,TAB_NAME,IF_PARTITION,TAB_DATE,DEL_VALUE  from mabas.s_job_info_m t where JOB_NAME = '"$JOB_NAME"'  "
DEL_DATA=`db2 -t "$delete_tab"`
if [ $? -ne 0 ]
then
echo "$SDATA"
fi
echo "$DEL_DATA"  | sed -e '4,/^$/!d;/^$/d'|
#循环读取job,然后调度
while read SCH_NAME TAB_NAME IF_PARTITION TAB_DATE DEL_VALUE
do
#判断删除方式
#分区字段拼写
if [ "$IF_PARTITION" = 'Y' ]
   then
       #分区进行拼写
       partiton_name=`db2 -tx "select upper('p'||replace(varchar(date('"$DELETE_DATE"')),'-','')) from sysibm.sysdummy1 "`
       tmp_tab=`db2 -tx "select upper('tmp'||substr(replace(replace(varchar(current timestamp),'-',''),'.',''),5,10)) from sysibm.sysdummy1 "`
       #判断分区是否存在
       vi_result=`db2 -tx "select count(*) from syscat.datapartitions t where tabschema = upper('"$SCH_NAME"')
                                                      and tabname = upper('"$TAB_NAME"')
                                                      and datapartitionname=upper('"$partiton_name"') "`
       #对分区进行操作
       if [ "$DEL_VALUE" = '-' ]
           then
       #判断分区是否存在
           if [ $vi_result -ne 0 ]
               then
               #进行分区数据到临时表
               alter_parition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME detach partition $partiton_name into $SCH_NAME.$tmp_tab"`
               #删除临时表
               drop_tmp=`db2 -tx "drop table $SCH_NAME.$tmp_tab"`
               #分区结束
               partition_end=`db2 -tx "select varchar(date('"$DELETE_DATE"') +1 days) from sysibm.sysdummy1"`
               #增加分区
               add_partition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME add partition $partiton_name  STARTING '"$DELETE_DATE"' INCLUSIVE ENDING  '"$partition_end"' exclusive "`
           else
               #分区结束
               partition_end=`db2 -tx "select varchar(date('"$DELETE_DATE"') +1 days) from sysibm.sysdummy1"`
               #增加分区
               add_partition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME add partition $partiton_name  STARTING '"$DELETE_DATE"' INCLUSIVE ENDING  '"$partition_end"' exclusive "`
           fi
       else
           if [ $vi_result -ne 0 ]
               then
               #进行分区数据到临时表
               drop_parition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME detach partition $partiton_name into $SCH_NAME.$tmp_tab "`
               #备份临时表中不属于该条件的数据
               delete_tab_date=`db2 -tx "delete from $SCH_NAME.$tmp_tab where "$DEL_VALUE" "`
               #分区结束
               partition_end=`db2 -tx "select varchar(date('"$DELETE_DATE"') +1 days) from sysibm.sysdummy1"`
               #增加分区
               add_partition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME add partition $partiton_name  STARTING '"$DELETE_DATE"' INCLUSIVE ENDING  '"$partition_end"' exclusive "`
               #insert 不符合删除条件的数据
              
               insert_date=`db2 -tx "insert into $SCH_NAME.$TAB_NAME select * from $SCH_NAME.$tmp_tab "`
           else
               #分区结束
               partition_end=`db2 -tx "select varchar(date('"$DELETE_DATE"') +1 days) from sysibm.sysdummy1"`
               #增加分区
               add_partition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME add partition $partiton_name  STARTING '"$DELETE_DATE"' INCLUSIVE ENDING  '"$partition_end"' exclusive "`
           fi
       fi
else
#直接删除表
       if [ "$DEL_VALUE" = "-" ]  
           then
           delete_tab_data=`db2 -tx "delete from $SCH_NAME.$TAB_NAME where date("$TAB_DATE") = DATE('"$DELETE_DATE"') "`
       else
            delete_tab_data=`db2 -tx "delete from $SCH_NAME.$TAB_NAME where date("$TAB_DATE") = DATE('"$DELETE_DATE"') and $DEL_VALUE "`
       fi
fi
done

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor