0% found this document useful (0 votes)
21 views

DB 2 Basic

The document discusses various DB2 commands for database, schema, tablespace and bufferpool creation and management. It includes commands to check tablespace usage and generate commands to lower high water marks. It also shows queries to view tablespace, bufferpool and database configuration details.

Uploaded by

Atul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

DB 2 Basic

The document discusses various DB2 commands for database, schema, tablespace and bufferpool creation and management. It includes commands to check tablespace usage and generate commands to lower high water marks. It also shows queries to view tablespace, bufferpool and database configuration details.

Uploaded by

Atul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

############################# db2 basic kt ###########################

1) database creation
db2 create db TESTDB
DB20000I The CREATE DATABASE command completed successfully.

2) db2 activate db TESTDB

db2 list active databases

Active Databases

Database name = TESTDB


Applications connected currently = 0
Database path =
/db2home/db2inst1/db2inst1/NODE0000/SQL00010/MEMBER0000/

3) db2 deactivate db TESTDB


DB20000I The DEACTIVATE DATABASE command completed successfully.
[db2inst1@TCS-PCM-216 ~]$ db2 list active databases
SQL1611W No data was returned by Database System Monitor.

4) db2 "select * From syscat.bufferpools"

5) db2 "select * From syscat.tablespaces"

6) db2 create schema TEST


DB20000I The SQL command completed successfully

db2 "select schemaname from syscat.schemata"

SCHEMANAME
-----------------------------------------------------------------------------------
---------------------------------------------
ACCT

7) db2 create bufferpool reshmabp1 size automatic PAGESIZE 8K

db2 "create tablespace reshmTB1_8K PAGESIZE 8k bufferpool reshmabp1"

8) db2stop force

9) db2start
###########################################################################
#####################################################
TABLESPACE IMP COMMANDS #
#####################################################
1) Check tablespace high water mark :-

db2 "select substr(tablespace_name,1,20) as Tablespace,


sum(used_pages*page_size/(1024*1024)) as
Used_MB,sum(high_water_mark*page_size/(1024*1024)) as
High_Water_Mark_MB,sum(total_pages*page_size/(1024*1024)) as Total_MB,
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) as Percent_Used from table(snapshot_tbs_cfg('${DBNAME}',-2)) as
tbspace where tablespace_type = 0 group by tablespace_name having
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) >= 0 and sum(total_pages*page_size/(1024*1024)) <> 0"
TABLESPACE USED_MB HIGH_WATER_MARK_MB TOTAL_MB
PERCENT_USED
-------------------- -------------------- -------------------- --------------------
--------------------
SYSCATSPACE 103 103 128
80
SYSTOOLSPACE 0 0 32
0
USERSPACE1 1086 1325 1344
80
USERSPACE_8K 0 0 5
0

4 record(s) selected.

2) command to generate low water mark commands


db2 -x "select 'alter tablespace '||trim(tbspace)||' lower high water mark ;' from
syscat.tablespaces where tbspacetype='D'"
alter tablespace SYSCATSPACE lower high water mark ;
alter tablespace USERSPACE1 lower high water mark ;
alter tablespace USERSPACE_8K lower high water mark ;
alter tablespace SYSTOOLSPACE lower high water mark ;

3) again cross verify with first command , used space and high water mark should be
same.

db2 "select substr(tablespace_name,1,20) as Tablespace,


sum(used_pages*page_size/(1024*1024)) as
Used_MB,sum(high_water_mark*page_size/(1024*1024)) as
High_Water_Mark_MB,sum(total_pages*page_size/(1024*1024)) as Total_MB,
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) as Percent_Used from table(snapshot_tbs_cfg('${DBNAME}',-2)) as
tbspace where tablespace_type = 0 group by tablespace_name having
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) >= 0 and sum(total_pages*page_size/(1024*1024)) <> 0"

TABLESPACE USED_MB HIGH_WATER_MARK_MB TOTAL_MB


PERCENT_USED
-------------------- -------------------- -------------------- --------------------
--------------------
SYSCATSPACE 103 103 128
80
SYSTOOLSPACE 0 0 32
0
USERSPACE1 1086 1086 1344
80
USERSPACE_8K 0 0 5
0

4 record(s) selected.

#######################
Tablespace Utilization

db2 "select substr(tbsp_name,1,20) as TBSP_NAME , tbsp_type as TBSP_TYPE ,


