『壹』 db2中建立了分區表,如何快速清除一個分區的
參考吧,網路一下那含轎州么多答案談蔽。帆鉛網頁鏈接
『貳』 DB2分區表的創建,求各位幫幫忙 解釋一下下面這段代碼。俺實在看不懂了
在蘆脊 schema RHETTE上創建 test4表,含 col1,col2兩型渣欄位陪租滲,按 col2進行表分區,DATAPARTION1 存放 col2 1-10
DATAPARTION2 存放 col2 11-20
『叄』 db2 表數據分區 是否可以修改
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 進行判斷,如果有則刪除分區,否則進行新建,以下為具體的腳本。
. /home/odSUSEr1/.profile#配置文件SYSNAME=GDBMAMADS_HOME=/home/odsuser1/gdbma/etl#DS ConfigDSConfigFile=$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=$DBNAMEuser=$DBUSRpasswd=$DBPWD#連接資料庫db2 connect to $DBNAME user $DBUSR using $DBPWD >/dev/nulldb2 set schema=$DBSCHEMA;#傳遞參數JOB_NAME=$1DELETE_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 ]thenecho "$SDATA"fiecho "$DEL_DATA" | sed -e '4,/^$/!d;/^$/d'|#循環讀取job,然後調度while read SCH_NAME TAB_NAME IF_PARTITION TAB_DATE DEL_VALUEdo#判斷刪除方式#分區欄位拼寫if [ "$IF_PARTITION" = 'Y' ] then #分區進行拼寫 partiton_name=`db2 -tx "select upper('p'||replace(varchar(date('"$DELETE_DATE"')),'-','')) from sysibm.sysmmy1 "` tmp_tab=`db2 -tx "select upper('tmp'||substr(replace(replace(varchar(current timestamp),'-',''),'.',''),5,10)) from sysibm.sysmmy1 "` #判斷分區是否存在 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.sysmmy1"` #增加分區 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.sysmmy1"` #增加分區 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.sysmmy1"` #增加分區 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.sysmmy1"` #增加分區 add_partition=`db2 -tx "alter table $SCH_NAME.$TAB_NAME add partition $partiton_name STARTING '"$DELETE_DATE"' INCLUSIVE ENDING '"$partition_end"' exclusive "` fi fielse #直接刪除表 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 "` fifidone
『肆』 db2資料庫 怎麼按日或者按月 自動創建表分區
CREATE TABLE lineitem(l_orderkey DECIMAL(10,0) NOT NULL, l_cpartkey INTEGER, l_suppkey INTEGER, l_linenumber INTEGER, l_quantity DECIMAL(12,2), l_extendedprice DECIMAL(12,2), l_discount DECIMAL(12,2), l_tax DECIMAL(12,2), l_returnflag CHAR(1), l_linestatus CHAR(1), l_shipdate DATE, l_commitdate DATE, l_receiptdate DATE, l_shipinstruct CHAR(25), l_shipmode CHAR(10), l_comment VARCHAR(44)) PARTITION BY RANGE(l_shipdate) (STARTING MINVALUE, STARTING '1/1/2000' ENDING '31/12/2020' EVERY 1 YEAR(按月就是 MONTH), ENDING MAXVALUE);
『伍』 如何建立DB2分區資料庫
1) 了解分區定義
分區定義可以從節點配置文件(db2nodes.cfg)得到,其位於實例所有者的主目錄中,它包含一些配置信息,告訴 DB2 有哪些伺服器參與分區資料庫環境的實例。分區資料庫環境中的每個實例都有一個 db2nodes.cfg 文件。對於每個參與實例的伺服器,db2nodes.cfg 文件必須包含一個條目。當創建實例時,會自動創建 db2nodes.cfg 文件並對擁有實例的伺服器添加條目。這里我們假設有4個分區。
2) 創建資料庫
create db dpfdb;
默認會創建3個分區組IBMCATGROUP(只在0號分區上)
IBMTEMPGROUP ,IBMDEFAULTGROUP(在所有分區上),如果用戶沒有創建其他分區組,所創建的表空間會默認放在IBMDEFAULTGROUP上
3) 創建分區組
我們在 1到3號分區建立一個分區組
CREATE DATABASE PARTITION GROUP USERGROUP ON DBPARTITIONNUMS(1,2,3);
4) 創建表空間
CREATE TABLESPACE TS IN USERGROUP MANAGED BY DATABASE USING (file '/DB2containers/TScontainer $N' 10000)
有4個containers被創建
/DB2containers/TScontainer0 - on DATABASE PARTITION 0
/DB2containers/TScontainer1 - on DATABASE PARTITION 1
/DB2containers/TScontainer2 - on DATABASE PARTITION 2
/DB2containers/TScontainer3 - on DATABASE PARTITION 3
5) 創建表
CREATE TABLE DPFTABLE (ID INTEGER NOT NULL,
NAME CHAR(20) NOT NULL)
IN TS
PARTITIONING KEY (ID) USING HASHING;
『陸』 db2 查看錶分區語句與創建表分區
創建分區:ALTER TABLE tablename ADD PARTITION statis_month STARTING'201301'
INCLUSIVE ENDING '20090102' INCLUSIVE。
查詢分區select * from syscat.datapartitions t where tabschema =模式名 and tabname = 表名
『柒』 db2 分區表分區鍵還要建索引嗎
DB2 V9.7 使用分區索引的索引組織方案,即索引數據根據表的分區方喚察案分布到多個索引分區和侍茄中,每個索引分區都談純只引用對應數據分區中的錶行。
創建方式:Create index idx_t1_l_partkey on t1(l_partkey) partitioned;
『捌』 db2表分區
表分區可擴展表的存儲能力、對於大量數據表使用分區欄位可提高查詢效率。
create table IXP.ESBTLOG(
UUID VARCHAR(32),
UUID_POS VARCHAR(20),
MSG_ID VARCHAR(100),
ESB_CODE VARCHAR(20) ,
CLI_ID CHAR(3),
CLI_ID_BOCS CHAR(2),
SERV_ID CHAR(3),
SERV_TRAN_CODE VARCHAR(20),
SERV_TCODE VARCHAR(20),
ESB_RET_CODE VARCHAR(20),
ERR_MSG VARCHAR(200),
TRACE_NO VARCHAR(32),
TRAN_DATE CHAR(8),
TRAN_TIME CHAR(6),
TXN_STATUS CHAR(1),
JOURNAL_NO VARCHAR(32),
EXT_UUID VARCHAR(32),
TXN_TOKEN VARCHAR(32),
NODE_NAME VARCHAR(32),
TRAN_BEGIN_TIME TIMESTAMP,
TRAN_END_TIME TIMESTAMP,
SP1_BEGIN_TIME TIMESTAMP,
SP1_END_TIME TIMESTAMP,
USER_BOCS VARCHAR(20),
WKSTATION VARCHAR(20),
RET_TRAN_DATE VARCHAR(20),
RET_TRAN_TIME VARCHAR(20),
RET_AC_DATE VARCHAR(20),
RET_TRACE_NO VARCHAR(20),
AP_TRACE_NO VARCHAR(20),
BR_NO VARCHAR(20),
TM_SMP TIMESTAMP default current timestamp
)PARTITION BY RANGE(TM_SMP)
(
PARTITION PARTMAX starting '9999-12-31-00.00.00.000000' INCLUSIVE ending MAXVALUE EXCLUSIVE
)in USERSPACE1
注意:倒數三行是創建分區表的關鍵,此例中此TM_SMP為鄭清爛分區欄位,第一個分區名為PARTMAX,從'9999-12-31-00.00.00.000000'到無窮大都是它的分區范圍。我們還可以在EXCLUSIVE後面繼 續定義其它的分區,定義規則喊漏與定義PARTMAX分區相同,當然定義每個分區需以逗號分隔。最後一行in USERSPACE1表示分正或區表創建在USERSPACE1表空間中。我們還能添加,刪除表分區。
db2 "alter table ESBTLOG add partition part20181118 STARTING '2018-11-18-00.00.00.000000' INCLUSIVE ENDING '2018-11-19-00.00.00.000000' EXCLUSIVE"
其思路是:detach分區到一張臨時表(該操作會創建臨時表,臨時表已存在會報錯,detach是分離分區的意思),再將臨時表刪除。
db2 "alter table ixp.esbtlog detach partition PART20181118 into table tb_temp"
db2 "drop table tb_temp"
A. 查詢該表共有多少分區
db2 "select substr(TABNAME ,1,11) t_name,substr(DATAPARTITIONNAME ,1,12) part_name,DATAPARTITIONID ,substr(LOWVALUE,1,28) low_value,substr(HIGHVALUE,1,28) high_value from syscat.datapartitions where tabname='ESBTLOG' order by datapartitionid"
B. 顯示每個分區有多少數據(沒有數據的不會顯示)
db2 "select substr(tm_smp,1,10) as tran_day,count(*) from esbtlog where 1=1 group by substr(tm_smp,1,10)"
C. 查詢某個分區有多少數據量
db2 "select count(*) from ixp.esbtlog where substr(tm_smp,1,10)='2019-01-08'"
1. 請注意上方例子中的名稱和分區范圍。
2. 要想將一個已存在的表改成分區表,需要先將原表數據保存下來,刪除表。再新建分區表,設定好表的分區及其范圍,將原數據導入進去。