You are on page 1of 8

------

To 1. 2. 3. 4.

create create database: Download the GSDB_Database.sql DDL Open a DB2 Command Window Navigate to the location of where you saved the GSDB_Database.sql file On the DB2 Command Window, type db2 -td~ -vf GSDB_Database.sql

db2 -td~ -vf /home/kjr/DB2Docs/DB2DB/GSDB_Database.sql Sometime you can gather information like that using the 'get snapshot' command s o, db2 get snapshot for application agentid <agentid> You can get the agentid from the: db2 list applications (show detail) command(s ). db2inst1@ubuntu:~$ db2 list applications show detail db2inst1@ubuntu:~$ db2 get snapshot for application agentid 38 db2inst1@ubuntu:~$ db2 list active databases -----------------------------------------------To get Database names in DB2 instace : db2inst1@ubuntu:~$ db2 list db directory db2inst1@ubuntu:~$ db2 GET CONNECTION STATE ---------------------------------------------* DB2 cheat sheet Some useful DB2 commands * DB2 System Commands DB2LEVEL -- checks version of DB2 installed. DB2ILIST -- lists all instances installed DB2CMD -- opens a command line processor DB2CC -- opens db2 control center DB2LICM -l -- gets db2 type. * Command Line Processor Commands DB2 LIST NODE DIRECTORY -- Lists all nodes DB2 CATALOG TCPIP NODE DB2NODE REMOTE MACHINE215 SERVER 50000 -- catalogs no de. In this case, node is db2Node on the machine with name machine215. Port is 50000. DB2 LIST DATABASE DIRECTORY -- list databases DB2 GET DB CFG FOR SAMPLE -- get configuration info for the SAMPLE db. DB2 CONNECT TO alexDB USER myuser USING mypass -- connect to db. In this cas e, database is alexdb, usern is myuser and password is mypass. DB2 DISCONNECT alexdb -- disconnects DB2 LIST APPLICATIONS SHOW DETAIL -- shows all running db's DB2 GET DBM CFG -- view authentication paramater (e.g. something like server _encrypt) DB2 UPDATE DBM CFG USING AUTHENTICATION SERVER_ENCRYPT -- alter the authenti cation mechanism to server_encrypt

DB2 GET AUTHORIZATIONS -- get authorisation level. * Database commands via Command Line Processor (CLP) DB2 DB2 DB2 DB2 GET DATABASE CONFIGURATION -- gets current database configuration VALUES CURRENT USER - - gets the current user VALUES CURRENT SCHEMA -- gets the current schema VALUES CURRENT QUERY OPTIMIZATION -- get query optimization level.

* Schemas DB2 SELECT SCHEMANAME FROM SYSCAT.SCHEMATA -- list all schemas DB2 VALUES CURRENT SCHEMA -- gets the current schema DB2 SET SCHEMA ALEXSCHEMA -- set schema * Tables DB2 LIST TABLES FOR schema_name -- list all tables for particular schema DB2 LIST TABLES SHOW DETAIL; -- show detail about tables DECLARE GLOBAL TEMPORARY TABLE -- declares a temporary table CREATE TABLE MQT AS (SELECT c.cust_name, c.cust_id, a.balance FROM customer c, account a WHERE c._cust_name IN ('Alex') AND a.customer_id - c.cust_id) DATA INITIALLY DEFERRED REFRESH DEFERRED -- Creates a materialised query table. In th is case the MQT is based on a join query from the customer and account table. * Tablespaces DB2 LIST TABLESPACES SHOW DETAIL -- show detail about table spaces SELECT * FROM SYSCAT.TABLESPACES; -- show what syscat has about tablespaces SELECT tbspace, bufferpoolid from syscat.tablespaces; -- get tablespace and bufferpoolid SELECT TABNAME FROM SYSCAT.TABLES WHERE TBSPACE=2; -- Check what TABLES are in tablespace where id = 2. * Constraints SELECT SELECT SELECT SELECT * Sequences CREATE SEQUENCE STESTRESULT AS INTEGER INCREMENT BY 1 START WITH 1 NO MINVAL UE NO MAXVALUE NO CYCLE CACHE 10 ORDER; -- Create Sequence starting with 1 whic h cache 10 values SELECT * FROM SYSCAT.SEQUENCES; -- Gets systcat info on sequences VALUES NEXT VALUE FOR MYSEQ; -- Gets next value from sequence myseq ALTER SEQUENCE MYSEQ RESTART WITH 11 INCREMENT BY 1 MAXVALUE 10000 CYCLE CAC HE 12 ORDER -- Changes MySeq sequence * Locksize SELECT TABNAME, LOCKSIZE FROM SYSCAT.TABLES WHERE TABNAME = ' EMPLOYEES'; - Check locksize which can be tablespace, table, partition, page, row - (usually row). * * * * FROM FROM FROM FROM SYSCAT.TABCONST; -- Table constraints SYSCAT.CHECKS; -- Colum checks SYSCAT.COLCHECKS; -- Column constraints SYSCAT.REFERENCES; -- Referential constraints

* Bufferpools SELECT bpname, npages, pagesize from syscat.bufferpools -- get useful buffer pool info. SELECT buffer.bufferpoolid, buffer.bpname, buffer.npages, buffer.pagesize, t ablespace.tbspace, tablespace.tbspaceid from syscat.bufferpools buffer, syscat.t ablespaces tablespace where tablespace.bufferpoolid = buffer.bufferpoolid; -- g ets buffer pool and corresponding tablespace info. * Indexes SELECT * FROM SYSCAT.INDEXES -- show all indexes SELECT COLNAMES, TABNAME, INDEXTYPE, CLUSTERRATIO, CLUSTERFACTOR FROM SYSCAT .INDEXES WHERE TABNAME = 'TPERSON'; -- some useful columns * Functions SELECT * FROM SYSCAT.FUNCTIONS; -- check what functions DB has. * SYSDUMMY1 commands SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1; -- gets current date. SELECT HEX(36) FROM SYSIBM.SYSDUMMY1; -- same as VALUES HEX(36) SELECT XMLCOMMENT ('This is an XML comment') FROM SYSIBM.SYSDUMMY1; * Runstats RUNSTATS ON TABLE TAUSER1.TOSUSER FOR INDEXES ALL; -- runstats for all inde xes * Checking the last time runstats was run... SELECT CARD, STATS_TIME FROM SYSCAT.TABLES WHERE TABNAME = 'TOSUSER'; SELECT NLEAF, NLEVELS, FULLKEYCARD, STATS_TIME, TABNAME, INDNAME FROM SYSCAT .INDEXES WHERE TABNAME = 'TOSUSER'; The following catalog columns can be queried to determine if RUNSTATS has been p erformed on the tables and indexes: * If the CARD column of the SYSCAT.TABLES view displays a value of -1, or the STATS_TIME column displays a NULL value for a table, then the RUNSTATS utility has not run for that table. * If the NLEAF, NLEVELS and FULLKEYCARD columns of the SYSCAT.INDEXES view di splay a value of -1, or the STATS_TIME column displays a NULL value for an index , then the RUNSTATS utility has not run for that index. --------------------------------------------------------* db2pd to monitor locks Let's first assume that someone calls you because their application appears to b e hung. Perhaps you think there may be a locking issue, so the first command to run is * db2pd -db sample -applications

This will list out all the applications currently running in the database named SAMPLE. I have shown here a condensed view (note that the db2pd output has a lot of "stuff" that you won't care about and won't be able to decipher since it's i ntended for support). In the view below you can see that the application with Ap pHandl 494 is in a lock wait state. Applications: AppHandl NumAgents CoorTid Status Appid 494 1 5204 Lock-wait *LOCAL.DB2.070320185650 490 1 5648 UOW-Waiting *LOCAL.DB2.070320185556

If you want to see what lock this application is waiting on you can run the comm and

* db2pd -db dbname -locks wait By using the wait keyword the tool will only dump out information on locks that are actually being waited on. If you omit this keyword, db2pd will dump out all the contents of the lock list so you can narrow down the info by just using the wait option. As you can see the lock being waited on is a Row level lock and bot h transactions are trying to get an X lock on that row. One has the lock granted (G) to it while the other transaction is the one that is waiting (W). If you wa nted to determine who is the application for TranHdl 6 and 2 you can use the -tr ansactions option on db2pd but I won't go into that here. Locks: TranHdl Lockname Type 6 030011000600A00A0000000052 Row 2 030011000600A00A0000000052 Row Mode ..X ..X Sts W G

Now you want to know what object the contention is on exactly. The Lockname is a ctually a hex representation of the physical object that is being waited on. But rather than have you decipher the object name you can use the keyword showlocks in the db2pd command as shown below and it will dump out for you the object inf ormation. db2pd -db dbname -locks showlocks wait Locks: TranHdl Type Mode Sts 6 Row ..X W TbspaceID 3 TableID 17 Page 2720 Slot 6 2 Row ..X G TbspaceID 3 TableID 17 Page 2720 Slot 6

If you don't know your tables by there tablespace id and table id (come on isn't that something you stay up at nights memorizing :-) then you can quickly use th e command

db2pd -db dbname -tcbstats

As described in my previous posting to see what table is the one listed in the l ock wait output. Now for those that want to get really fancy and want to dump out the data that e xists on that page in that slot to see what data is being locked (it's probably much more useful to look at the SQL statements that are locking each other out b ut hey, you come here to find out stuff that's under the hood so that's what you get :-) If you run DB2DART dbname /DD you can dump out the data page you want to view. Y ou will be asked for the table_id, the tablespace_id, the page you want to dump out (you need to put a 'p' suffix on the page 2720 from the above output) and ho w many pages you want to dump (in this case just 1). You will then get the data in the page dumped and you can see what data is in slot 6. This stuff is not for the weak at heart. Personally I prefer the admin views to look at this stuff through SQL but for those that really want to or need to dive deep db2pd is for you! ------------------------------------------------------------From the locks output you can see the transaction handle (TranHdl). You can find the corresponding transaction handle in the db2pd -transactions. Or you can even run db2pd -transaction and it dumps information about that transaction. From here you can see the appli cation handle which you can feed into a get snapshot for applications (using tha t app handle) to see the statement text for that statement)

Hi Chris, db2pd is giving lot of informations. How can we interpret this? For eg: db2pd -db dbname -locks showlocks wait will give the following. Locks: TranHdl Type Mode Sts 6 Row ..X W TbspaceID 3 TableID 17 Page 2720 Slot 6 2 Row ..X G TbspaceID 3 TableID 17 Page 2720 Slot 6 For example,What are the different values for Sts columns? Can I get the link to the document? Thanks, Reji Chris Eaton Aug 7, 2007 Hi Reji, The Sts column is telling you what the "status" is of the lock request. So in th e above example a transaction with transaction handle (TranHdl) 2 has a Row lock ed in eXclusive mode (and the tableid, page and slot can tell you what table and even what row is locked). The G tells you that the row lock is "Granted" to tra nsaction 2. The first row tells you that transaction 6 also is requesting an exc

lusive (i.e. updating the row) lock on the same row and it is in a status of Wai ting (W). So when transaction 2 commits or rollsback then it will release it's l ock and transaction 6 can be granted that row lock. I put a quick awk script together to pull out the statements for lock waits, it helped us identify statements which were soaking up time via lock waits in an OL TP system. db2pd -db due01 -app -transactions -dyn -locks showlocks wait | awk -f qry_lock_ list The awk script contains the following BEGIN { inLock = 0; inApp = 0; inTrans = 0; inStatement = 0; LockID = 0; } { if ( $0 == "Locks:" ) inLock = 1; else if ( $0 == "Applications:" ) { inLock = 0; inApp = 1; } else if ( $0 == "Transactions:" ) { inApp = 0; inTrans = 1; } if ( $0 == "Dynamic Cache:" ) { inTrans = 0; } else if ( $0 == "Dynamic SQL Statements:" ) { inStatement = 1; } if ( $0 == "Dynamic SQL Environments:" ) { inStatement = 0; } else { if (inLock == 1 && $2 > 0 ) lockTran[lockID++] = $2; if (inTrans == 1 && $4 > 0) appTrans[$4] = $2; if (inApp == 1 && $2 > 0) { appAnch[$2] = $7; sqlID[$2] = $8;} if (inStatement == 1 && $2 > 0) appStatement[$2,$3] = $0; } } END { printf("%d\n", lockID); for (i=1 ; i { trn =lockTran[i]; app =appTrans[trn]; anch = appAnch[app]; usqlid = sqlID[app]; statement = appStatement[anch,usqlid]; printf("%d\t%d\t%d\t%s\n", trn, app, anch, statement); } } ----------------------------------------To make David L. script work, change for (i=1 ; i { to for (i=1 ; i

-------------------------------------Debug Slow Code in DB2 with DB2 Event Monitors http://www.performancewiki.com/db2-event-monitors.html

db2monitor A small DB2 performance monitoring program. -Shows active connections with lock information. -SQLs in db cache,. -database snapshots. -database objects information. -list history. -Can help resolve locking problems, identify worst performing queries. Very easy to use and lightweight. http://chuzhoi_files.tripod.com/ ---------------------------------------------------------------*db2top You could also try db2top. It alows quick identification of locks and blocking sessions. http://www.alphaworks.ibm.com/tech/db2top -------------------------------------------------------When I use "db2 get snapshot for locks for db-alies" to get following message: Database Lock Snapshot * db2 get snapshot for locks * "db2 get snapshot for locks for db-alies" A deadlock is not a type of lock (like a S, U, or X lock). It is a condition th at occurs when two or more applications are already holding one lock (successful ly) and they each need to obtain another lock which another application holds. I n this case, the deadly embrace will never be resolved, until DB2 returns a -911 reason code 2 to one of the applications. DB2 arbitrarily picks one of the appl ications involved in a deadlock as its victim for the -911, so that the other ap plication can continue. A lock timeout (-911 reason code 68) is not quite the same as a deadlock. This o ccurs when one application holds another lock that is need by a second applicati on. Eventually a lock timeout would resolve itself when the first application fi nishes, but most DBA's set a time limit for the wait time, after which the -911 is returned to the second application. I understand what you mean. ** From the "db2 get snapshot for locks for db-alies" command, I can't get the deadlock information because the return information from this c ommand does not show what the lock are obtaining. Also I know that DB2 can automatically balance this confict. However, I met with the case that there were many locks so that my application b

ecame slow. ** I had to use "db2 force application all" to close all the locks. So I want to watch the deadlocks in order to avoid this case just like in the OR ACLE. Could you tell me the methods to watch the existed deadlock? thanks a lot ** You can use the Event Monitor to capture information about deadlocks (and oth er events). The Event Monitor can be activated via the Control Center or by issu ing the CREATE EVENT MONITOR SQL statement. http://www-106.ibm.com/developerwork...10wilkins.html is a very good artcile on the subject --------------------------------------Hi,

db2bp is a one kind of background process for the DB2 CLP (DB2 Command Line Proc essor). It is the process which actually connects to the database. DB2 CLP allow s OS as well as DB2 commands/statements, this background process is required.

Please refer this useful link to get more information [about DB2 UDB Processes|http://www.ibm.com/developerworks/data/library/techarti cle/0304chong/0304chong.html]. -------------------------------* db2inst1@ubuntu:~$ db2stop SQL1025N The database manager was not stopped because databases are still activ e. * db2inst1@ubuntu:~$ db2 force application all DB20000I The FORCE APPLICATION command completed successfully. DB21024I This command is asynchronous and may not be effective immediately. db2inst1@ubuntu:~$ db2 list applications SQL1611W No data was returned by Database System Monitor. db2inst1@ubuntu:~$ db2stop SQL1064N DB2STOP processing was successful. ----------------------------

You might also like