int(tbsp_total_size_kb/1024) as TBSP_SIZE_MB , smallint(tbsp_utilization_percent)
as UTIL_PCNT , int(tbsp_free_size_kb / 1024) as FREE_SIZE_MB,DBPARTITIONNUM from
sysibmadm.tbsp_utilization where TBSP_TYPE='DMS' order by DBPARTITIONNUM"
TBSP_NAME TBSP_TYPE TBSP_SIZE_MB UTIL_PCNT FREE_SIZE_MB DBPARTITIONNUM
-------------------- ---------- ------------ --------- ------------ --------------
SYSCATSPACE DMS 128 91 11 0
USERSPACE1 DMS 64 73 17 0
USERSPACE_8K DMS 5 7 4 0
SYSTOOLSPACE DMS 32 1 31 0
###########################
1) Check tablespace high water mark :-

db2 "select substr(tablespace_name,1,20) as Tablespace,


sum(used_pages*page_size/(1024*1024)) as
Used_MB,sum(high_water_mark*page_size/(1024*1024)) as
High_Water_Mark_MB,sum(total_pages*page_size/(1024*1024)) as Total_MB,
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) as Percent_Used from table(snapshot_tbs_cfg('${DBNAME}',-2)) as
tbspace where tablespace_type = 0 group by tablespace_name having
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) >= 0 and sum(total_pages*page_size/(1024*1024)) <> 0"

TABLESPACE USED_MB HIGH_WATER_MARK_MB TOTAL_MB


PERCENT_USED
-------------------- -------------------- -------------------- --------------------
--------------------
SYSCATSPACE 103 103 128
80
SYSTOOLSPACE 0 0 32
0
USERSPACE1 1086 1325 1344
80
USERSPACE_8K 0 0 5
0

4 record(s) selected.
##################################
2) command to generate low water mark commands
db2 -x "select 'alter tablespace '||trim(tbspace)||' lower high water mark ;' from
syscat.tablespaces where tbspacetype='D'"
alter tablespace SYSCATSPACE lower high water mark ;
alter tablespace USERSPACE1 lower high water mark ;
alter tablespace USERSPACE_8K lower high water mark ;
alter tablespace SYSTOOLSPACE lower high water mark ;

##################################
3) again cross verify with first command , used space and high water mark should be
same.

db2 "select substr(tablespace_name,1,20) as Tablespace,


sum(used_pages*page_size/(1024*1024)) as
Used_MB,sum(high_water_mark*page_size/(1024*1024)) as
High_Water_Mark_MB,sum(total_pages*page_size/(1024*1024)) as Total_MB,
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) as Percent_Used from table(snapshot_tbs_cfg('${DBNAME}',-2)) as
tbspace where tablespace_type = 0 group by tablespace_name having
(sum(used_pages*page_size/(1024*1024))*100)/(sum(total_pages*page_size/
(1024*1024))) >= 0 and sum(total_pages*page_size/(1024*1024)) <> 0"

TABLESPACE USED_MB HIGH_WATER_MARK_MB TOTAL_MB


PERCENT_USED
-------------------- -------------------- -------------------- --------------------
--------------------
SYSCATSPACE 103 103 128
80
SYSTOOLSPACE 0 0 32
0
USERSPACE1 1086 1086 1344
80
USERSPACE_8K 0 0 5
0

4 record(s) selected.
##################################
db2 "SELECT SUBSTR(TBSPACE, 1, 16)TBSPACE, TBSPACETYPE, DATATYPE,
PAGESIZE,EXTENTSIZE, PREFETCHSIZE FROM SYSCAT.TABLESPACES"

TBSPACE TBSPACETYPE DATATYPE PAGESIZE EXTENTSIZE PREFETCHSIZE


---------------- ----------- -------- ----------- ----------- ------------
SYSCATSPACE D A 4096 4 -1
TEMPSPACE1 S T 4096 32 -1
USERSPACE1 D L 4096 32 -1
USERSPACE_8K D A 8192 16 16
TEMPSPACE_8K S T 8192 16 16
SYSTOOLSPACE D L 4096 4 -1
SYSTOOLSTMPSPACE S U 4096 4 -1

7 record(s) selected.
##################################
db2 " SELECT SUBSTR(TBSP_NAME, 1, 16)TBSP_NAME, MEMBER, TBSP_PAGE_SIZE,
TBSP_TOTAL_SIZE_KB,TBSP_UTILIZATION_PERCENT FROM SYSIBMADM.MON_TBSP_UTILIZATION"

TBSP_NAME MEMBER TBSP_PAGE_SIZE TBSP_TOTAL_SIZE_KB


TBSP_UTILIZATION_PERCENT
---------------- ------ -------------------- --------------------
------------------------
SYSCATSPACE 0 4096 131072
80.94
TEMPSPACE1 0 4096 4
100.00
USERSPACE1 0 4096 1376256
80.81
USERSPACE_8K 0 8192 5120
7.69
TEMPSPACE_8K 0 8192 8
100.00
SYSTOOLSPACE 0 4096 32768
2.05
SYSTOOLSTMPSPACE 0 4096 4
100.00

##################################
db2 "SELECT TBSP_NAME,TBSP_STATE FROM SYSIBMADM.TBSP_UTILIZATION"
TBSP_NAME TBSP_STATE
---------------- --------------------
SYSCATSPACE NORMAL
TEMPSPACE1 NORMAL
USERSPACE1 NORMAL
USERSPACE_8K NORMAL
TEMPSPACE_8K NORMAL
SYSTOOLSPACE NORMAL
SYSTOOLSTMPSPACE NORMAL
MY_DMS_TABLESPACE BACKUP_IN_PROGRESS
MY_AUTOSTOR_TBSP BACKUP_IN_PROGRESS
###################################
db2 "select substr(tbsp_name,1,30) as Tablespace_Name, tbsp_type as Type,
substr(tbsp_state,1,20) as Status, (tbsp_total_size_kb / 1024 ) as Size_Meg,
decimal((flo
at(tbsp_total_size_kb - tbsp_free_size_kb)/ float(tbsp_total_size_kb))*100,3,1)as
Percent_used_Space, int((tbsp_free_size_kb) / 1024 )as Meg_Free_Space from
sysibmadm.tbsp_utilization where
tbsp_type='DMS'"

TABLESPACE_NAME TYPE STATUS SIZE_MEG


PERCENT_USED_SPACE MEG_FREE_SPACE
------------------------------ ---------- -------------------- --------------------
------------------ --------------
SYSCATSPACE DMS NORMAL 128
91.2 11
USERSPACE1 DMS NORMAL 64
73.4 17
USERSPACE_8K DMS NORMAL 5
10.0 4
SYSTOOLSPACE DMS NORMAL 32
2.0 31

####################################
db2 "SELECT SUBSTR(TBSP_NAME, 1, 16)TBSP_NAME, DBPARTITIONNUM, TBSP_PAGE_SIZE,
TBSP_TOTAL_PAGES,TBSP_USED_PAGES, TBSP_FREE_PAGES FROM SYSIBMADM.TBSP_UTILIZATION"

TBSP_NAME DBPARTITIONNUM TBSP_PAGE_SIZE TBSP_TOTAL_PAGES TBSP_USED_PAGES


TBSP_FREE_PAGES
---------------- -------------- -------------- --------------------
-------------------- --------------------
SYSCATSPACE 0 4096 32768
26520 6244
TEMPSPACE1 0 4096 1
1 0
USERSPACE1 0 4096 344064
278016 66016
USERSPACE_8K 0 8192 640
48 576
TEMPSPACE_8K 0 8192 1
1 0
SYSTOOLSPACE 0 4096 8192
168 8020
SYSTOOLSTMPSPACE 0 4096 1
1 0

###################################
db2 " SELECT SUBSTR(TBSP_NAME, 1, 16)TBSP_NAME, MEMBER, TBSP_PAGE_SIZE,
TBSP_TOTAL_SIZE_KB,
TBSP_UTILIZATION_PERCENT FROM SYSIBMADM.MON_TBSP_UTILIZATION"
TBSP_NAME MEMBER TBSP_PAGE_SIZE TBSP_TOTAL_SIZE_KB TBSP_UTILIZATION_PERCENT
---------------- ------ -------------------- --------------------
------------------------
SYSCATSPACE 0 8192 163840 82.73
TEMPSPACE1 0 8192 8 100.00
USERSPACE1 0 8192 32768 51.18
IBMDB2SAMPLEREL 0 8192 32768 14.96
IBMDB2SAMPLEXML 0 8192 32768 35.43
SYSTOOLSPACE 0 8192 32768 2.73
MY_SMS_TABLESPAC 0 8192 48 100.00
MY_DMS_TABLESPAC 0 8192 24000 9.89
MY_AUTOSTOR_TBSP 0 8192 32768 2.36
###################################
db2 "SELECT SUBSTR(TBSPACE, 1, 16)TBSPACE, TBSPACETYPE, DATATYPE, PAGESIZE,
EXTENTSIZE, PREFETCHSIZE FROM SYSCAT.TABLESPACES"
TBSPACE TBSPACETYPE DATATYPE PAGESIZE EXTENTSIZE PREFETCHSIZE
---------------- ----------- -------- ----------- ----------- ------------
SYSCATSPACE D A 8192 4 -1
TEMPSPACE1 S T 8192 32 -1
USERSPACE1 D L 8192 32 -1
IBMDB2SAMPLEREL D L 8192 32 -1
IBMDB2SAMPLEXML D L 8192 32 -1
SYSTOOLSPACE D L 8192 4 -1
MY_SMS_TABLESPAC S A 8192 32 -1
MY_DMS_TABLESPAC D A 8192 32 -1
MY_AUTOSTOR_TBSP D A 8192 32 -1
###################################
SOME USEFUL COMMANDS FOR TABLESPACES
1. To show all tablespaces
db2 "LIST TABLESPACES SHOW DETAIL"
###################################
2. To get usage of dms tablespaces in MB
db2 "SELECT SUBSTR(TBSP_NAME, 1, 16)TBSP_NAME, DBPARTITIONNUM ,
(TBSP_TOTAL_SIZE_KB/1024)
TBSP_TOTAL_SIZE_MB, (TBSP_FREE_SIZE_KB /1024) TBSP_FREE_SIZE_MB,
TBSP_UTILIZATION_PERCENT FROM
SYSIBMADM.TBSP_UTILIZATION WHERE TBSP_TYPE='DMS' "
###################################
3. To check the state of the tablespace details
db2 "SELECT SUBSTR(TBSP_NAME, 1, 16)TBSP_NAME, DBPARTITIONNUM ,
SUBSTR(CONTAINER_NAME,1,10)
CONTAINER_NAME, CONTAINER_TYPE, TOTAL_PAGES, FS_ID, FS_TOTAL_SIZE_KB,
FS_USED_SIZE_KB FROM
SYSIBMADM.CONTAINER_UTILIZATION"
###################################
4.To check which tablespaces are currently being backed up (during whole database
backup process)
db2 "SELECT TBSP_NAME FROM SYSIBMADM.TBSP_UTILIZATION WHERE
TBSP_STATE='BACKUP_IN_PROGRESS'"
###################################
5. To add 1000 pages to each container of a tablespace
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE EXTEND (ALL 1000)"
###################################
6. To add 1000 Pages to just one container c00
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE EXTEND (FILE 'C:\db2\sample\
MY_DMS_TABLESPACE\C00'
1000)"
###################################
7. To add 1000 pages to container c00 and 2000 pages to c01
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE EXTEND (FILE 'C:\db2\sample\
MY_DMS_TABLESPACE\C00' 1000,
FILE 'C:\db2\sample\MY_DMS_TABLESPACE\C01' 2000)"
###################################
8. To reduce 1000 pages from container c00
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE REDUCE (FILE 'C:\db2\sample\
MY_DMS_TABLESPACE\C00' 1000)"
###################################
9. To change the prefetchsize to 64
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE PREFETCHSIZE 20"
###################################
10. To check which tablespaces use AUTO STORAGE
db2 "SELECT TBSP_NAME FROM SYSIBMADM.TBSP_UTILIZATION WHERE
TBSP_USING_AUTO_STORAGE=1"
###################################
11. To check the AUTO STORAGE parameters of a tablespace
db2 "SELECT TBSP_MAX_SIZE, TBSP_INCREASE_SIZE, TBSP_INCREASE_SIZE_PERCENT,
TBSP_AUTO_RESIZE_ENABLED FROM
SYSIBMADM.TBSP_UTILIZATION WHERE TBSP_NAME='MY_AUTOSTOR_TBSP' "
###################################
12. To change the INCREASE_SIZE to 64K
db2 "ALTER TABLESPACE MY_AUTOSTOR_TBSP INCREASESIZE 64 K "
###################################
12. To change the INCREASE_SIZE to 5%
db2 "ALTER TABLESPACE MY_AUTOSTOR_TBSP INCREASESIZE 5 PERCENT "
###################################
13. To convert a REGULAR tablespace to LARGE
db2 "ALTER TABLESPACE MY_AUTOSTOR_TBSP CONVERT TO LARGE"
###################################
14. To turn ON filesystem caching,
db2 "ALTER TABLESPACE MY_AUTOSTOR_TBSP FILE SYSTEM CACHING "
###################################
15. To add a new container to the tablespace
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE ADD (FILE 'C:\db2\sample\MY_DMS_TABLESPACE\
C002' 2000)"
###################################
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE ADD (FILE 'C:\db2\sample\MY_DMS_TABLESPACE\
C003' 2M) "
(notice that 2M signifies 2 MB)
###################################
16. To drop a tablespace
db2 "DROP TABLESPACE MY_TBSP”
###################################
17. To bakup a tablespace
db2 "BACKUP DATABASE SAMPLE TABLESPACE MY_DMS_TABLESPACE ONLINE "
###################################
18. To bring a tablespace online
db2 "ALTER TABLESPACE MY_DMS_TABLESPACE SWITCH ONLINE"

###################################
ALTER TABLESPACE TS LOWER HIGH WATER MARK
#######################################to lower max size
db2 "alter tablespace USERSPACE1 REDUCE MAX"

You might also like