下面,為了提高數據庫性能,我們將不同的分區(qū)放到不同的表空間下。首先創(chuàng)建6個表空間,3個數據表空間,3個索引表空間:
db2 "create tablespace ts_dat managed by database using (file '/home/db2inst1/data/ts_dat' 100M)"
db2 "create tablespace ts_dat1 managed by database using (file '/home/db2inst1/data/ts_dat1' 100M)"
db2 "create tablespace ts_dat2 managed by database using (file '/home/db2inst1/data/ts_dat2' 100M)"
db2 "create tablespace ts_idx managed by database using (file '/home/db2inst1/data/ts_idx' 100M)"
db2 "create tablespace ts_idx1 managed by database using (file '/home/db2inst1/data/ts_idx1' 100M)"
db2 "create tablespace ts_idx2 managed by database using (file '/home/db2inst1/data/ts_idx2' 100M)"
在靈丘等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網站設計制作、網站制作 網站設計制作定制網站,公司網站建設,企業(yè)網站建設,品牌網站設計,全網整合營銷推廣,成都外貿網站建設公司,靈丘網站建設費用合理。
首次執(zhí)行后不需要執(zhí)行
--DDL for Bufferpools--
CREATE BUFFERPOOL "BP32K_DATA" SIZE 32768 PAGESIZE 32768;
CREATE BUFFERPOOL "BP32K_INDEX" SIZE 32768 PAGESIZE 32768;
--Minic Storage Groups--
ALTER STOGROUP "IBMSTOGROUP" OVERHEAD 6.725000 DEVICE READ RATE 100.000000 DATA TAG NONE SET AS DEFAULT;
創(chuàng)建用戶臨時表空間
CREATE USER TEMPORARY TABLESPACE "USER_TMP" IN DATABASE PARTITION GROUP IBMDEFAULTGROUP PAGESIZE 32768 MANAGED BY SYSTEM
--修改表空名TABLESPACE "USER_TMP"
USING ('/home/db2inst1/data/usrtmp')----修改路徑'/home/db2inst1/data/usrtmp'
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
創(chuàng)建系統(tǒng)臨時表空間
CREATE TEMPORARY TABLESPACE "TMP_SYSTEM" PAGESIZE 32768 MANAGED BY SYSTEM
USING ('/home/db2inst1/data/SYStmp')
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
創(chuàng)建普通表空間
CREATE LARGE TABLESPACE "IDM_DATATABLE" PAGESIZE 32768 MANAGED BY DATABASE
----修改表空間TABLESPACE "IDM_DATATABLE"
USING (file '/home/db2inst1/data/IDM_datatable' 100M)-----修改路徑和大小
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
CREATE TABLESPACE "IDMINDX" PAGESIZE 32768 MANAGED BY DATABASE
USING (file '/home/db2inst1/data/IDMINDX' 100M)
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
CREATE LARGE TABLESPACE "IDMDTL1" PAGESIZE 32768 MANAGED BY DATABASE
USING (file '/home/db2inst1/data/IDMDTL1' 100M)
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
CREATE LARGE TABLESPACE "IDMDTL2" PAGESIZE 32768 MANAGED BY DATABASE
USING (file '/home/db2inst1/data/IDMDTL2' 100M)
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
CREATE LARGE TABLESPACE "IDMDTL3" PAGESIZE 32768 MANAGED BY DATABASE
USING (file '/home/db2inst1/data/IDMDTL3' 100M)
EXTENTSIZE 64
PREFETCHSIZE AUTOMATIC
BUFFERPOOL "BP32K_DATA"
OVERHEAD 6.725000
TRANSFERRATE 0.320000
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY OFF;
分區(qū)表類型
1、自動分區(qū)
2、手工分區(qū)
PARTITION BY RANGE(分區(qū)列)
(PART 分區(qū)名 STARTING 開始 ENDING 結束 IN 表空間 INDEX IN 索引鎖在表空間)
省略starting,下一個上一個結束
STARTING(2017,1) ENDING (2017,6),
ENDING (2017, 9),
ENDING (2017, 12),
ENDING (2018, 12));
省略ending,結束為上一個的開始
PARTITION BY RANGE(COL35)
(PART PART0 STARTING('2017-01-01') IN IDMDTL1,
PART PART1 STARTING('2017-04-01') IN IDMDTL2,
PART PART2 STARTING('2017-07-01') IN IDMDTL3,
PART PART3 STARTING('2017-10-01') ENDING('2017-12-31') IN IDMDTL1,
EEXCLUSIVE 分區(qū)2018-02-01這個排除在外
PART IDM_TRANSDTL_PART201801 STARTING('2018-01-01') ENDING('2018-02-01') EXCLUSIVE IN IDMDTL1 INDEX IN IDMINDX,
表分區(qū)鍵不支持的數據類型如下:
用戶定義的類型(結構化)
LONG VARCHAR
LONG VARCHAR FOR BIT DATA
BLOB
BINARY LARGE OBJECT
CLOB
CHARACTER LARGE OBJECT
DBCLOB
LONG VARGRAPHIC
REF
C變長字符串
Pascal變長字符串
XML
db2 "describe DATA PARTITIONS for table parttabtest02 show detail"
實例:
實例1、表空間均勻分布在表空間上
CREATE TABLE parttabtest01
( ID INTEGER NOT NULL,
SALES_PERSON VARCHAR(50),
REGION VARCHAR(50),
SALES_DATE DATE)
IN ts_dat1,ts_dat2 ------指定表在分區(qū)上
INDEX IN ts_idx ------指定索引分區(qū)上
PARTITION BY RANGE(SALES_DATE)
( STARTING MINVALUE, STARTING '1/1/2012' ENDING '12/31/2012' EVERY 1 MONTH,ENDING MAXVALUE);
實例2、
類似的,我們也可以在分區(qū)后面加表空間名稱,顯式地為分區(qū)指定所在的表空間。
對于建立分區(qū)語句里沒有指定表空間的分區(qū),使用CREATE TABLE里指定的表空間。
CREATE TABLE parttabtest02
( ID INTEGER NOT NULL,
SALES_PERSON VARCHAR(50),
REGION VARCHAR(50),
SALES_DATE DATE)
IN TS_DAT
INDEX IN TS_IDX
PARTITION BY RANGE(SALES_DATE)
( PART PJAN STARTING '1/1/2017' ENDING '3/31/2017' IN TS_DAT1 INDEX IN TS_IDX1,-----指定分區(qū)在指定的表空間上
PART PFEB STARTING '4/1/2017' ENDING '7/31/2017' IN TS_DAT1 INDEX IN TS_IDX1,
PART PMAR STARTING '8/1/2017' ENDING '12/31/2017' IN TS_DAT2 INDEX IN TS_IDX2,
PART PAPR STARTING '1/1/2018' ENDING '11/31/2018' )
實例3、
如果選擇使用CREATE TABLE語句的EVERY子句來自動生成數據分區(qū),那么只能將一列用作表分區(qū)鍵。
如果選擇通過在CREATE TABLE語句的PARTITION BY子句中指定每個范圍來手動生成數據分區(qū),
那么可以將多個列用作表分區(qū)鍵,如以下示例所示:
CREATE TABLE parttabtest03
( ID INTEGER NOT NULL,
SALES_PERSON VARCHAR(50),
REGION VARCHAR(50),
SALES_YEAR INT,
SALES_MONTH INT)
PARTITION BY RANGE(SALES_YEAR, SALES_MONTH)
( STARTING(2017,1) ENDING (2017,6),
ENDING (2017, 9),
ENDING (2017, 12),
ENDING (2018, 12));
實例4、
CREATE TABLE parttabtest04
( ID INTEGER NOT NULL,
SALES_PERSON VARCHAR(50),
REGION VARCHAR(50),
SALES_DATE DATE,
SALES_MONTH GENERATED ALWAYS AS (month(SALES_DATE)))
PARTITION BY RANGE(SALES_MONTH)
( STARTING FROM 1 ENDING AT 12 EVERY 1 );
表名
CREATE TABLE IDM_TRANSDTL (
COL1 VARCHAR(22) NOT NULL,
COL2 VARCHAR(8) NOT NULL,
COL3 VARCHAR(8) NOT NULL,
COL4 VARCHAR(6) NOT NULL,
COL5 VARCHAR(6) ,
COL6 VARCHAR(6) ,
COL7 VARCHAR(40) ,
COL8 VARCHAR(4) ,
COL9 VARCHAR(3) ,
COL10 VARCHAR(8) ,
COL11 VARCHAR(22) ,
COL12 VARCHAR(4) ,
COL13 VARCHAR(14) ,
COL14 VARCHAR(5) ,
COL15 VARCHAR(30) ,
COL16 VARCHAR(100) ,
COL17 VARCHAR(20) ,
COL18 VARCHAR(30) ,
COL19 VARCHAR(30) ,
COL20 VARCHAR(20) ,
COL21 VARCHAR(1) ,
COL22 VARCHAR(1) ,
COL23 VARCHAR(20) ,
COL24 VARCHAR(2) ,
COL25 VARCHAR(14) ,
COL26 VARCHAR(2) ,
COL27 VARCHAR(10) ,
COL28 VARCHAR(256) ,
COL29 VARCHAR(20) ,
COL30 VARCHAR(100) ,
COL31 VARCHAR(100) ,
COL32 VARCHAR(100) ,
COL33 VARCHAR(100) ,
COL34 VARCHAR(100) ,
COL35 DATE NOT NULL WITH DEFAULT CURRENT DATE)
INDEX IN IDMINDX PARTITION BY RANGE(COL35)
(PART PART0 STARTING('2017-01-01') IN IDMDTL1,
PART PART1 STARTING('2017-04-01') IN IDMDTL2,
PART PART2 STARTING('2017-07-01') IN IDMDTL3,
PART PART3 STARTING('2017-10-01') ENDING('2017-12-31') IN IDMDTL1,
PART IDM_TRANSDTL_PART201801 STARTING('2018-01-01') ENDING('2018-02-01') EXCLUSIVE IN IDMDTL1 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201802 STARTING('2018-02-01') ENDING('2018-03-01') EXCLUSIVE IN IDMDTL2 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201803 STARTING('2018-03-01') ENDING('2018-04-01') EXCLUSIVE IN IDMDTL3 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201804 STARTING('2018-04-01') ENDING('2018-05-01') EXCLUSIVE IN IDMDTL1 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201805 STARTING('2018-05-01') ENDING('2018-06-01') EXCLUSIVE IN IDMDTL2 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201806 STARTING('2018-06-01') ENDING('2018-07-01') EXCLUSIVE IN IDMDTL3 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201807 STARTING('2018-07-01') ENDING('2018-08-01') EXCLUSIVE IN IDMDTL1 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201808 STARTING('2018-08-01') ENDING('2018-09-01') EXCLUSIVE IN IDMDTL2 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201809 STARTING('2018-09-01') ENDING('2018-10-01') EXCLUSIVE IN IDMDTL3 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201810 STARTING('2018-10-01') ENDING('2018-11-01') EXCLUSIVE IN IDMDTL1 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201811 STARTING('2018-11-01') ENDING('2018-12-01') EXCLUSIVE IN IDMDTL2 INDEX IN IDMINDX,
PART IDM_TRANSDTL_PART201812 STARTING('2018-12-01') ENDING('2019-01-01') EXCLUSIVE IN IDMDTL3 INDEX IN IDMINDX)