search
HomeDatabaseMysql Tutorialdb2 delete 大表不写日志方法

dbname=$DBNAMEuser=$DBUSRpasswd=$DBPWD#连接数据库db2 connect to $DBNAME user $DBUSR using $DBPWD gt;/dev/nulldb2 set sc

最近由于项目需要,用shell程序批量删除业务表数据,但还需要按业务需求保留业务历史数据,由于项目中用的是db2,db2在删除数据时会产生大量的日志,会把日志文件充满,会报57011错误.通过在网上查找一些资料,最终在不改变原表结构参数的基础上,减少其他们人员的工作量的基础上动态调整参数。

以下为具体操作步骤:

-- db2 delete 大表不写日志操作
1.update command options using c off  -- //关闭自动提交
2.alter table MARPT.RPT_DIM_U_ORG_INX_M_CURR_CUS_PRO_TBK  activate not logged initially //设置不记日志
3.delete from MARPT.RPT_DIM_U_ORG_INX_M_CURR_CUS_PRO_TBK -- 删除数据
4.commit//手动提交
5.update command options using c on//打开自动提交

相关操作说明;

1.alter table testdeletetab ACTIVATE  NOT  LOGGED  INITIALLY,设置表操作不记日志,这条命令只在一个事务里有效,,遇到commit之后就失效了,这个很需要关注,因为有的时候我们的连接都是设置的自动提交,所以虽然设置了不记日志,但是并没有起到作用。

2.可以通过相关的命令查看当前命令参数

db2 list command options

下边为相应的shell脚本,可以参照一下

. /home/odSUSEr1/.profile
#ODS RUN ALL GDBMA JOBS
#GDBMA
#2011-3-16
#删除表参数
WORK_DATE=$2
TAB_NAME=$1
#
SYSNAME=GDBMA
if [ "$TAB_NAME#" -eq "#" ]
then
echo "the job do not delete the table data!! "
else
MADS_HOME=/home/odsuser1/gdbma/etl
#DS Config
DSConfigFile=$MADS_HOME/dsconfig_gdbma
#MARPT ETL2数据库
#DB
DBNAME=`awk 'FS="=" {if ($0~/^MARPTDBName/) print $2}' $DSConfigFile`
DBUSR=`awk 'FS="=" {if ($0~/^MARPTDBUser/) print $2}' $DSConfigFile`
DBPWD=`awk 'FS="=" {if ($0~/^MARPTDBPassword/) print $2}' $DSConfigFile`
DBSCHEMA=`awk 'FS="=" {if ($0~/^MARPTDBSchema/) 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;
ssql="SELECT SCHEDULE,KEEP_DATE,SCH_NAME,TAB_DATE,NO_LOG  from "$DBSCHEMA".S_TAB_INFO where tab_name='"$TAB_NAME"' "
SDATA=`db2 -t "$ssql"`
if [ $? -eq 0 ]
then
echo " the job drop data start!"
else
echo "this query false!!!"
fi
echo "$SDATA"  | sed -e '4,/^$/!d;/^$/d'|
#循环读取job,然后调度
while read SCHEDULE KEEP_DATE SCH_NAME TAB_DATE NO_LOG
do
echo "$NO_LOG"
if [ "$NO_LOG" = 'Y' ]
      then
          #参数调整(取消自动提交)
          COMMIT_OFF=`db2 -a "update command options using c off"` 
          #激活不写日志
          LOG_OFF=`db2 -a "alter table "$DBSCHEMA"."$TAB_NAME"  activate not logged initially"` 
    # 清数据开始
    if [ "$SCHEDULE" = 'M' ]
        then
        if [ "$KEEP_DATE" = 1 ]
            then
            delete_table="delete from "$DBSCHEMA"."$TAB_NAME" "
        else
           v_tx_date=`db2 -x "select  DATE(SUBSTR(varchar(date('"$WORK_DATE"')),1,8)||'01') -(int(trim('"$KEEP_DATE"'))-2) MONTHS -1 DAYS from sysibm.sysdummy1 "`
           echo "$v_tx_date"
         delete_table="delete from "$DBSCHEMA"."$TAB_NAME" where date("$TAB_DATE") = date('"$v_tx_date"')"
       fi
    fi
   
    if [ "$SCHEDULE" = 'D' ]
        then
        if [ "$KEEP_DATE" = 1 ]
            then
            delete_table="delete from "$DBSCHEMA"."$TAB_NAME" "
        else
            v_tx_date=`db2 -x "select date('"$WORK_DATE"') -(int(trim('"$KEEP_DATE"'))*31) days from sysibm.sysdummy1 "`
          delete_table="delete from "$DBSCHEMA"."$TAB_NAME" where date("$TAB_DATE") = date('"$v_tx_date"')"
       fi
    fi
    DELDATA=`db2 -a $delete_table`
         echo "$DELDATA" |  sed -n -e 's/^.*sqlcode: \([-,0-9][0-9]*\).*/\1/p' |  read SQLCODE
         if [ $SQLCODE -ge 0 ]
          then
          echo "the job delete table sucessfull"
         else
        echo "the job delete table false"
         fi
   
     #提交
       COMMIT_DATE=`db2 -a "commit"`
       #设置自动提交
       COMMIT_ON=`db2 -a "update command options using c off"`
else
     if [ "$SCHEDULE" = 'M' ]
        then
        if [ "$KEEP_DATE" = 1 ]
            then
            delete_table="delete from "$DBSCHEMA"."$TAB_NAME" "
        else
            v_tx_date=`db2 -x "select ( DATE(SUBSTR(varchar(date('"$WORK_DATE"')),1,8)||'01') -(int(trim('"$KEEP_DATE"'))-2) MONTHS -1 DAYS)  from sysibm.sysdummy1"` 
          delete_table="delete from "$DBSCHEMA"."$TAB_NAME" where date("$TAB_DATE") = date('"$v_tx_date"')"
       fi
    fi
   
    if [ "$SCHEDULE" = 'D' ]
        then
        if [ "$KEEP_DATE" = 1 ]
            then
            delete_table="delete from "$DBSCHEMA"."$TAB_NAME" "
        else
           v_tx_date=`db2 -x "select date('"$WORK_DATE"') -(int(trim('"$KEEP_DATE"'))*31) days from sysibm.sysdummy1 "`
         delete_table="delete from "$DBSCHEMA"."$TAB_NAME" where date("$TAB_DATE") = date('"$v_tx_date"')"
       fi
    fi
    DELDATA=`db2 -a $delete_table`
         echo "$DELDATA" |  sed -n -e 's/^.*sqlcode: \([-,0-9][0-9]*\).*/\1/p' |  read SQLCODE
         if [ $SQLCODE -ge 0 ]
          then
          echo "the job delete table sucessfull"
         else
        echo "the job delete table false"
         fi
fi

done
fi

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
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment