Sunteți pe pagina 1din 232

IBM Software Information Management

An IBM Proof of Technology

IBM DB2 10.1 Administration for the Experienced SQL Server DBA
Lab Exercises

An IBM Proof of Technology


PoT.IM.07.1.017.13

Version 6.1 August 25, 2012 Burt Vialpando & Vikram Khatri

Copyright IBM Corporation, 2006, 2012 US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

Contents
LAB 01. INSTANCE EXPLORATION ............................................................................................................................... 8 A. DB2 OPERATING SYSTEM DIRECTORIES AND FILES .............................................................................................. 8 B. START AND STOP A DB2 INSTANCE................................................................................................................... 12 C. CLP INTRODUCTION........................................................................................................................................ 13 D. DB2 WINDOWS REGISTRY ............................................................................................................................... 16 E. CREATE A NEW DB2 INSTANCE ........................................................................................................................ 17 F. CATALOG A NEW DB2 INSTANCE ...................................................................................................................... 19 G. EXTRA EXERCISES.......................................................................................................................................... 22 DATABASE CREATION AND EXPLORATION ............................................................................................... 24 A. GENERAL DB2 DATABASE COMMANDS ............................................................................................................. 24 B. THE DB2 SYSTEM AND LOCAL DATABASE DIRECTORIES ..................................................................................... 26 C. CREATING A DATABASE ................................................................................................................................... 27 D. EXPLORING VARIOUS DB2 DATABASE FUNCTIONALITY FEATURES ......................................................................... 30 E. ALTERING A TABLE .......................................................................................................................................... 31 DATA STUDIO.................................................................................................................................................. 32 A. LAUNCHING DATA STUDIO ................................................................................................................................ 32 B. CONNECTING TO AND MANAGING A DATABASE .................................................................................................... 33 CLPPLUS AND ORACLE COMPATIBILITY.................................................................................................... 55 A. LOGGING ON TO CLPPLUS............................................................................................................................... 55 B. INTERACTIVE CLPPLUS COMMANDS .................................................................................................................. 57 C. RUNNING A SCRIPT FILE IN BATCH MODE ............................................................................................................ 63 D. ORACLE COMPATIBILITY .................................................................................................................................. 66 SECURITY ........................................................................................................................................................ 77 A. INSTANCE LEVEL SECURITY.............................................................................................................................. 77 B. DATABASE LEVEL SECURITY ............................................................................................................................ 80 B. DATABASE LEVEL SECURITY ............................................................................................................................ 80 D. USING THE SYSTEM CATALOG SECURITY VIEWS ................................................................................................ 86 E. USER AND GROUP OVERVIEW .......................................................................................................................... 89 F. SCHEMAS ....................................................................................................................................................... 90 G. VIEWING DATABASE AUTHORITIES ...................................................................................................................... 91 H. COLUMN AND ROW LEVEL SECURITY .................................................................................................................. 92 I. EXTRA EXERCISE: DB2AUDIT AND AUDIT POLICY............................................................................................ 94 J. EXTRA EXERCISE: A QUICK LOOK INTO LABEL BASED ACCESS CONTROL ............................................................... 96 K. CLEANUP ....................................................................................................................................................... 96 AUTONOMIC COMPUTING ............................................................................................................................. 97 A. SELF TUNING MEMORY MANAGER (STMM)....................................................................................................... 97 B. AUTOMATIC STORAGE ................................................................................................................................... 101 C. RUN AUTOCONFIGURE (COMMAND LINE FOR CONFIGURATION ADVISOR)............................................................ 102 D. EXTRA EXERCISE: AUTOMATIC MAINTENANCE ................................................................................................. 103 E. EXTRA EXERCISE: STATISTIC PROFILING ......................................................................................................... 109 F. EXTRA EXCERCISE: SYSTOOLS SETUP ......................................................................................................... 110 G. CLEANUP ..................................................................................................................................................... 110 DEEP COMPRESSION .................................................................................................................................. 111 A. TABLE DEEP COMPRESSION........................................................................................................................... 111 C. CLEAN UP .................................................................................................................................................... 115 EXPLAIN FACILITIES AND THE OPTIMIZER............................................................................................... 116 A. CREATE EXPLAIN TABLES .............................................................................................................................. 116 B. UNION ALL VIEW EXAMPLE SETUP .................................................................................................................. 117 C. VISUAL EXPLAIN IN DATA STUDIO ................................................................................................................... 119 D. VISUAL EXPLAIN TUNING A QUERY ............................................................................................................... 123 E. USING DB2EXFMT EXPLAIN MULTIPLE QUERIES AT ONCE.................................................................................. 128 F. EXTRA EXERCISE USING DB2EXPLN ............................................................................................................. 129 H. EXTRA EXERCISES: REBIND AND ROW MOVEMENT ................................................................................... 131 WORKLOAD MANAGEMENT (WLM)............................................................................................................ 132 A. SETTING UP A CUSTOM WORKLOAD MANAGER ENVIRONMENT ............................................................................. 132 B. SETTING UP TO SIMULATE A WORKLOAD ........................................................................................................... 136 C. RUNNING A WORKLOAD AS USER DB2USER1 .................................................................................................... 138 D. RUNNING A SECOND WORKLOAD AS DB2COBRA ................................................................................................ 140 E. RUNNING A THIRD WORKLOAD AS USER DB2DEFAULT ........................................................................................ 142 F. WORKING WITH WLM EVENT MONITORS .......................................................................................................... 144

LAB 02.

LAB 03.

LAB 04.

LAB 05.

LAB 06.

LAB 07.

LAB 08.

LAB 09.

G. EXTRA EXERCISE: MISCELLANEOUS WLM LESSONS ........................................................................................ 145 H. CLEAN UP .................................................................................................................................................... 145 LAB 10. HIGH AVAILABILITY DISASTER RECOVERY (HADR)................................................................................ 146 A. HADR LAB OVERVIEW ............................................................................................................................... 146 B. SET UP HADR.............................................................................................................................................. 147 C. CREATE OBJECTS IN PRIMARY DATABASE AND MONITOR HADR ....................................................................... 148 CREATE SOME TABLES TO WORK WITH .................................................................................................................. 151 D. MONITOR HADR OUTSIDE THE GUI ................................................................................................................ 157 E. SWITCH ROLE OF DATABASES ........................................................................................................................ 158 F. CLEANUP...................................................................................................................................................... 160 LAB 11. IBM DB2 PUREXML....................................................................................................................................... 161 A. CREATE A SUPPORTING DATABASE AND AN XML READY TABLE ........................................................................ 161 B. INSERTING AND EXPLORING XML DATA ........................................................................................................... 163 C. IMPORTING AND EXPORTING XML DATA .......................................................................................................... 166 D. CREATING INDEXES ON XML DATA ................................................................................................................. 168 E. EXTRA EXERCISE: REGISTERING AN XSR........................................................................................................ 169 F. EXTRA EXERCISE: VALIDATING WITH THE XSR ................................................................................................. 170 G. CLEANUP ..................................................................................................................................................... 170 LAB 12. MULTIDIMENSIONAL CLUSTERS (MDCS) .................................................................................................. 171 A. CARS TABLES EXAMPLE SETUP .................................................................................................................... 171 B. EXPLORE MDC SPACE USAGE ....................................................................................................................... 172 C. EXPLORE MDC TABLE ORGANIZATION ............................................................................................................ 174 D. USING THE DB2BATCH BENCHMARKING UTILITY ................................................................................................ 176 E. EXTRA EXERCISE - MDC PERFORMANCE TIMING TEST ..................................................................................... 177 F. CLEANUP...................................................................................................................................................... 181 LAB 13. DATA MOVEMENT UTILITIES....................................................................................................................... 182 A. DB2RELOCATEDB (A TRANSPORTABLE DATABASE UTILITY) .............................................................................. 182 B. CONVERTING NON-UNICODE DATABASE TO UNICODE DATABASE ....................................................................... 187 C. SYSPROC.ADMIN_MOVE_TABLE ........................................................................................................... 191 C. DATA INGESTION........................................................................................................................................... 192 D. CLEANUP ..................................................................................................................................................... 193 LAB 14. BACKUP, RESTORE & RECOVERY............................................................................................................. 194 A. OFFLINE BACKUP .......................................................................................................................................... 194 B. OFFLINE RESTORE ........................................................................................................................................ 195 C. OFFLINE REDIRECTED RESTORE USING DATA STUDIO ...................................................................................... 197 D. CONFIGURE DATABASE LOGGING ................................................................................................................... 201 E. ONLINE DATABASE BACKUP ........................................................................................................................... 202 F. DATABASE RECOVER AND RESTORE / ROLL FORWARD TECHNIQUES .................................................................. 203 G. RECOVER A DROPPED TABLE......................................................................................................................... 204 H. RESTORE A HISTORY FILE ............................................................................................................................. 204 I. MOVING TABLE SPACE LOCATIONS WITH A SCRIPT (REDIRECTED RESTORE) ........................................................ 205 J. ONLINE TABLE SPACE BACKUP / RESTORE / RECOVERY .................................................................................... 207 K. INCREMENTAL BACKUP / RESTORE / RECOVERY ............................................................................................... 207 L. DELTA BACKUP / RESTORE / RECOVERY .......................................................................................................... 208 N. CLEANUP ..................................................................................................................................................... 208 LAB 15. MONITORING................................................................................................................................................. 209 A. SNAPSHOT MONITORING ................................................................................................................................ 209 B. EVENT MONITORING ...................................................................................................................................... 210 C. DATA STUDIO STORED PROCEDURE PROFILING ............................................................................................... 213 D. LIGHT WEIGHT MONITORING SQL FUNCTIONS AND DB2PD ............................................................................. 215 E. IBM DATA STUDIO WEB CONSOLE ................................................................................................................. 216 E. CLEANUP ..................................................................................................................................................... 221 LAB 16. ADDITIONAL MISC. TOPICS......................................................................................................................... 222 A. TEMPORAL DATA MANAGEMENT & TIME TRAVEL QUERY ................................................................................... 222 B. FEDERATION SIMPLE DB2 TO DB2 EXAMPLE ................................................................................................. 225 C. CLEANUP ..................................................................................................................................................... 227 OPTIONAL TOPICS VOTING SHEET ........................................................................................................................................ 231

[This page left intentionally blank]

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab Instructions
Setting up this Proof of Technology (PoT) if you are not using an IBM VMware approach
If you are not doing this lab in an IBM hosted Proof of Technology session on IBM prepared VM imaged computers, you can prepare for this PoT yourself by doing the following:

Create userid dbapot with password dbapot123. Log on as this user to complete these steps. Install IBM DB2 10.1 Enterprise Server Edition on a Windows XP Enterprise machine (as user dbapot) Use all the defaults while installing DB2 Copy the PoT scripts to your computer (if you did not get these, ask for them from your IBM rep) Run the setup script in a DB2 Command Line Processor (CLP) Command Window. Script is located at: C:\POT_DB2\00SETUP\Setup01.BAT. Install the free DB2 Data Studio product, using the defaults. http://www.ibm.com/developerworks/spaces/optim?pageid=649 Make sure your Windows Explorer can see hidden files and folders. From the Windows Explorer choose: Organize Folder and Search Options. Then choose the tab: View and check the button: Show Hidden Files and Folders.

Helpful icons:
You might want to create the following desktop icons to make your PoT experience easier: IBM Data Studio DB2 CLP Command Window Windows Command Prompt Windows Explorer Services

Windows users:
The Setup01.BAT script creates the following users: db2cobra, db2user1, db2user2 and db2default, each of these have passwords that match the names. Important note #1: YOU must first create a user dbapot yourself with password dbapot123. After you create this user, you should log on with this user to do the product install and setup. This is the user you should do the PoT lab exercises with. The other users will be invoked as needed by the scripts. Important note #2: You should also make sure all the users do not have an expiring password. To see this, at a Windows Command Prompt type: NET USER [username]. If any are expiring, change the password to not expire, or reset the password using the Windows Control Panel.

Page 6

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab Instructions

IBM Software

Frequently used terms:


Windows Command Prompt Windows Explorer DB2 CLP Command Window DB2 CLP Interactive Windows Operating System command shell Windows Operating System GUI DB2 command window running in the operating system (OS) shell DB2 command window running in its own shell

The script directories and files:


C:\POT_DB2\ 00SETUP\ 01INSTANCE\ 02DATABASE\ 03DATASTUDIO\ 04CLPPLUS\ 05SECURITY\ 06AUTONOM\ 07COMPRESS\ 08EXPLAIN\ 09WLM\ 10HADR\ 11PUREXML\ 12MDC\ 13DATA\ 14BACKUP\ 15MONITORING\ 16MISC\ 99INSTRUCTOR\ Main PoT directory Script to set up the PoT lab Instance creation and exploration and the CLP Database creation and exploration Data Studio CLPPlus and Oracle compatibility Security concepts and usage Autonomics DB2 Deep compression Explain facilities and the Optimizer DB2 Workload Manager High Availability Disaster Recovery IBMs XML concepts Multidimensional Clusters Data Movement utilities Backup and restore and logging Health Center and other monitoring facilities Multi-temperature storage, temporal data management, DB2 Cloud, etc. Other supporting documents for this PoT

Script file names use this convention: TTTTNN.EXT TTTTT NN EXT = Topic lesson (like Instance) = Number of script within that lab section = File extension that best describes the contents: CMD, BAT, DB2, SQL, DML, DCL, etc. For example: CMD files are the scripts that run other scripts.

*** End of Lab Instructions ***

Lab Instructions

Page 7

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01. Instance Exploration


A. DB2 Operating System Directories and Files
Let us explore how installed DB2 looks on your Windows operating system: ___1. Open a Windows Command Prompt:

___2.

To check where to find code is installed, type: db2set all (Note the location of DB2INSTPROF and DB2PATH )

The following are the kinds of DB2 profile registry variables and are parsed by DB2 in this order: Environment variables - denoted by [e] Node level registry Global level registry - denoted by [n] - denoted by [g] Instance level registry - denoted by [i]

The DB2INSTPROF may differ for you depending upon your operating system type. For example, in Windows XP, it is at: C:\Documents and Settings\All Users\Application Data\IBM\DB2\DB2COPY1\

Page 8

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

DB2 install path ___3. Using the Windows Explorer, find that DB2PATH location: Windows Explorer Program Files IBM SQLLIB Explore this directory and the BIN directory under it to see all the DB2 binaries

Instance default metadata path ___4. Using the Windows Explorer, find the DB2INSTPROF location by using: Windows Explorer ProgramData IBM DB2 DB2COPY1 DB2 and explore this directory. (Note, this will be different for Windows XP)

If not using the PoT VM image, to see this normally hidden directory, in the Windows Explorer use: Organize Folder and search options Show hidden files & folders.

Lab 01 Instance Exploration

Page 9

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___5.

Find the folder for instance called DB2: Find db2systm - this is the DBM config binary file. (Please, do not try to edit this.) Find db2diag.log in the DIAG000 directory - this is the diagnostic log file. (You can edit this.)

Database default metadata path ___6. ___7. Now lets explore the database files managed by instance DB2 Using the Windows Explorer, find: ___8. My Computer C: DB2 NODE0000 SQL0001

Look at directory SQL0001 This is the metadata folder for the SAMPLE database. Even though we are exploring instances in this lab, we still need to see how databases relate to instances and how each are placed on your operating system. Well show you later how you know which SQL subdirectory goes with which database

SQL0001 is the directory for the metadata for the SAMPLE database. This is different that the SAMPLE directory which is where the automatic storage containers are kept for the SAMPLE database.

Page 10

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

___9.

Browse directory SAMPLE to see the metadata files located there

___10. Browse the various directories and files. What do you suppose SQLOGSTREAM0000 is? (Hint: its a default directory) SQLOGSTREAM0000 is where your active log path defaults to unless you change the location ___11. SQLDBDIR, which is on the same level as SQL0001, is the location of the "local database directory" for all databases created under instance DB2. We will be learning about what a local database directory is later.

Lab 01 Instance Exploration

Page 11

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

B. Start and Stop a DB2 Instance


___12. Open a Windows Command Prompt

___13. To see the environment variable that shows the current instance, type: set db2instance ___14. To stop an instance, type: db2stop force This command works against the current instance only db2 stop dbm is an alternative using the CLP (try later when we use the CLP)

___15. To start an instance, type: db2start A DB2 instance always takes only a few seconds to start

___16. Open Services: Review all services starting with DB2

Find DB2 DB2COPY1 - DB2-0 right click on it stop Windows service naming convention starts all DB2 services with DB2, then it uses the copy of DB2 you are using (usually DB2COPY1).

___17. Start the service by using right click start or use db2start at the command line ___18. You can make Windows automatically start your instance using this screen, right click and choose properties ___19. Minimize your Services window for now; dont close it yet. UNIX notes on instances: The default instance created on a UNIX system is usually called db2inst1. To automatically start a DB2 instance upon server startup, use the db2iauto command.

Page 12

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

C. CLP Introduction
___20. This section is to demonstrate a command line interface for DB2. ___21. Here are terms used throughout these Proof of Technology lab workbooks: Windows Command Prompt Windows Explorer DB2 CLP Command Window DB2 CLP Interactive Windows Operating System command shell Windows Operating System GUI DB2 command window running in the OS shell DB2 command window running in its own shell

___22. To start the DB2 CLP Command Window, from the Windows Command Prompt, type: db2cmd This starts the DB2 shell over the OS shell often called the DB2 CLP Command Window This is one mode of the Command Line Processor (CLP)

___23. Close the Windows Command Prompt you opened first as we won't be needing it for now. ___24. From the new DB2 CLP Command Window you just opened, type: db2 get dbm cfg Notice the use of dbm (data base manager = instance) These are the instance configuration parameters

___25. To see what is in the instance memory, hit your up arrow key, then finish typing: db2 get dbm cfg show detail You will get an error stating you are attached to the instance, so you cannot get detailed information from it. The detail will show temporary changes to the cfg settings (if any) so you need to be attached to the instance to find this information The cfg it is referencing is simply information in a binary file and saved changes are persistent.

Lab 01 Instance Exploration

Page 13

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___26. To attach to a DB2 instance, type: db2 attach to db2 ___27. Try the previous command again (hint: hit your up arrow key): db2 get dbm cfg show detail ___28. To see the DB2 Administration Server (DAS) settings, type: db2 get admin cfg DAS is a special process that helps administrate communication between instances and databases, locally and remotely which has its own configuration settings For UNIX systems, a special DAS user needs to be created, normally called dasusr1

___29. Try the DB2 CLP in the interactive mode, type: db2 This is sometimes called the DB2 Command Line Processor or CLP, however, it really is just another mode of the DB2 CLP

___30. To verify your instance location, type: get instance Notice this time we did not need to preface the command with db2

___31. Run run a Windows command or launch a .bat file, preface it with a !, type: !dir ___32. To understand connections and applications, in the DB2 CLP interactive window type: connect to gsdb

Page 14

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

___33. Now, type: list active databases Active databases have at least one connection to them by at least one application This database is active because we connected to it

___34. Type: list applications Notice that GSDB has a db2bp.exe application name (the CLP)

___35. Type: list applications show detail This demonstrates there are other applications to the database started on our behalf when the database was activated

___36. Type: terminate This command does the following: resets the connection to GSDB, refreshes the directory cache, stops the background process and leaves the CLP interactive session

___37. We are no longer in the interactive mode, but we can still run DB2 commands. Try listing the active databases. What happens and why?

Lab 01 Instance Exploration

Page 15

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. DB2 Windows Registry


___38. Click the "Start" button ___39. In the search type: regedit then choose regedit.exe Do not make changes here!

___40. In this window, go to HKEY_LOCAL_MACHINE\SOFTWARE\IBM\DB2\PROFILES\ DB2 is the instance GLOBAL_PROFILE is for all DB2 instances on server Just note for now the various registers that are kept for DB2

Minimize this Registry Editor window for now, dont close it yet.

Page 16

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

E. Create a New DB2 Instance


DB2 instance creation on Windows: 1. 2. 3. 4. Requires administration authority to run Creates a Windows Service (with DB2 [instance_name node_name] convention) Creates an Instance directory (e.g. Documents and settings/DB2copy1/inst_name) Creates a Registry Key (regedit, HKEY_LOCAL)

DB2 Instance creation on UNIX: 1. Sets environment variables DB2INSTANCE and PATH Creates /SQLLIB subdirectory in $HOME of the SYSADM (e.g. /home/[inst-name] /sqllib) Configures communication based on servers available protocol

2. Creates files db2profile for main environment variable settings (bourne, bash, ksh) userprofile for upgrade and additional environment variable settings

3. Requires user id to: Run with root authority this is because it needs to access the db2icrt command in the DB2 install directory /opt/IBM/db2/[version]/instance/db2icrt on most UNIX platforms /opt/ibm/db2/[version]/instance/db2icrt on Linux Specifies an instance name that is tied to a particular user id which has its own home file system and home directory; this user id should also have the SYSADM authority

Create a DB2 instance and then verify it in Windows: ___41. In the DB2 CLP Command Window, change directories to C:\POT_DB2\01INSTANCE ___42. Type: db2icrt db2bkp

The instance is created. For details on this command see file: Instance_DB2ICRT_Command.txt

___43. Services window check: Go to Services window and click Action refresh Find new service db2bkp (its name is: DB2 - DB2copy1 - db2bkp) Start this instance (Hint: right click on it) Close the Services window now.

Lab 01 Instance Exploration

Page 17

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___44. Registry Editor check: In your Registry Editor window click: View refresh Find the new folder created for db2bkp Close the Registry Editor window Windows Explorer View refresh Find the instance metadata folder in: C:\Program Data\IBM\DB2\DB2COPY1 folder You should see your new instance folder DB2BKP

___45. Instance directory check:

___46. Perform other checks in a DB2 CLP Command Window to see you new instance, type: db2set l db2ilist db2 get instance (shows available instance profiles) (shows same thing, but with some options) (what is your default instance?)

set db2instance=db2bkp (change your current instance) db2 get instance (you should be pointing to DB2BKP instance now)

___47. Now lets explore dropping instances: db2icrt db2jnk db2idrop db2jnk (check to see if it created another instance) (gets rid of this instance thats it!)

This command only has one flag that it so force applications off before dropping, see: Instance DB2IDROP_Command.txt Creating and dropping instances in DB2 is just that easy

Page 18

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

F. Catalog a New DB2 Instance


Now that we know that the instance has been created, we need to catalog it. To catalog the instance manually with a script, go to a DB2 CLP Command Window: ___48. Make sure you change to directory C:\POT_DB2\01INSTANCE ___49. Review and execute this script: Instance01.CMD Note: to review scripts you can use more [scriptname] or use the Windows Explorer, click on the file and right click on [edit] To execute a CMD script simply type the name of the file (you can even leave off .CMD) All scripts in this PoT will be run from a DB2 CLP Command Window from now on unless otherwise specified (Hint use CD command)

___50. From the DB2 CLP Command Window type: db2 list node directory

___51. Notice the instance DB2 now knows about, or has cataloged, the DB2BKP instance. This means they can now communicate with each other and that any GUI tools launched can see both of them locally or remotely.

You can use the utilities db2cfexp AND db2cfimp to export and import all of your clients configuration settings. This allows you to easily share all of the catalog information without doing individual catalog commands. These commands also allow you to exchange protocol, registry, dbm cfg and ODBC/CLI settings. (ODBC is Open Database Connectivity.) Usage of this tool is demonstrated in the extra exercises of this lab.

Lab 01 Instance Exploration

Page 19

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___52. Let's set up the communication for our instance to make sure it is available for use by any TCPIP client: 1. Set up the DB2 Communication Protocol set db2instance=db2bkp db2 get instance db2start db2set db2comm=tcpip db2set -all

2. Set up the Instance Service Name (the TCPIP listening port) db2 update dbm cfg using svcename 50001 db2 get dbm cfg | findstr SVCE db2stop force db2start

Page 20

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

3. Attach to the instance db2 attach to db2bkp db2 get dbm cfg

On the DB2 server, we set the registry variable DB2COMM to the protocol(s) used for listening. TCP/IP is commonly used, while SSL is another option. Example: db2set DB2COMM=TCPIP If the protocol at the DB2 server instance is TCP/IP, then we can set the DBM configuration parameter SVCENAME too. Example Unix: update dbm cfg using svcename db2c_db2inst1 servicename | portnumber matches the entry in: etc/services

Example Windows: update dbm cfg using svcename 50000 db2c_db2 | 50000/tcp can match the entry in: %SystemRoot%\system32\drivers\etc\services After this, we should recycle the instance with db2stop, then db2start in order for it to take effect.

Lab 01 Instance Exploration

Page 21

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

G. Extra Exercises
DB2CFEXP (Connectivity Configuration Export Tool) usage example ___53. Review and execute script: Instance02.CMD Review files Instance02_OUTPUT.TXT DBMCFG, DBCFG and REG_VARIABLES Administration views usage example ___54. Review and execute script: Instance03.CMD which executes Instance04.SQL

DB2DIAG (diagnostic log tool) usage example ___55. Review and execute script: Instance05.CMD Review files Instance05a_OUTPUT.TXT and Instance05b_OUTPUT.TXT INSPECT and DB2INSPF usage example ___56. Review and execute script: Instance06.CMD Review files Instance06_OUTPUT.TXT DB2PD (Problem Determination) usage example ___57. Review and execute script: Instance07.CMD Review files Instance07_OUTPUT.TXT DB2GCF (Generic Control Facility) usage example ___58. Review and execute script: Instance08.CMD Review files Instance08_OUTPUT.TXT DB2SUPPORT usage example ___59. Review script and execute it: Instance09.CMD Executes problem analysis support tool, db2support, in a full mode Review file instance09_out.zip and then launch db2support.html

Page 22

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 01 Instance Exploration

IBM Software

CLP exercises ___60. How do you change your CLP interactive prompt to: Your next wish master? db2set db2_clpprompt=Your wish master? When this is done, change it back to the default with: db2set db2_clpprompt= ___61. Execute these commands from the CLP interactive mode: connect to sample select current date from sysibm.dummy ___62. When you get an error code, check it out by using the following: DB2 ? SQL0204N ___63. Fix this command by executing this: select current date from sysibm.sysdummy1

*** End of Instance Exploration Lab - by Burt Vialpando ***

Lab 01 Instance Exploration

Page 23

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 02. Database Creation and Exploration


A. General DB2 Database Commands
Lets explore the database called sample in instance called DB2. First lets make sure we are pointing to the appropriate instance. ___1. Open a DB2 CLP Command Window if you dont already have one open.

___2. ___3.

Change directory to C:\POT_DB2\02DATABASE In this window type: set db2instance=db2 db2 get instance

___4.

To make sure the instance is in an Oracle compatibility mode, do the following: db2set db2_compatibility_vector=ORA db2set all db2stop force db2start (sets the compatibility) (make sure the setting was done correctly) (stops the instance) (starts the instance)

___5.

To enter the DB2 CLP interactive mode type: db2

___6.

To see the database configuration parameters, type: get db cfg for sample

Page 24

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 02 Database Creation and Exploration

IBM Software

___7.

Connect to the SAMPLE database: connect to sample The complete syntax for the CONNECT would be: connect to sample user dbapot using dbapot123

___8.

To explore further, type these and hit [Enter] after each line: get db cfg get connection state list tables for schema dbapot

The default schema for a userid is usually the userid itself. Well explore in more detail later in the Security lab the differences between a user and a schema in DB2.

___9.

Continue exploring your database, type the following using [Enter] after each line: describe table department describe indexes for table department select * from employee describe output select * from employee values user select user from dual values current date select sysdate from dual runstats on table dbapot.employee reorgchk update statistics on schema dbapot quit

The above commands that used DUAL will only work when the registry variable is set to make our instance Oracle compatible. Well learn more about Oracle compatibility in a later lab.

Lab 02 Database Creation and Exploration

Page 25

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

B. The DB2 System and Local Database Directories


___10. In order to learn about DB2 database directories, use commands from the DB2 CLP Command Window: db2 list database directory This is a system database directory, which means a directory of all databases both remote and local that are cataloged for the current instance. The command: db2 list database directory on c: would give you a local database directory, which means all databases created on the drive path. This is only updated during a CREATE DATABASE or DROP DATABASE command.

___11. Connect to the GSDB database to prove that you can access it, type: db2 connect to gsdb ___12. Connect back to the SAMPLE database using the same background process: db2 connect to sample ___13. Uncatalog the GSDB database in the current instance: db2 uncatalog database gsdb ___14. Refresh the directory cache (and terminate the background process as well): db2 terminate ___15. List the system database directory again. Notice GSDB is not there: db2 list database directory ___16. List the local database directory db2 list database directory on c: ___17. Try to connect to the GSDB directory (notice you cannot): db2 connect to gsdb ___18. Recatalog database GSDB to make it available again: db2 catalog db gsdb ___19. Refresh the directory cache (and terminate the background process as well): db2 terminate ___20. Try to connect to the GSDB directory (notice now you can!): db2 connect to gsdb

Page 26

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 02 Database Creation and Exploration

IBM Software

C. Creating a Database
Creating a database managed by the instance DB2 ___21. To create a database in the instance DB2, try these commands: set db2instance=db2 db2 get instance db2 terminate db2 create database sample2 This will take a few minuteswhile this command is executing, see the explanation in the box below for what the CREATE DATABASE command is doing. Also, review the file Database_CREATE_DATABASE_Command.txt

The DB2 CREATE DATABASE command does the following: 1. 2. 3. 4. 5. 6. 7. Creates a subdirectory to hold the database metadata information Creates SYCATSPACE, TEMPSPACE1 and USERSPACE1 table spaces Creates system catalog tables in the SYSCATSPACE table space Creates SQLJ, SYSCAT, SYSFUN, SYSIBM, SYSIBMADM, SYSIBMINTERNAL, SYSIBMTS, SYSPROC, SYSPUBLIC and SYSSTAT schemas Grants SECADM, DBADM, DATAACCESS and ACCESSCTRL authorities to the database creator Grants selected database privileges to PUBLIC (unless specified as restricted) Defaults many things for you, but you can control these if you wish: Partition number Install path Database alias name Collating characteristics Codeset and territory Table space characteristics (types, sizes, containers, etc.) Database configuration parameters (using feature called autoconfigure which well go through later) Set up Automatic Storage and Automatic Resize defaults Many other things (see: Database CREATE_DATABASE_Command.txt)

___22. When the CREATE DATABASE command finishes, run this command next: db2 list db directory

Lab 02 Database Creation and Exploration

Page 27

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Creating a database managed by the instance DB2BKP ___23. Now lets create a database called BACKDB in a different instance: DB2BKP ___24. Review and run these scripts: Database01.CMD which executes Database02.DB2 Remember, it takes a few minutes to finish... while this is running.

___25. Execute these commands: db2 get instance db2 list db directory ___26. Notice database BACKDB is not in the directory for our instance DB2. This is because we have not yet cataloged it to instance DB2. (It is however cataloged to the instance DB2BKP which manages / owns it.) ___27. Lets catalog database BACKDB to instance DB2: db2 catalog db backdb at node db2bkp with 'my first db catalog' db2 terminate

___28. Review if it is in the system catalog of our instance DB2: db2 list db directory

Page 28

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 02 Database Creation and Exploration

IBM Software

___29. Try connecting to it now db2 connect to backdb

db2 terminate

Lab 02 Database Creation and Exploration

Page 29

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. Exploring various DB2 database functionality features


Run the following exercises in a DB2 CLP Command Window: ___30. To learn about administrative views and functions: Review and run script: Database03.CMD Review file: Database03_OUTPUT.TXT

___31. To learn about db2mtrk Memory Tracking: Review and run script: Database04.CMD Review files: Database04_OUTPUT.TXT & Database_DB2MTRK_Command.txt

___32. To learn about Event Monitoring: Review and run script: Database05.CMD Review file: Database05_OUTPUT.TXT & Database_DB2EVMON_Commant.txt

Page 30

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 02 Database Creation and Exploration

IBM Software

E. Altering a Table
DB2 allows you to alter a table in many different ways. Here is an example of how DB2 can alter a table: ___33. Review scripts Database15.CMD which executes Database16.DDL. Notice what this last script is doing: 1) renaming a column, 2) changing a column data type from SMALLINT to CHAR, 3) dropping a column:

Run script Database15.CMD which executes Database16.DDL Review file: Database15_OUTPUT.TXT

DB2 ALTER TABLE actually has much more power than this. For example, you can alter a column from CHAR to BIGINT for example, provided your data can be cast that way. So, if your CHAR column had 1,2,3 data in it, DB2 would scan your column to make sure your CHAR data can be cast into and BIGINT column, and if it can do that, it will! DB2 can alter a table to change any column data type to any other, provided the data can be cast that way. For an extra exercise, try these commands: db2 describe table alter_table_tb db2 alter table alter_table_tb alter column dept set data type bigint db2 describe table alter_table_tb

*** End of Database Creation and Exploration Lab by Burt Vialpando ***

Lab 02 Database Creation and Exploration

Page 31

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03. Data Studio


A. Launching Data Studio
___1. Open the IBM Data Studio by clicking on this icon on your desktop or in your task bar:

ORYou can open Data Studio by choosing: Start All Programs IBM Data Studio Data Studio 3.1.1 Full client ___2. At the Workspace Launcher window, make sure your workspace is pointed to the following folder: C:\POT_DB2\03DATASTUDIO\WORKSPACE

___3. ___4.

Check the box: Use this as the default and do not ask again After the perspective opens, close the Task Launcher view

___5.

The top right portion of your screen shows that you are now in the Database Administration perspective.

Page 32

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

B. Connecting to and managing a database


Now we will connect to the GSDB database using the Administration Explorer view. You will find this view in the top left corner of your Database Administration perspective. (A perspective is made up of a number of views.) ___6. Expand the view Administration Explorer a little more by clicking on its edge and dragging it over. Then expand the view to see all the databases.

___7. ___8.

Right click on the connection property GSDB Connect


You are now in a Properties for GSDB screen. Fill in the following (case sensitive!) User name: dbapot Password: dbapot123 Save password: [check]

___9.

Try [Test Connection]. You should get: Ping succeeded!

Lab 03 Data Studio

Page 33

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___10. In this same Properties for GSDB screen, click on the Common setting ___11. Check box: Connect every time the workbench is started [OK]

___12. You are now connected to the GSDB database. After you leave Data Studio, upon return, you will automatically reconnect. Notice you see GSDB database in the Administration Explorer below the instance DB2.

Instance management
___13. Maximize your Data Studio screen in order to best take advantage of the entire Database Administration perspective. ___14. Right click on the instance DB2. Notice the functionality that is available to you for the instance that manages GSDB. We wont be using any of these yet so dont choose any of them. (Note: Configure means changing the DBM cfg parameters; we will explore this in a later exercise.)

Page 34

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

Database management
___15. Right click on the GSDB database connection to see what database management activities you can perform with the UI. ___16. Choose: Set Up and Configure Configure. This will allow you to change database (DB) configuration parameters.

___17. An Editor view opens up in the middle pane of your perspective. Double click on the view name to expand it to full screen. Dont change any parameters just now, but notice how you could if you wanted to. Close this screen when done by clicking on the X in the view tab.

___18. Right click on GSDB and choose: Manage Database List or Force Applications Just browse through the applications screen to see how you can force (kill) an application with it. Do not force any applications! Close this screen when done. ___19. Now review: Manage Database Manage Storage to be able to add automatic storage paths to your database. ___20. Expand GSDB database and notice you can click on database level objects like buffer pools or table space to manage these as well. Expand table space for example and right click on one to see what you can do.

___21. Close all Editor views (top middle screens in your Database Admin perspective) when done.

Lab 03 Data Studio

Page 35

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Editing and Running SQL


___22. Open a new perspective called the Data perspective. To do this, click on the following: Window Open Perspective Data

___23. Expand the Data Source Explorer GSDB database connection as shown below. Notice the first GSDB entry is the connection property, the second is the database itself

___24. Right click on the GSDB database and choose: New SQL Script

___25. Choose the appropriate instance and database connection that this script will run in

The Editor view will be open with a blank SQL editor session.

Page 36

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___26. In the Script1.sql editor view, copy and paste the two SQL statements shown below found in the file : C:\POT_DB2\03DATASTUDIO\DataStudio01.SQL.

(Alternately, you can drag and drop the script from the Windows Explorer into the editor view or you can use a technique: File Open File...) ___27. The first SQL statement will use a proprietary Oracle syntax (reference to DUAL) and the second a proprietary DB2 syntax (reference to FETCH) select sysdate from dual; select * from gosales.product fetch first 5 rows only; ___28. Highlight both statements, right click, then choose: Format SQL

___29. Highlight the first SQL statement and hit [F5] SQL statement and then choose: Run SQL)

(You can also right click on the highlighted

___30. Find the output in the bottom right pane of your Data perspective in the SQL Results view. (You may have to expand it a bit to see the results better.)

___31. Highlight the second SQL statement and do the same.

Notice that Data studio will use whatever Oracle compatibility features you have enabled for your database. (We enabled this earlier.)

Lab 03 Data Studio

Page 37

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___32. Look at the SQL Results section closely. Notice you can now click on either query you have run before. Both result sets are available to you.

You can right click in the SQL Results pane and remove or save any of these results. ___33. Highlight the first query, then right click on it, then choose: Toggle Comment (or [Ctrl] /)

___34. Position your cursor after the asterisk in the second query. Use [Ctrl][Space] to get context assist for this SQL. Notice here it gives the column names and data types from the table referenced. This helps you build or edit the query without having to go to another window to look these things up. You can double click on any column name to put it into the query. Try it.

. ___35. Remove the keyword ONLY from the second query. Notice you get a syntax warning with the red X mark on the line and a red underline near the syntax problem. (Note: you can turn these off if you don't want to see them.)

___36. Put the keyword "ONLY" back into the SQL statement to fix the syntax error.

Page 38

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___37. Right click in the editor window anywhere, choose: Validate Database Object References

___38. Now change your query to reference a fake column name (like XXX) that does not exist in your table in the query (as shown below) and notice you get an error warning.

___39. Change the editor settings back to how it was in the beginning of this Editor lesson ___40. Close this Editor view and dont save the script.

Lab 03 Data Studio

Page 39

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Exploring Tables
Overview diagrams
___41. Expand the top and side of your Data Source Explorer to see more of it ___42. Expand the objects in this view by clicking on the + signs by: 1) GSDB database, 2) Schemas & 3) GOSALES under schema.

___43. Notice each database object type is represented for you to manipulate with the UI. Data Studio is powerful as you can right click on any object or any level of object and it will show you what things you can do. ___44. Expand the Tables under schema GOSALES. ___45. Select table PRODUCT Hold [SHIFT] Click PRODUCT_TYPE Right Click Add to Overview Diagram

Page 40

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___46. At the Overview Diagram Selection screen, check the Infer implicit relationships box. This is a powerful Data Studio capability that can go beyond defined foreign keys on tables. It will match up column names and data types to make relationships in the diagram. Click: [OK]

___47. Data Studio will take a couple of seconds to calculate the inferred relationships and return a diagram of these tables in the Editor view. ___48. Double click on the diagram views tab. This will maximize this view in your Data perspective.

___49. Review the diagram. Move the tables and lines around to get a sense for how this can be redrawn if you dont like the default diagram.

Lab 03 Data Studio

Page 41

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___50. Double click the diagram view tab again to shrink it. Select the Properties view and notice you can manipulate the diagram using this view if you want to by selecting anything on the diagram.

This is just a basic functionality of Data Studio. If you want the full power of a data modeling tool, please ask about IBM InfoSphere Optim Data Architect which integrates fully with other Eclipse based tools. ___51. Close the diagram view when you are done with this review.

Page 42

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

Editing and viewing table data


___52. Find table PRODUCT_LINE. Right click on it then Data Edit

___53. In the editing screen find the last row <new row>. Right click on it Insert Row ___54. Add values 2104 and Burts Beeswax to the row. Update another row by changing a value in it like shown below. Click Save or [CTRL]+S

___55. Notice the SQL Results view message that reflects what you just did.

___56. Close the Edit view. Save your work as you exit. ___57. Right click on the table PRODUCT_LINE again. This time try: Data Sample Contents ___58. Look in the SQL Results view, your changes should be there.

Lab 03 Data Studio

Page 43

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Generating DDL
___59. Select all tables in schema: GOSALES (hint: hold [Shift]). ___60. Right click then choose: Generate DDL

___61. Choose all the defaults [Next] then [Next]. ___62. In the Save and Run DDL screen, check the box: Edit and run DDL in the SQL Editor.

___63. Then [Next] then [Finish].

Notice you have generated DDL for all these tables.

___64. Close the script Editor view when done reviewing the DDL.

You have a great amount of flexibility in generating DDL like creating DROP statements, fully qualifying object names, including DDL for related objects, etc., but we just chose defaults for this example to keep it simple.

Page 44

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

Table maintenance exercices: runstats, reorg


___65. Right click on any table (PRODUCT table shown below) and choose: Update Statistics. Review the SQL Results to see how it did the runstats.

___66. Change to the Database Administrator perspective

___67. Find table PRODUCT under the tables view in the Administration Explorer. Right click and choose: Manage Reorg Table

___68. In the Reorg editor choose the options shown below then: Preview Command.

Close this window when done.

Lab 03 Data Studio

Page 45

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. Extra Exercise: Debug a stored procedure


Creating a project To debug a stored procedure, it is best to first create a project to put our work into. This allows us to create, alter, and save our scripts in a folder under our workspace. ___69. Return to the Data perspective.

___70. At the top of your Data Studio menu choose: File New Data Development Project

___71. Give the project a name: MyFirstProject. Click [Next].

___72. Make sure you associate the GSDB database connection property with this project. Click [Next].

___73. Change the Default schema to be GOSALESCT which happens to be where the stored procedure is we want to debug.

___74. Click [Finish].

Your project will be created with the properties you just chose.

Page 46

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___75. In the top left view in your Data perspective, find and expand your project MyFirstProject in the Data Project Explorer. Notice the types of projects we can do in this view. We will be working on a Stored Procedures project in this view.

___76. Now go to the Data Source Explorer, and find and expand Schemas to find schema GOSALESCT. Find Stored Procedures. Click on GET_CUSTOMER_NAME then drag and drop it into the Stored Procedures section of the project MyFirstProject as shown below.

Lab 03 Data Studio

Page 47

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___77. Choose the stored procedure code for GOSALES.GET_CUSTOMER_NAME, right click on it then: Open

___78. Notice the Editor view has this code loaded and ready for you to start working with.

Debugging the stored procedure ___79. Right click on the stored procedure, then choose: Deploy

___80. Keep defaults, then: [Next]

Page 48

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___81. On the Routine Options screen, make sure to click: Enable debugging Then: [Finish]

___82. The routine should be deployed to the database like this:

___83. Now right click on the stored procedure, then choose: Debug

___84. In the Specify Parameter Values screen, set CUSTOMERID to 101. Then: [Debug]

Lab 03 Data Studio

Page 49

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___85. Data Studio will now warn you that it is about to change perspectives on you. Check the Remember my decision box and then: [Yes]

___86. Look in the upper right hand corner of the screen and notice you now have both a Debug and a Data perspective opened. You can switch between them now if you want to. Stay in the Debug perspective for now.

___87. In this Debug Perspective, set breakpoints anywhere in your code by double clicking in the shaded area next to the code. Set breakpoints on all the SELECT statements as shown below.

Page 50

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

___88. Now either press [F5] to go from line to line or use the icon shown below to Step Into the code. Learn to use [F6] and [F7] for Step Return and Step Over.

___89. As you step through this simple stored procedure, notice the output parameters (variables) change. The code we are using here is purposefully simple to demonstrate how strategically placed breakpoints will help you find what your code is doing and what it is returning.

___90. If you run all the way through the code and the session is terminated, then just right click on the terminated session itself and choose: Relaunch. This will let you start your debugging all over again.

Lab 03 Data Studio

Page 51

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___91. Close the Debug perspective when you are done by right clicking on it and then: Close.

___92. Back in the Data perspective, close all the editor views.

Page 52

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 03 Data Studio

IBM Software

F. Extra Exercise: Stored procedure performance information (profiling)


You can use Data Studio to "profile" a stored procedure, which means to run it and gather performance information on it, line by line, in order to find out which lines of code are executed, how many times and for how long. Profiling can tell you total sort times, rows read and many other things. ___93. In your Data Project Explorer, find the stored procedure GET_CUSTOMER_NAME again. Right click on it then choose: Run

___94. In the Run screen, click on the Run and Performance Options tab

___95. Check the Performance data box that says: Gather performance information from the database Then: [Run]

Lab 03 Data Studio

Page 53

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___96. In the SQL Results view, find the Profiling Data tab. Review all the performance information and imagine a large complex procedure with lots of loops and branches. This information can tell you where the code is going and what it is doing line by line. Right click on any line of code to switch to the InfoSphere Optim Query Workload Tuner to begin a tuning session. (Note: this product has not been installed for this lab and is outside the scope of this topic. Ask about our other PoTs on various IBM tooling for more details.)

___97. In the SQL Results view, right click and choose: Remove All

___98. Close your editors and exit from the Data Studio by using the tool bar: File Exit

*** End of Data Studio Lab by Burt Vialpando ***

Page 54

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

Lab 04. CLPPlus and Oracle Compatibility


A. Logging on to CLPPlus
We will learn how to use CLPPlus in the interactive and batch modes here. To get started: ___1. ___2. Close your DB2 CLP Command Window. Open a new Windows Command Prompt instead.

___3.

Change directory to C:\POT_DB2\04CLPPLUS

CD \POT_DB2\04CLPPLUS

CLPPlus does not require a DB2 CLP Command Window to execute in. We will show that in the next few exercises by only using a Windows Command Prompt. ___4. ___5. For our CLPPlus session, we will be using the following parameters: User: Password: Host: Port: Database: dbapot dbapot123 localhost 50000 sample

There are several ways to start up CLPPlus. The first method we will use is to connect directly to the database upon start up. In the terminal window, type in the following command:

CLPPLUS dbapot/dbapot123@localhost:50000/sample

Lab 04 CLPPlus and Oracle Compatibility

Page 55

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___6.

You are now in an interactive CLPPlus window with a default prompt: SQL>

We could have started CLPPlus without a new window staying in the Windows Command Prompt window instead. To do this use the -nw option, as shown below. For these exercises, we'll just use the default window.

Page 56

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

B. Interactive CLPPlus commands


___7. Use the DESCRIBE command to look at the details of the table EMPLOYEE. The columns and their details are listed.

DESCRIBE employee

CLPPlus commands are not case sensitive. In this lab, CLPPlus commands are capitalized in order to distinguish the command from the keywords. ___8. Use the HOST command to run any operating system command. For example, try this:

HOST dir
___9. Using the Windows Explorer, review script file: CLPPlus01.SQL. ___10. To execute this script in CLPPlus, use the command:

START CLPPlus01.SQL
___11. Any DB2 commands in the script will be loaded to the SQL buffer and executed.

Lab 04 CLPPlus and Oracle Compatibility

Page 57

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

The GET command will load the script into the SQL buffer without executing it.

___12. Use the LIST command to display the SQL buffer:

LIST

Use CLPPlus SQL buffer line commands to manipulate a query ___13. We would like to sort the entries by salary in ascending order instead of descending. To do this, we can use the CHANGE command that will do a pattern matching and replace the old token with the new one. Be aware that the pattern string you provide is case-sensitive.

CHANGE /salary DESC/salary ASC


___14. We want to double check that the SQL commands in the buffer have been changed by typing in the command LIST. We can see that line 3 has been changed successfully to ORDER BY job, salary ASC. Note that CHANGE only alters the SQL buffer. The original script file which we loaded remains unchanged.

LIST

___15. Use the RUN command to execute the query contained in the SQL Buffer. You can also use / as an alias to the RUN command.

RUN

Page 58

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

Using the editor ___16. Use the EDIT command to invoke the default editor to make changes to the SQL buffer.

EDIT
___17. In the editor, change the NOT LIKE value from E% to A%

___18. Save to the buffer with: File Save ___19. Exit the editing session with : Exit ___20. Your buffer has changed and lists the code again for you automatically. Use the / key to run this new change (or use the RUN command)

Lab 04 CLPPlus and Oracle Compatibility

Page 59

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Writing results to a file (SPOOL) ___21. Use the SPOOL command to save the results of our SQL output to a file in order to make a report. SPOOL requires an output file name and optionally a complete path. Type in the command:

SPOOL employee_report.txt
___22. Now execute the following commands to create your report file:

RUN SPOOL OFF


___23. In the Windows Explorer, find your report file and open it:

___24. Use the SHOW command to check the value of a particular CLPPlus setting. Try this to see if your spool is on or not:

SHOW spool
___25. Try this to see all of your command control settings:

SHOW

Page 60

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

___26. Notice your timing command control setting is off. To see timing of your executed SQL try this:

SET timing on RUN

Lab 04 CLPPlus and Oracle Compatibility

Page 61

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Save and clear the SQL buffer content ___27. Use the SAVE command to save the contents of the SQL buffer to a file:

SAVE employee_changed_report.sql
___28. Now find and review this file using the Windows Explorer

With SAVE, you could hard code a complete path where you want the saved file to go, otherwise CLPPlus will put the file in the path where you invoked SAVE from.

___29. Use the CLEAR command to empty your SQL buffer in order to start fresh.

CLEAR BUFFER LIST


___30. Use the QUIT command to exit CLPPlus.

QUIT

Page 62

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

C. Running a script file in batch mode


Starting out using CLPPlus batch mode ___31. Using the Windows Explorer, find the command file CLPPlus02.CMD in the directory 03CLPPLUS. Open it. (Right click Edit)

___32. Notice in this script we invoke the CLPPlus with a SQL script file all in one command line:

___33. Using the Windows Explorer, find the SQL script file CLPPlus03.SQL. Open it and review it. Take time to review the control setting commands like SET or COLUMN. ___34. Run the command file to see how it creates a report automatically for you using all of these techniques weve seen so far:

CLPPlus02.CMD
___35. Notice the output report is spooled to a file CLPPlus02_OUTPUT_REPORT.TXT. Use the Windows Explorer to open this report to see the formatting.

Lab 04 CLPPlus and Oracle Compatibility

Page 63

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Using substitution variables ___36. Lets explore using substitution variables in a script. Use the Windows Explorer to open command file CLPPlus04.SQL. ___37. Notice this script has two ACCEPT commands. The first is for the OWNER_NAME_STRING variable which is accepted, defined and used later in the query.

___38. Run this SQL script the way shown below. (This is typed all in one line and there is a space after the database name and before the @SQL script file name.)

CLPPLUS dbapot/dbapot123@localhost:50000/sample @CLPPlus04.SQL


___39. When prompted for the Schema Owner String, answer the value pot When prompted for the Table String, answer the value emp Do not use quotes in your answers

These answers are strings that our query uses to find any owner with pot in the name and any table with emp in its name because the query uses a LIKE syntax in the WHERE clause.

Page 64

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

___40. Review output report CLPPlus04_OUTPUT_REPORT.TXT. Notice our query has used the substitution variables to customize the report. (Owner only has pot in the name and the table_name only has ones with emp in the name. Try running this script again, but this time use different values for the variables, or, just hit [ENTER] when prompted for the variable and let it default and see what happens. (Hint: use the up arrow key to bring back your old command at the Windows Command Prompt.)

Substitution variables can be used in your SQL in any way you can dream up in any part of the SQL statement, not just in the WHERE clause. These make a single query more flexible. ___41. Run this SQL script again, but use the way shown below to see that you can provide substitution variables with the execution of the script (without having to do it interactively.) Use the variables dba and mdc as shown below:

CLPPLUS dbapot/dbapot123@localhost:50000/sample @CLPPlus04.SQL dba mdc


___42. Review output report CLPPlus04_OUTPUT_REPORT.TXT again.

Lab 04 CLPPlus and Oracle Compatibility

Page 65

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. Oracle Compatibility
Now lets see some more native Oracle functionality using the CLPPlus. This functionality works in the DB2 CLP Command Window too by the way, or in any programming interface for that matter. We will be mixing CLP with CLPPlus command examples in this lab exercise, so do the following: Checking your Oracle compatibility environment ___43. First, lets look at our registry variables to see if we are in an Oracle compatible mode. Type the following:

db2set -all

Notice the registry variable DB2_COMPATIBILITY_VECTOR. Setting this to ORA turns on all Oracle compatibility features. (It is possible to set this to choose only some compatibility features, but we have set it for all Oracle related features.) To activate this registry variable for your instance, you would start and stop the instance with two commands: DB2STOP then DB2START. (We did these things already in the first command script in this lab.) Once this registry variable is set for your instance, you would next create your database. Creating a database with the compatibility vector set will enable it to use Oracle native data types and also creates an Oracle data dictionary compatible set of views in the DB2 system catalog. (We have already done that for the SAMPLE database in our setup script for this PoT.) ___44. To see the Oracle dictionary views in the DB2 database, review and run script CLPPlus05.CMD:

CLPPlus05.CMD
___45. Review CLPPLUS05_OUTPUT_REPORT.TXT. Seeing these views and the compatibility vector as above tells us we are ready to use all Oracle native PL/SQL syntax.

Page 66

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

Using Oracle proprietary syntax in a DB2 database with CLPPlus ___46. Review and run script CLPPlus05.CMD and CLPPlus06.SQL to see SQL using Oracle proprietary syntax used in a DB2 database. Notice now you will be able to run these kinds of commands in DB2 because you are in a compatibility mode. You can run these in the CLP, in CLPPlus, in a Java program, in any way that you may want.

CLPPlus06.CMD
___47. Review the output file CLPPlus05_OUTPUT_REPORT.TXT to see how DB2 can use: DECODE, SYSDATE, DUAL, ROWNUM and NVL. Using Oracle proprietary syntax in a DB2 database with the CLP ___48. Close your Windows Command Prompt window ___49. Open a DB2 CLP Command Window and change to directory C:\POT_DB2\04CLPPLUS

___50. We can run Oracle syntax in the DB2 CLP Command Window too. However, the CLP does not by default recognize the / as a delimiter in scripts the way CLPPlus does. So, in order to get the CLP to use this / delimiter we use a SET SQLCOMPAT command. ___51. Review script CLPPlus08.CMD

___52. Review the script it calls: CLPPlus09.SQL. Notice this is just regular Oracle PL/SQL syntax that creates a procedure.

Lab 04 CLPPlus and Oracle Compatibility

Page 67

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___53. Run CLPPlus08.CMD to see how this works:

CLPPlus08.CMD
___54. Notice that the creation of the procedure is successful and the call to that procedure works. Review file CLPPlus08_OUTPUT_REPORT.TXT for details.

Automatic Revalidation ___55. There is a database parameter called AUTO_REVAL. This parameter controls how revalidation of objects is enforced in DB2. The most powerful option of this parameter is called deferred_force. Lets see how this works: ___56. From the DB2 CLP Command Window, type the following: db2 connect to sample db2 update db cfg using auto_reval deferred_force ___57. To make sure this has taken affect, type this next (this is case sensitive): db2 get db cfg | findstr AUTO_REVAL ___58. You should see this if your commands worked properly:

Page 68

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

___59. Review script CLPPlus11.DDL (which is called by CLPPlus10.CMD):

First, notice DB2 will perform the CREATE OR REPLACE functionality on the view. This capability is very important for online schema change. It means that we can avoid using DROP in order to recreate a view which preserves grants to the view. Second, notice also that the view references a function not yet created, and then the function references a table not yet created. Wont this DDL fail if it is attempted to be run in this order? Normally, yes, but with AUTO_REVAL to deferred_force set as we did earlier, it will work just fine. We will see this in action soon. Third, the table is created and data inserted into it. Finally, the view is used in a SELECT. Results are displayed because the view and function will automatically revalidate upon reference. Since everything is there for them to validate, the view works just fine!

___60. Run script CLPPlus10.CMD. ___61. Notice that the view and function are created, but are marked as invalid and an error code is produced

Lab 04 CLPPlus and Oracle Compatibility

Page 69

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___62. Notice though that at the end of the script, the SELECT from the view works just fine! This is because the view and functions are automatically revalidated upon access.

DB2 has another online schema change ability called soft invalidation. This allows for you to drop or alter an object, even while transactions are using that object. For example, you could have a long running query going against a view. DB2 allows you to alter or drop the view without killing the transaction. The running transaction simply uses the copy of the view until it completes. This is the default setting for DB2. If you want to turn this off, do the following registry variable change: db2set DB2_DDL_SOFT_INVAL=OFF

Page 70

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

Migrating a schema from Oracle to DB2 ___63. We have taken some DDL from an Oracle database, and separated it by object type as shown below. Lets see if we can create these objects in DB2. ___64. Using your Windows Explorer, review this source DDL in the 04CLPPLUS directory. Pay special attention to the PL/SQL types of objects like packages, procedures, functions and triggers. Notice all of these files use the proprietary Oracle syntax. This would represent all the source code from your source Oracle database that you wish to migrate to DB2.

___65. Review script CLPPlus20.CMD. Note the following things about the script:

It connects as user db2user1

(so, the target objects will end up in that schema) (so, we can create objects in any order) (so, the source DDL can have / delimiters)

It uses auto_reval deferred_force It uses SQLCOMPAT PLSQL

Lab 04 CLPPlus and Oracle Compatibility

Page 71

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___66. It then calls each .sql file using the DB2 CLP Batch Command Processor

___67. Run script CLPPlus20.CMD to create all these objects in DB2

CLPPlus20.CMD
___68. You can review file CLPPlus20_OUTPUT.RPT to see the details on this. (Please make sure you use WordPad and not Notepad to review this file. WordPad formats this for better viewing.) ___69. Run script CLPPlus21.CMD to see a report of all the objects in the DB2 database under schema db2user1. Review CLPPlus21_OUTPUT.TXT for that report.

CLPPlus21.CMD

Page 72

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

Currently Committed ___70. We can easily see how the Currently Committed behavior works in DB2. ___71. Review and run CLPPlus30.CMD to set up a session that is updating a table

CLPPlus30.CMD
___72. This script does the following:

CONNECT as user dbapot Display CUR_COMMIT database configuration parameter setting SELECT all records from table INVENTORY Begins an UPDATE of the first record in the INVENTORY table, but does not commit it

___73. Notice how the table looks before the update:

___74. Again, please note: user dbapot is in the middle of a non-committed update to this table. ___75. Now, this is important: OPEN A SECOND DB2 CLP Command Window ___76. In this second CLP window, simulate another DB2 application by typing:

db2 connect to sample user db2user1 using db2user1

Lab 04 CLPPlus and Oracle Compatibility

Page 73

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___77. Now, select from the INVENTORY table like this:

db2 select * from dbapot.inventory


___78. User db2user1 is using Currently Committed, so the writer does not block the reader. The SQL returns what is currently committed for that first record without a wait or a lock of any kind.

___79. If you want to see what the data looks like that hasnt been committed, you can use a dirty read of the data in memory using isolation level UR (Uncommitted Read). Try this:

db2 select * from dbapot.inventory with ur


___80. Notice DB2 reads the dirty data from memory, again, without a lock and without a wait.

Page 74

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 04 CLPPlus and Oracle Compatibility

IBM Software

___81. If you want to use the classic behavior of isolation level CS that does not use Currently Committed, try this:

db2 select * from dbapot.inventory wait for outcome

___82. Notice the screen stays in a locked mode. The query is waiting for the other transaction to commit or rollback ___83. Return to the FIRST CLP window to commit the update to the table, type:

db2 commit
___84. Notice now that both transactions complete.

___85. Close one of the CLP windows.

Lab 04 CLPPlus and Oracle Compatibility

Page 75

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Extra Exercise: Anonymous Block and DBMS package use ___86. To see an example of anonymous block PL/SQL in DB2 as well as support of DBMS_OUTPUT: ___87. In a Windows Command window, logon into the SAMPLE database using CLPPlus:

CLPPLUS dbapot/dbapot123@localhost:50000/sample @CLPPlus35.sql

___88. Quit from CLPPlus

*** End of CLPPlus and Oracle Compatibility lab by Burt Vialpando ***

Page 76

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

Lab 05. Security


A. Instance Level Security
This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database. We will first check the instance configuration parameters using Data Studio. ___1. Launch Data Studio make sure you are open to the Database Administrator perspective.

___2.

In the Administration Explorer, connect to the SAMPLE database. (Hint: right click on the database).

Use dbapot, dbapot123 as the User name and Password to connect with as shown below

___3.

Right click on the instance DB2, then choose: Configure

Lab 05 Security

Page 77

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___4.

Choose SAMPLE as the Connection Profile.

___5.

Review the Administration section of DBM cfg parameters in the UI

This section of DBM cfg parameters controls security at the instance level A section of DBM cfg parameters is a UI concept only as this arrangement is to help you find similar parameters within the Data Studio UI interface.

___6.

Click on parameter: AUTHENTICATION. Then click in the Pending Value box to find a drop down option.

You can now set the DBM cfg value in the UI

Hover on the question mark symbol

to see help on this parameter

___7.

Review parameters: TRUST_ALLCLNTS and TRUST_CLNTAUTH

Page 78

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

These work with client authentication only.

___8.

Review parameter: SYSADM_GROUP

This authority grant is very important on UNIX systems as it is the highest authority level you can have

Notice the other instance level authorities that are granted through a DBM cfg parameter to a group: SYSADM_GROUP, SYSCTRL_GROUP, SYSMAINT_GROUP, and SYSMON_GROUP. These parameters are all set to null on Windows systems due to the way Windows handles administrator capabilities: a user is either an administrator or not. But these are important on the UNIX systems to separate the duties of different users. Review parameters: SVRCON_PW_PLUGIN and CLNT_PW_PLUGIN

___9.

Remember, a plug-in is a DB2 method of controlling database authentication. The default plug-in that uses the OS to authenticate comes with DB2, all others are optional purchases from vendors or they can be custom built by your IT shop. You can pretend to put a plug-in name here called myplugin to see how to set this. Do not save this parameter setting however.

___10. Close the Configure Parameters editor without executing any changes.

Lab 05 Security

Page 79

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

B. Database Level Security


We will be connecting from the CLP to do these next security exercises. Please keep your Data Studio session open. We will be returning to it later. When you connect to a DB2 database on a remote server, you need to specify your user credentials. In the example below, SAMPLE database is on the same machine you are connected at, so the connect string can be more simple. ___11. From a DB2 CLP Command Window, position yourself in the directory C:\POT_DB2\05SECURITY

___12. Type the following one at a time in the DB2 CLP Command Window: db2 terminate db2 connect to sample db2 connect to sample user dbapot using dbapot123 db2 get connection state db2 connect to sample in exclusive mode ___13. NOTE: If you get an error here, go back to the Data Studio tool and right click on SAMPLE then choose: disconnect. You cannot get exclusive lock on the database while it is in use and the Data Studio connection you had kept it in use. ___14. Now, try this again. db2 connect to sample in exclusive mode db2 get connection state (You should now show exclusive connection mode.) db2 terminate Exclusive mode connections lock out everybody else. If you were to open another DB2 CLP Command Window and try to connect as a different user (like user db2user1 password db2user1) you would not connect and receive a message: The database is currently in use.

Page 80

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

___15. Return to Data Studio and connect to SAMPLE again. ___16. Right click the database SAMPLE Manage Privileges

___17. Review the Properties section of this perspective. Notice that you can use the icons shown below to grant to new or existing users, groups or roles any database level authority. We will do this on the next step.

___18. Click on the yellow diamond to make database level grants to a new user.

___19. Fill in the Grantee as GUEST. Check off CONNECT and CREATETAB grant boxes, then: [OK]

Lab 05 Security

Page 81

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

You did NOT just create a user GUEST. You simply are performing a grant to a user called GUEST so that, if properly authenticated, that user is allowed to connect to the SAMPLE database and then create a table in it. You dont care at this point how GUEST authenticates, you have just made sure that if they do authenticate, they will have these database authorities. ___20. Try this again, but this time use Group and name the new group NEWGROUP.

DB2 does not have concept of creating nor maintaining users as does some other databases like SQL Server. Only the grants to users, groups and roles are maintained in DB2. We will show how this looks in the system catalog later in this lab. ___21. You will now harden these changes to the database by performing the following: ___22. Make sure to click on the database SAMPLE in the Administration Explorer

___23. Find the Review and Deploy Changes icon in the top right corner of the editor view which looks like the one below. Hover on it first to make sure it is the correct one, then click on it.

Page 82

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

___24. The very first time this is done in a Data Studio session, it can take a few seconds to complete, so please be patient. ___25. Review the changes then click [Run].

___26. The SQL Results view will tell you if you did this properly.

Lab 05 Security

Page 83

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Object Level Security


___27. In Data Studio click on the Tables for the SAMPLE database. Scroll to find table DBAPOT.ACT.

___28. Right click on the table ACT, then Manage Privileges

___29. Notice the Properties view for the table becomes available. Click on all the privileges for the user DB2COBRA so it looks like the image below:

___30. Notice that an icon symbolizing a delta change now shows next to this table. That means this object is being altered in some way in Data Studio.

___31. Find the icon on this editor view at the top right that symbolizes Review and deploy changes shown below. Hover on it first to see what it is, then click on it when you are sure you have found it.

Note: If you get a warning, you can inspect the [Details] to see that it is telling you a related view might need changes as well as the table. Go ahead with the changes anyway for now.

Page 84

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

___32. Review the changes. Notice that your earlier double clicking of a grant box adds the WITH GRANT OPTION to the grant. Click [Run].

___33. Security for most other database objects works similarly, take a look at a couple of different object types as the exact kind of privileges vary from object type to object type, for example: Table spaces have only one privilege: USE. Indexes only have one: CONTROL

Other objects, like triggers and buffer pools, dont have any specific privileges associated with them directly at all. Review these kind of objects too.

___34. Close any editors you may have open. Don't worry about saving changes.

Lab 05 Security

Page 85

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. Using the System Catalog Security Views


Determine what privileges and authorities we have in the database by reviewing the system catalog: ___35. In the Data Studio Administration Explorer, right click on Views Database Catalog Filter

___36. In the Conditions screen fill in the Value as %AUTH

___37. Notice you are now only showing views with names that have AUTH in them

___38. Right click on the SYSCAT.DBAUTH view Browse Data

Page 86

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

___39. You will see all the database authority grants in this database. Notice the last entries were the ones we made earlier in this lab to user NEWGROUP and group GUEST.

___40. Close this view. ___41. Find view SYSCAT.TABAUTH. Right click on it then Browse Data

___42. Find and click on the filter icon on the top right of this editor (browse) session

___43. First click the [Add All] button to select all columns to display in the filter.

___44. Next, in the Row Selection Condition section, fill in the column, operator and value as shown below with TABNAME, =, ACT. ACT doesnt have to be entered with quotes, Data Studio will add the quotes. This will filter all data in the browse session to only see grants on table ACT.

Lab 05 Security

Page 87

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___45. Click [OK] to see the results of this filter for the data in this view. You will notice we can see the grants we made earlier in this lab for table ACT for user DB2COBRA.

___46. Right click on the filtered Views in the Administration Explorer again Database Catalog Filter

___47. Blank out the filter and click [Finish]

___48. The views should no longer have a filter on them.

Page 88

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

E. User and Group Overview


Use the Data Studio special view for users and groups ___49. In the Data Studio Administration Explorer, expand Users and Groups. Click on Users.

___50. Find user GUEST and right click on it Manage Privileges

___51. Notice in the Properties view you can manage all database and object privileges for this single user here in one editor session!

___52. Review group NEWGROUP the same way. So, you can see that these Data Studio views allow you to change all the privileges for a user, group or role in one editor session.

Lab 05 Security

Page 89

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

F. Schemas
This exercise will show you how schemas work in DB2 in your CLP Command Window, change directory to C:\POT_DB2\05SECURITY ___53. Review & run script Security01.CMD. Review output file Security01_OUTPUT.TXT. ___54. The script sets up the example by creating a schema called TSCHEM. Then it creates a table fully qualified with the schema name. It is called TSCHEM.TSCHEM_TABLE

___55. The script next does a simple select from this table, but the first time it fully qualifies the table name with the schema:

___56. The second time it does a select from this table, but uses a SET SCHEMA method:

___57. Notice the results work fine this way too. ___58. Extra exercise: You can explore the schema and table we just created in Data Studio.

IMPLICITSCHEMA is a database authority that lets users create a schema on the fly if it doesnt exist. So, if you issued a CREATE TABLE schema1.table1 command and schema1 did not yet exist and the user had IMPLICITSCHEMA authority, then that schema would be created along with the table.

Page 90

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

G. Viewing database authorities


We will show you how to view your database authorities using one table function: SYSPROC.AUTH_LIST_AUTHORITIES_FOR_AUTHID ___59. Review and run: Security02.CMD. After running above script, review file: Security02_OUTPUT.TXT ___60. The output from this function shows both direct and indirect grants to an authority through various means. The first columns are for direct grants to a user, group or through a public grant The last columns are for role grants, first direct, then indirect Asterisks (*) mean this is not applicable. For example, you cannot grant instance level authorities like SYSADM through a user grant, only through a group assignment (please recall our first exercise in this lab...) Authorities without a "Y" somewhere means the user does not have this authority

Lab 05 Security

Page 91

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

H. Column and Row level security


Row Column Access Control (RCAC) is also called Fine Grained Security in the database industry. Here's how DB2 does it: ___61. Review and run: Security03.CMD. After running above script, review file: Security03_OUTPUT.TXT ___62. Notice that at first user DBAPOT can see all 42 records of the table EMPLOYEE, as well as the data in the SALARY column

___63. After Row and Column Access Control is implemented by the security administrator (user DB2SECADM) and rerunning the exact same query produces a limited result set for rows and columns:

Page 92

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

___64. Use Data Studio to Return All Rows from this table connected as user dbapot.

___65. Notice that any access method obeys these RCAC rules, even the Data Studio UI. They cannot be overridden by any access method. They can only be changed by the security administrator.

___66. Review the properties for this table to see how RCAC is enabled. (Note: we could have added RCAC to our table with Data Studio if we wanted to.)

Lab 05 Security

Page 93

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

I. Extra Exercise: DB2Audit and AUDIT POLICY


Please disconnect from the SAMPLE database in Data Studio before doing this exercise.

DB2 passive auditing is controlled in following 2 ways: SYSADM is in control of instance level security through the use of the db2audit tool. SECADM is in control of database level security through the use of AUDIT POLICY and AUDIT commands. ___67. A user with SYSADM grants SECADM authority to user secpot which creates 2 audit policies. Review and run script Security04.CMD. Explore output file Security04_OUTPUT.TXT; Notice that secpot with SECADM creates 2 audit policies: CHECK_POLICY to determine what DBAs are doing SENSITIVE_DATA_POLICY to determine who is accessing a given table.

___68. The audit facility controlled by SYSADM is demonstrated by the script Security05.CMD. Review and run scripts: Security05.CMD that executes Security07.SQL and Security08.SQL Explore output file Security05_OUTPUT.TXT. The last part of the output shows the output of SQL reports run against AUDIT tables. This shows that user dbapot using SYSADM: Did see instance level auditing information, in this case, connection audits Did not see database level audit results from SAMPLE database at all

Page 94

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 05 Security

IBM Software

Hint: You can also use Data Studio to browse table AUDIT.VALIDATE

___69. The script Security06.CMD shows the audit capabilities exercised by SECADM at the database level. Review and run scripts Security06.CMD that executes Security07.SQL and Security08.SQL Explore output file Security06_OUTPUT.TXT. The last part of the output shows the output of SQL reports run against AUDIT tables. This shows that user secpot using SECADM: Did see database level auditing information, in this case, who was creating or accessing tables Did not see instance level audit results from instance DB2 at all

Lab 05 Security

Page 95

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

J. Extra Exercise: A quick look into Label Based Access Control


This lab exercise gives you a quick look into the DB2 Label Based Access Control (LBAC). This exercise just shows a very small portion of the LBAC security capability by showing an example of a SALES table having restricted access. ___70. This is the scenario used in this exercise: User CEO: User WEST: User EAST: User CENTRAL: regions. full read access to the SALES table will only see sales data from WEST region. will only see sales data from EAST region will see sales data from CENTRAL_NORTH & CENTRAL_SOUTH

___71. Review and run exercise Security09.CMD. Review the output file Security09_OUTPUT.TXT. ___72. All users issue the identical query: SELECT SALES_DATE, SALES_PERSON, REGION, SALES FROM ACCOUNT.SALES (This query does not have a WHERE clause, so it runs against every row in the table) ___73. Notice that the user CEO sees all 4 rows of the table:

___74. Other users only see data of their region:

K. Cleanup
After you are done with the above exercises, you can run the following command to clean up the resources: Security50.BAT *** End of Security Lab - by Burt Vialpando and Vikram Khatri ***

Page 96

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

Lab 06. Autonomic Computing


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Self Tuning Memory Manager (STMM)


___1. ___2. Open a DB2 CLP Command Window Go to the directory C:\POT_DB2\06AUTONOM

___3.

Review and then execute Autonom01.CMD which executes Atonom02.DB2 and Autonom03.DB2. This will set up our lab. Notice that these scripts create a database AUTONMDB, a buffer pool, a table space and a table for our lab exercises. The script takes about 2 minutes to complete. Wait for the script to finish before going on to the next step. After the script is finished, restart Data Studio by choosing: File Restart

___4.

___5.

The restart should have automatically added a new database connection attribute for AUTONMDB in Data Studio. If it did not, then you can add a new connection manually by right clicking on the All Databases folder in the Administration Explorer view and choosing: New Connection to a database

Lab 06 Autonomic Computing

Page 97

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___6.

Then fill out the connection attributes as shown below. Test the connection before finishing. You will now be ready to work with your new database AUTONMDB in Data Studio.

___7.

Next run Autonom04.CMD which creates output files Autonom04_OUTPUT1.TXT and Autonom04_OUTPUT2.TXT. This file shows all the STMM related database configuration parameters for AUTONMDB:

___8.

___9.

This file shows the buffer pools that are STMM configurable for our database:

Page 98

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

___10. Now run Autonom05.CMD which executes Autonom06a.DML and Autonom06b.SQL multiple times. Youll see a window for each session of this running script simulating many users doing work against the STMM_TABLE table in our database. ___11. Let all the sessions run for a couple of minutes or so... After a few minutes, use your Windows Task Bar to find and kill all the CLP sessions except for your original DB2 CLP Command Window (the others have scripts running in them).

If you accidently close them all, that's OK, just open up a new CLP window for your next exercise and make sure to return to the appropriate directory. ___12. In your remaining CLP window, execute this script: Autonom07.CMD which creates output files Autonom07_OUTPUT1.TXT and Autonom07_OUTPUT2.TXT. ___13. Review and compare files Autonom04_OUTPUT1.TXT with Autonom07_OUTPUT1.TXT to see that STMM changed the DATABASE_MEMORY automatically:

Lab 06 Autonomic Computing

Page 99

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___14. Review and compare files Autonom04_OUTPUT2.TXT with Autonom07_OUTPUT2.TXT to see that STMM changed two buffer pool sizes automatically:

Page 100

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

B. Automatic Storage
Note when this lab was first set up, we created the database with the AUTOMATIC STORAGE YES ON C:\ parameter. This is all you need to do in order to get DB2 to handle automatic storage for you from now on in this database.

If you did not create the database with automatic storage, you can always add this later with: CREATE STORAGE GROUP [NAME] ON PATH [PATH] ___15. Review and execute the command Autonom08.CMD that executes Autonom09.DDL (which contains our ready-made table space DDL) and Autonom10.SQL (which uses administration views to show us information about all the table spaces in this database) ___16. Notice how the container paths look different for automatic and non-automatic storage table spaces. All automatic storage table spaces are under directory C:\DB2\AUTONMDB

Extra Exercise: Convert table space TEST_NONAUTO_STG to use automatic storage


___17. Perform the following commands in your DB2 CLP Command Window, in this sequence: db2 alter tablespace test_nonauto_stg managed by automatic storage db2 td; vf autonom10.SQL db2 alter tablespace test_nonauto_stg rebalance db2 td; vf autonom10.SQL

Lab 06 Autonomic Computing

Page 101

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Run Autoconfigure (Command Line for Configuration Advisor)


___18. Review and execute the command Autonom20.CMD to run autoconfigure command contained in Autonom21.DB2.

___19. Review the output in Autonom20_OUTPUT.TXT file ___20. You can modify the Autonom21.DB2 to change APPLY NONE parameter to APPLY DB AND DBM to apply changes in database manager and database configuration parameters.

Page 102

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

D. Extra Exercise: Automatic Maintenance


Automatic Maintenance RUNSTATS Exercise Setup
In order to test out the DB2 auto maintenance utility, we will build 4 quarterly tables so we can use DB2 autonomics to do RUNSTATS for us on these tables: ___21. Review and run script Autonom30.CMD which executes Autonom31.DDL and Autonom32.DDL Review output file: Autonom30_OUTPUT.TXT In Data Studio find database AUTONMDB (Use the Data Source Explorer) In Data Studio find the tables AUTOMAINT%

___22. To see what statistics look like for these tables, review and run these scripts: Autonom33.CMD which executes Autonom34.SQL Review output file: Autonom33_OUTPUT.RPT (HINT: Use WordPad for best formatting of this output)

Lab 06 Autonomic Computing

Page 103

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___23. Run script Autonom35.CMD which executes Autonom36.DDL. This script will reset the statistics for the table cardinality and NPAGES. ___24. Autonom33.CMD which executes Autonom34.SQL (yes, run this again) Now review this file again: Autonom33_OUTPUT.RPT (Use WordPad)

So, now you know what these tables look like with and without statistics We will set up the automatic maintenance facility to update these statistics for us from now on.

Page 104

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

Automatic Maintenance RUNSTATS UI Settings


The first automatic maintenance feature we will explore it the RUNSTATS feature. As of DB2 9.1, the RUNSTATS feature in automatic maintenance is already on by default. Still, we will learn how to use it and customize it in this lab so we can get a handle on how the rest of the automatic maintenance features work. ___25. In Data Studio get to the Database Administration perspective, and find the Administration Explorer ___26. Right click on database AUTONMDB Set Automatic Up and Configure Configure Maintenance

___27. Options screen: Review defaults and leave as-is, click: Online Maintenance Window

___28. Online Maintenance Window screen: Maintenance Window ___29. Offline Maintenance Window screen: Policy

Review defaults and leave as-is, click Offline

Review defaults and leave as-is, click Runstats

Lab 06 Autonomic Computing

Page 105

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___30. Runstats Policy screen: Check off: Create or update the runstats policy. Click on: Selected tables Fill in simple condition as shown below which will only run statistics on tables with names that start AUTOMAINT%

___31. Click on Preview Command

___32. Review the commands that will perform the automatic maintenance set up tasks in your database, then click: [Run]

___33. Wait for the command to finish and make sure it is successful. Close the task assistant editor.

Page 106

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

Checking the UI usage ___34. Now lets see what weve done in the database when setting the automatic maintenance ON for RUNSTATS on all tables. ___35. In your Data Studio view, right click on database AUTONMDB: Set Up and Configure Configure ___36. Find section called Maintenance and find the keyword parameters called AUTO_MAINT, AUTO_TBL_MAINT and AUTO_RUNSTATS.

If these three are set to ON, then DB2 will automatically run statistics in your database. Close this editor window after reviewing. ___37. Find the table called SYSTOOLS.POLICY in Data Studio and browse the data. This is the table that keeps all the automatic maintenance settings you gave it during the UI usage as well as any it determines for itself. Close the editor window when done.

You are now all set up for automatic table maintenance, RUNSTATS on all tables, during your specified maintenance window. DB2 will automatically detect that statistics are required in those AUTOMAINT% tables and run statistics on them. This happens at the discretion of DB2.

Lab 06 Autonomic Computing

Page 107

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Automatic Maintenance RUNSTATS Exercise Finalization


___38. To complete this exercise, run script Autonom37.CMD which executes Autonom38.DML This script will simulate activity against the AUTOMAINT% tables by doing selects, updates and deletes to them. ___39. DB2 will now wake up every 2 hours to see if it is within its maintenance windows. If it is, then it will update the statistics for the selected tables sometime within that window. You can go back to this later to check the results of the automatic RUNSTATS you have configured. To check how DB2 is doing, you can run this script again and again until you see the DB2 engine automatically do the work for you: Autonom33.CMD Review Autonom33_OUTPUT.RPT

Remember, the desired final outcome is for the automatic maintenance feature to produce statistics for your tables without you having to do so manually. The final outcome should look like this:

Page 108

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 06 Autonomic Computing

IBM Software

E. Extra Exercise: Statistic Profiling


DB2 RUNSTATS has the concept of profiling, which is just a way of saving the last way you ran RUNSTATS in your catalog and using that saved command instead of the having to run the entire command all over again. ___40. To see a simple way of how this works, execute these commands: DB2 CONNECT TO AUTONMDB DB2 RUNSTATS ON TABLE DBAPOT.AUTOMAINT_TAB4 ON ALL COLUMNS SET PROFILE DB2 RUNSTATS ON TABLE DBAPOT.AUTOMAINT_TAB3 WITH DISTRIBUTION TABLESAMPLE BERNOULLI(30) REPEATABLE(4196) SET PROFILE ___41. Now execute and review the output from this script again: Autonom33.CMD In output file Automaint33_OUTPUT.RPT, pay special attention to column PROFILE

___42. To use a previously set profile, run this command: DB2 RUNSTATS ON TABLE DBAPOT.AUTOMAINT_TAB4 USE PROFILE

Lab 06 Autonomic Computing

Page 109

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

F. Extra Excercise: SYSTOOLS setup


Automatic maintenance is controlled with policies that are saved by the GUI into tables in a schema called SYSTOOLS. Lets make these tables and review them: ___43. Review and run these scripts: This script will reset your Policy table and effectively undo the previous exercises in this lab. Do not do this exercise until you are satisfied with the other exercises in this lab and have seen the automatic maintenance work for you at least once! Autonom39.CMD which executes Autonom40.DB2 ___44. From Data Studio, find all tables in the SYSTOOLS schema ___45. Review contents of table POLICY There are 5 records in this table Review columns NAME and UPDATE_TIME to get an idea

DB2 normally creates this table for you when you first invoke the Automatic Maintenance UI in Data Studio, but we went ahead and did this manually to see what is involved for purposes of understanding it better in this lab, or this could be done if you every want to start from scratch in your database.

G. Cleanup
After you are done with the above exercises, you can run the following command to clean up the resources: Autonom50.BAT *** End of Autonomic Computing Lab by Burt Vialpando ***

Page 110

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 07 Deep Compression

IBM Software

Lab 07. Deep Compression


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Table Deep Compression


Setting up the database compression exercise using SAMPLE database
___1. ___2. Open a DB2 CLP Command Window and go to the directory C:\POT_DB2\07COMPRESS Run script Compress01.CMD While it is running review the script to notice that it creates a table called COMP1.ORDERS in the SAMPLE database. It generates a data file Compress04.DAT for this table through a data generation program and then loads 1.5 million records in that table through a DB2 LOAD command. Please review Compress01_OUTPUT.TXT when it completes. ___3. In Data Studio, go to the Data perspective and find the SAMPLE database. Find and review the table COMP1.ORDERS. Notice in its properties that it starts out as not using row compression.

___4. ___5.

Open the SQL Script Editor by right clicking on database SAMPLE New SQL Script Run the following SQL in the SQL Script Editor to check the cardinality of this table: SELECT COUNT(*) FROM COMP1.ORDERS

Lab 07 Deep Compression

Page 111

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Estimating Compression
___6. Review and run script Compress05.CMD This script runs a DB2 INSPECT command to estimate the compression and percentage of saving in the data pages if compression is enabled. Review the output file Compress05_OUTPUT.TXT and check the output from INSPECT command to notice the percentage saving it estimates will happen for this table. You will see an output as shown below.

The INSPECT command estimates compression on the table before you actually do it. In this way you can determine whether or not you think it is worthwhile to do compression before actually doing it.

___7.

In the SQL Script Editor, run the SQL statement in script Compress16.SQL

___8.

So, reviewing the view SYSSTAT.TABLES, this table uses 27,287 pages (of 4096 bytes each because that is the table space page size). This means the table usage is 111,767,552 bytes (a little over 110 MB) uncompressed. We should expect a 56% compression savings out of that (according to the INSPECT command used above) or 62 MB of savings for this table.

Page 112

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 07 Deep Compression

IBM Software

Compressing a table Complete REORG


___9. Review and run script Compress06.CMD This script will first alter the COMP1.ORDERS table to turn compression ON and then it will perform a REORG on that table which will build the dictionary used to compress the data during the REORG. This is the most complete way to compress a table because it builds a tablelevel compression dictionary based on all of the data in the table. ___10. The script does a RUNSTATS on the table to collect the latest statistics for us so we can view it again in Data Studio. ___11. Run the SQL from script Compress16.SQL in the SQL Editor for a second time. ___12. Notice it had a compression ratio of the same amount as the INSPECT command estimated.

Compressing a table Partial sample REORG with secondary load


___13. Review and run script Compress07.CMD This script first creates a new table called COMP2.ORDERS which has compression turned ON during its creation using COMPRESS YES. It then splits the 1.5 million record data file into two files of 150,000 records in the first one and the remaining 1,350,000 records in the second one. It then loads the first 150,000 record file into the COMP2.ORDERS table (10% sample). It then does a REORG on the table and builds the dictionary during the REORG but only using the 150,000 record, 10% sample to do so. It then loads the remaining records into the COMP2.ORDERS table which will be compressed DURING the load compression is turned on and because a dictionary was already built for it during the REORG of the partial data 150,000 record data.

___14. When this completes, use the SQL Editor to run script Compress16.SQL for a third time. ___15. Notice now that the table that had the entire REORG (the exercise we just did previously) had slightly better compression than the table with a partial REORG and subsequent LOAD after the compression dictionary was built on the sample data. However, the compression results are an impressive 55% even using the partial LOAD/REORG and subsequent LOAD method.

Lab 07 Deep Compression

Page 113

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Compressing a table using automatic dictionary build & compress


___16. Review and run script Compress13.CMD. This script first creates a new table called COMP3.ORDERS which has compression turned ON during its creation. It then loads all of the data and lets DB2 determine when to build the dictionary and compress. This is by far the simplest of the approaches for row compression for a DBA to implement. ___17. Run Compress16.SQL for a fourth time in the SQL Editor.

___18. Notice that automatic dictionary creation (ADC) doesnt compress all the rows in the table because it takes a certain amount of sampling to build the dictionary first. After that, the dictionary is built and compression of the remaining rows occurs. We still ended up with fairly impressive results with very little work on our part to get this mainly because adaptive compression will kick into to do page level compression as well.

DB2 automatic dictionary creation means that after you ALTER a table to turn compression on, you do not have to REORG the table for a dictionary to be built. DB2 will build the dictionary for you once it determines enough new records have been INSERTED or LOADED to determine a valid compression result. This makes it easy to use, yet shows impressive results.

___19. In Data Studio, find tables COMP2.ORDERS and COMP3.ORDERS and review their properties. Notice what kind of compression gets turned on when using keywords COMPRESS YES.

Page 114

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 07 Deep Compression

IBM Software

Compressing indexes
___20. Review and run script Compress18.CMD. ___21. This script uses the COMPRESS YES for a CREATE INDEX statement. Thats all you need to do in order to compress an index. (If the index already exists, then you can do an ALTER INDEX COMPRESS YES followed by a REORG INDEX command)

___22. The script next uses a table function to help show compression for indexes. We could have used a table function for showing compression on our tables too.

___23. This is the output results from this compression on our index:

C. Clean up
In Data Studio, close your editors. Run Compress50.Bat to clean up resources after above lab exercises.

*** End of Deep Compression Lab - by Vikram Khatri and Burt Vialpando ***

Lab 07 Deep Compression

Page 115

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08. Explain Facilities and the Optimizer


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Create Explain Tables


All DB2 explain facility tools use the explain tables, so lets create them now. We will use the DB2 supplied script found in the install library under folder MISC. The script is called EXPLAIN.DDL. Here is how to do this: ___1. ___2. ___3. Open a DB2 CLP Command Window and change your directory to C:\POT_DB2\08EXPLAIN Review and run the following script to create the explain tables in database GSDB: Explain01.CMD Launch Data Studio (if not open) and open the Data perspective:

___4.

Connect to GSDB database. Verify that the explain tables were created in schema DBAPOT. These tables are named: EXPLAIN_%

The script also created the ADVISE_% tables. The presentation material mentioned how these tables can be used, but we will not be working with these tables in this lab.

Page 116

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

B. Union All View Example Setup


To demonstrate the explain facilities in DB2, we will use an example of a DB2 union all view: ___5. ___6. To set up this example, review and run these scripts: Explain02.CMD which executes Explain03.DDL and Explain04.DDL The end of the output for these scripts should show a check results section with a Monthly and Quarterly Tax summary. If data is returned from this script, then your setup was successful.

___7.

In Data Studio, right click on Tables and choose: Refresh

___8.

Expand Tables again, and find the 4 individual tables named Q?_2012. Sample the contents of one of them to make sure it has data and to see what it looks like. (Hint: use Data Sample Contents)

Lab 08 Explain Facilities and the Optimizer

Page 117

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___9.

Expand table Q1_2012 and review its Constraints.

___10. Find the view called YEAR_2012 and right click on it. Sample contents from it too. ___11. Right click on view YEAR_2012 again and choose Generate DDL.

___12. Choose the defaults using [Next] until you see the Preview DDL screen. Use this to double check your union all view DDL. You can open the DDL for editing here by checking the appropriate box. Then [Finish].

___13. Review the DDL in the editor. You can reformat it here if you want to be able to better understand what it is doing.

___14. Close this editor view and dont bother to save this edited script. ___15. Clean out your SQL Results cache too. (Right click, Remove All)

We are now ready to run SQL against this union all view to see how the DB2 explain facility works.

Page 118

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

C. Visual Explain in Data Studio


___16. In the Data Studio Data Source Explorer, right click on database (not the connection!) GSDB New SQL Script

___17. Using the Windows Explorer, navigate and find directory C:\POT_DB2\08EXPLAIN and double click on file: Explain05.SQL

___18. Copy and paste the contents of this file into your editor view in Data Studio

___19. Notice this script will count all records from the view YEAR_2012 that have a month of value 3 (March). ___20. Highlight and run this script with [F5] to see the results. Your results may vary from the one shown here, but the key thing is that throughout this exercise, your results should not change if we do things to tune this query using explain. Note your query results here: _______

___21. To see this query explained visually in Data Studio, right click anywhere on that query (or you can highlight it) and choose: Open Visual Explain

___22. Choose defaults by using [Next] and [Finish].

Lab 08 Explain Facilities and the Optimizer

Page 119

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

If we had not built the explain tables through the script as we did previously, the Data Studio UI would build them for us anyway at this time by merely invoking the Visual Explain. ___23. Double click on the Access Plan Diagram that was generated to expand it to fill the perspective.

___24. Click on the Scale to Fit icon shown below to make your visual explain fill your screen.

___25. Right click on one of the table icons on the bottom of the diagram and choose: Show Description.

___26. Notice you have details on this explain node regarding the details of the table, its statistics creation time, number of rows, and so on. You even can drill down into its table space, columns, frequent values, and so on.

___27. Close this after reviewing it.

Page 120

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

___28. Find the section on the left side of the Access Plan Diagram called Canvas. Click on it to expand it or to contract it. The click on the tab called Description of Selected Node

___29. Click on the top node called RETURN in your Visual Explain diagram. Notice this node is described in the canvas. Find and expand Environment to see things like the overall parallelism, CPU speed, buffer pool size, etc. used in this query.

___30. Now, notice the total timeron cost from this query shown in the node: RETURN. (To see this, hover over the top RETURN node.) Make note of this result here: __________________

___31. In the Access Plan Diagram, notice that the query is using table scans and these are pretty costly. Review the table scan predicate detail to see which column this is occurring on. Can we reduce the cost of this query somehow to avoid these table scans? (Answer: Yes and we will do this later.)

Lab 08 Explain Facilities and the Optimizer

Page 121

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___32. In the Canvas, find the tab called Overview Diagram. Click on the icons shown below to see what they do.

___33. Close this Access Plan Diagram when you are finished.

Page 122

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

D. Visual Explain Tuning a query


Design change example
The answer to the previous question about being able to avoid table scans was yes, we can create indexes on the tables for the TX_DATE column to reduce the timeron cost of this query. ___34. In Windows Explorer, find script Explain06.DDL ___35. Left click on it and drag it into the editor window of your Data Studio SQL Editor. Let go of it when it is in the window.

___36. You will have a new editor session opened with the DDL from that script in it. ___37. Fist make a connection to the GSDB database by clicking on: No Connection GSDB [Finish]

___38. Now highlight all the statements in the script and then [F5] to run the DDL together. Note: alternatively, if you don't highlight anything, Data Studio runs all SQL together by default.

Lab 08 Explain Facilities and the Optimizer

Page 123

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___39. Review the SQL Results view to see if these were created. Make sure all 4 indexes were created.

___40. If the DDL ran successfully, close the editor for this DDL script. You are done with it. ___41. Find table Q1_2012 in the Data Source Explorer and verify it has an index by expanding as shown below: (note: you may have to refresh the view to see it.)

___42. Highlight the original query in your editor, right click on it and choose: Open Visual Explain again. Choose the same defaults again. Review the Access Plan Diagram now that we have made a design change to the database by adding indexes. Notice the reduced timeron count.

Page 124

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

___43. Notice that even though we were performing a function in the query on the indexed column, DB2 was able to use the index. In other databases, this would require a functional index. The DB2 optimizer is aware of many of its built-in functions and does not require functional indexes for this kind of situation.

Query change example


Is there any more room to improve this query and reduce the timeron counts? For example, why is the query looking through all four tables in the union all view and not just one table? Can we get DB2 to just look at the one table the March data is actually in? To answer these questions: ___44. Use your Windows Explorer to find and review script. Explain07.SQL. Replace the single query in our editor session with the contents of this script with a COPY / PASTE.

This script provides two solutions, both of which work. DB2 can parse the second query, with its function usage on an indexed column, and still return the correct answer quickly without a functional index.

Lab 08 Explain Facilities and the Optimizer

Page 125

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___45. Highlight the first query and Open Visual Explain for it to see the solution work. Notice the total timeron count has fallen even more and that the query is looking through only one table in the union all view.

___46. Why does the SQL work? Because we also provided the YEAR and not the just MONTH in the query. Our union all view was based on the table constraints for each table and those constraints contained both YEAR and MONTH. Just providing a month alone in the query was not enough for the optimizer to prune the other tables out. This is true for partitioned tables in SQL Server, DB2, etc. You must provide the entire partition key in the WHERE predicate for partition pruning to work. ___47. Run this query to make sure the final answer is not changed from our original answer.

___48. Highlight the second query solution (the one using functions) and run it with [F5]. The answer should be the same as when we first ran it at the beginning of this lab. So, adding indexes should not change the result of the query, and writing our query more efficiently should not change the result answer either. ___49. Do a visual explain on the second query too. Notice it also gives an efficient plan.

Page 126

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

___50. But, what about the query rewrite that DB2 does. What does that mean? ___51. In the Access Plan Diagram, find and click on the Canvas section on the left edge as shown here, then make sure you are on the tab Overview of Diagram

___52. On View the SQL Statements (to the left of the diagram) click on the second tab: Compare the Original tab with the Optimized tab

___53. This tab contains the query rewritten by DB2 and is shown the way the DB2 optimizer sees the query. Notice the function calls have been replaced with what looks like a BETWEEN clause. Notice that DB2 has rewritten this query to not use functions. ___54. As a final comparison, find the original "bad" query we ran at the very beginning in file Explain05.sql. Run a visual explain on it and look at the Original vs. Optimized SQL Statements

___55. Close all editor sessions and Access Plan Diagrams. Dont bother saving any scripts.

Lab 08 Explain Facilities and the Optimizer

Page 127

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. Using db2exfmt explain multiple queries at once


___56. To demonstrate how db2exfmt works, review these scripts Explain08.CMD which executes Explain09.DML ___57. From a DB2 CLP Command Window, run Explain08.CMD ___58. Explore output file Explain08_OUTPUT_PLAN.TXT. Notice that explain plans for 3 different queries are included in this output. You can find the beginning of each query by searching on the following text: EXPLAIN INSTANCE

___59. Review the data in the explain tables EXPLAIN_INSTANCE and EXPLAIN_PREDICATE. Notice what the EXPLAIN ALL FOR command produces in these tables.

___60. Review Explain_DB2EXFMT_Command.txt for full command syntax ___61. Use Data Studio to explore the data in EXPLAIN_INSTANCE and EXPLAIN_STATEMENT tables.

Page 128

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

F. Extra Exercise Using db2expln


Static SQL method
Earlier in script Explain04.DDL, we created a stored procedure called: UNION_DATA. Lets find the DB2 package that contains the static SQL in that stored procedure so we can explain that package: ___62. Review and run this script in the SQL script editor: Explain10.SQL Find the package name for the stored procedure in your own database and note it here: _____________

___63. To demonstrate how the db2expln utility works in its static mode, review and run this script: Explain11.CMD [package_name] Dont forget to use the package_name parameter when executing this script!

___64. Explore output file Explain11_OUTPUT_PLAN.TXT Notice packages are broken into sections. Each section is for each SQL statement in the package. So, each SQL statement in any DB2 package can be explained this way.

___65. Close any editor session you may have open in Data Studio. Dont bother to save the scripts.

Lab 08 Explain Facilities and the Optimizer

Page 129

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Dynamic SQL Method


___66. To demonstrate how the db2expln utility works in its dynamic mode, review and run these scripts: Explain12.CMD which executes Explain13.SQL

___67. Explore output file Explain12_OUTPUT_PLAN.TXT Check the explain tables

___68. Review Explain_DB2EXPLN_Command.txt for full command syntax

Page 130

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 08 Explain Facilities and the Optimizer

IBM Software

H. Extra Exercises: REBIND and ROW MOVEMENT


Rebind ___69. Rebind package for the UNION_DATA stored procedure using: Explain14.DB2

Question: Why would you think you would ever need to do this? Answer: For stored procedures, the package is bound when it is created. Statistics and the data model can change over time and affect the access path. A rebind will make sure this path is up-to-date.

Row movement ___70. Single out the first record in table Q1_2012 and note the TX_NUMBER here ________

Through the union all view YEAR_2012 update that record with the date: 2012-12-30. Use script Explain15.DB2 which has the solution and looks something like this: UPDATE YEAR_2012 SET TX_DATE=DATE(2012-12-30) WHERE TX_NUMBER=1; COMMIT;

Question: What happens to the record in the individual tables and why? Can you find it in table Q1_2012? Answer: The data moves from table Q1_2012 to Q4_2012. Note that this is NOT the DB2 default behavior. When this view was created, the syntax used to make this happen was: WITH ROW MOVEMENT. Find the moved record: SELECT * FROM Q4_2012 WHERE TX_NUMBER=1;

In lieu of the above query, you can also right click on the Q4_2012 table and choose: Return all rows. Then, in the SQL Results, scroll down to the last record. Notice this moved record is there. Try the same in table Q1_2012. You'll see the record is gone.

___71. Close all editors and exit from Data Studio. *** End of Explain Facilities and the Optimizer Lab by Burt Vialpando***

Lab 08 Explain Facilities and the Optimizer

Page 131

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09. Workload Management (WLM)


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Setting up a custom workload manager environment


___1. In a DB2 CLP Command Window, change the directory to C:\POT_DB2\09WLM

___2.

Review and run WLM01.CMD. This script will execute SQL against the system catalog of the SAMPLE database showing how the WLM environment looks. This first run of this script shows the default workloads and no defined service classes, which is the way a DB2 database looks just after creation. Review WLM01_OUTPUT.TXT to see how a default WLM database environment looks.

___3.

Review scripts WLM03.CMD and WLM04.DB2. This will customize the WLM environment. Notice it creates workloads, services classes, thresholds, work class sets, work action sets, etc. This script does these things in a particular order since the WLM objects are hierarchically dependant upon one another.

Page 132

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___4.

When reviewing the script, there are two workload / service class configurations you should pay attention to: WORKLOAD #1: Name: Identify by: Service Class: (higher resource usage) CLP_Workload_Admin Client userid db2cobra CLP_Serv_Admin
Let prefetch, agent and buffer pool priority default

Service subclass1: CLP_Serv_Admin_HI


Give a high prefetch, agent and buffer pool priority

Service subclass2: CLP_Serv_Admin_MED


Give a medium prefetch, agent and buffer pool priority

Work Act. Set:

Admin_Actions
Map WRITE work to the high sub service class Map READ work to the medium sub service class

WORKLOAD #2: Name: Identify by: Service Class:

(Lower resource usage) CLP_Workload_User1 Client userid db2user* CLP_Serv_User


Give a low prefetch, agent and buffer pool priority

___5. ___6.

Run script WLM03.CMD to create these WLM customizations. Run script WLM01.CMD again and review WLM01_OUTPUT.TXT again to see how the WLM environment has changed. Study this for all WLM objects created.

Lab 09 Workload Manager (WLM)

Page 133

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___7.

From this output, note the following things:


The service superclass called CLP_Serv_User does not have a defined service subclass, but it still uses one: SYSDEFAULTSUBCLASS. Workload IPADDR does not have any defined service classes, so it will use a default. Notice the thresholds and event monitors the script created too.

Setting up Priority Aging ___8. ___9. Review script WLM05.CMD. Notice it calls a script wlmtiersdefault.db2 which is supplied in the DB2 sample scripts directory in the DB2 install path. Review script wlmtiersdefault.db2. This script is useful for instructing you how to set up a WLM priority aging environment, where service subclasses can remap activities to other service subclasses, while those activities are running. The key to how this works is shown below in excerpts from this script:

Page 134

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___10. Run script WLM05.CMD. Then run WLM01.CMD a third time. Review WLM01_OUTPUT.TXT a third time to see your WLM environment now set up with priority aging. Notice the script altered the SYSDEFAULTUSERWORKLOAD to map to the WLM_TIERS service superclass.

___11. Notice too that in our earlier script we created some event monitors that write to a DB2 table. We will be using these later.

___12. Review and run WLM10.CMD to see this WLM using the db2pd utility. WLM10_OUTPUT.TXT contains the db2pd report. This report has zeros in the activity counts because we havent started running an active workload on the database yet.

Lab 09 Workload Manager (WLM)

Page 135

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

B. Setting up to simulate a workload


___13. Review and run WLM20.CMD which runs WLM21.DDL. These scripts will create a table and inserts 200,000 records into it. This table is used in a simulated workload later.

___14. This script takes a few minutes to run, so while it is running go on to the next steps to save time in this lab. ___15. In Data Studio, we will connect to the SAMPLE database as user db2cobra, password db2cobra. Right click on SAMPLE then: Properties ___16. In the Driver Properties screen, fill in the new credentials as below, then [OK]. Answer "yes" to reconnect.

Note: if the reconnect fails, just connect as usual. ___17. Start the Data Studio SQL Script Editor for the SAMPLE database.

___18. Copy and paste into the editor the contents of script file: WLM30.SQL Hint: it is located at: C:\POT_DB2\09WLM\

Page 136

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___19. You should now have a session that looks like this:

___20. Scroll down to find the very first SQL SELECT statement in the script. It is labeled #1 Workload -> service class occurrences. Highlight it and run it. (note: you can also use [F5] to run it.)

___21. The db2bp.exe application name is the background process of our CLP session that we used to create the WLM environment in. It is still connected so it is using a default for now. The db2jcc_applicat application names are for the Data Studio tool itself.

___22. Go to your DB2 CLP Command Window that you ran the script in and type this: db2 terminate ___23. Return to the SQL Script Editor session and run script #1 again to see the CLP workload gone.

Lab 09 Workload Manager (WLM)

Page 137

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Running a workload as user db2user1


We are now ready to start a workload in order to monitor it through built-in WLM monitoring capabilities. ___24. Review WLM22.CMD. Notice this script connects as user db2user1 and it sets the client information to set our client_userid as db2user1. This stored procedure is necessary to use since we are submitting this work through the CLP and we want the CLIENT_USER to be db2user1 (not the SESSION_USER or SYSTEM_USER only)

___25. Notice also the script runs batch file WLM28.BAT 200 times.

___26. Notice WLM28.BAT runs WLM29.DML (which will be done 200 times):

___27. WLM29.DML is a collection of SELECT, INSERT, UPDATE, DELETE and other DB2 workload:

Page 138

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___28. Execute WLM22.CMD to see it begins its 200 cycles of workload as user db2user1

___29. Return to the SQL Script Editor and run script #1 again. You will now see a new db2bp.exe application running user db2user1. This user is NOT using the default workload. It is using workload CLP_Workload_User1 which in turn uses the service superclass CLP_Serv_User.

___30. db2user1 mapped to this workload because of this statement we used when we created it:

___31. In the SQL Script Editor, find and run query #2 Service class activity counts. Youll see the service class CLP_Serv_User has been busy completing many activities.

Please keep the first CLP window open and running the WLM22.CMD script for the remainder of this lab. Do not close it! If it should complete before you are done with this lab, just start it again in its window.

Lab 09 Workload Manager (WLM)

Page 139

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. Running a second workload as db2cobra


___32. Open a new DB2 CLP Command Window and change directory to C:\POT_DB2\09WLM

___33. Review and run WLM23.CMD. Notice this script connects as user db2cobra and it sets the client information to set our client_userid as db2cobra. ___34. This script will run the same exact workload 200 times just as the previous script did, but it will use a different client userid. ___35. Notice that script WLM22.CMD is started first and so it is further ahead of the 200 cycles of workload than script WLM23.CMD. (your results will vary.)

___36. Return to the SQL Script Editor and run script #1 again. ___37. Notice that now db2cobra is running under a different workload CLP_Workload_Admin and this uses a different service superclass CLP_Serv_Admin.

___38. If you recall, this service class runs with much higher resource allocation than the one db2user1 is using. We should expect to see it finishing more activities faster.

Page 140

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___39. Run script #2 again to see the activities it has done so far. The total number of activities is behind the other service class right now. But, we should expect it to catch up at some point and pass the number of activities in the lesser service class.

Please keep this second CLP window open and running the WLM23.CMD script for the remainder of this lab. Do not close it!

Lab 09 Workload Manager (WLM)

Page 141

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. Running a third workload as user db2default


___40. Open a new DB2 CLP Command Window and change directory to C:\POT_DB2\09WLM

___41. Review and run WLM24.CMD. Notice this script connects as user db2default and it sets the client information to set our client_userid as db2default. ___42. Run script #1 and #2 again in the SQL Script Editor.

___43. Run script #3 Work Action Set Activity Count. Remember, script wlmtiersdefault.db2 used work action sets to do initial mapping into the service subclasses depending upon what type of activity it was.

We can also use work action sets to map to subclasses based on data tags in table spaces or storage groups. We don't show that in this exercise, but it works in the same way.

Page 142

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

___44. Run script #4 Long query to test activity remapping. This script will run longer than the subclass threshold allows and will remap the query activity to another subclass. (Dont forget to select and run the FLUSH PACKAGE CACHE DYNAMIC statement with the query!) This might take a while to run, so be patient. If it doesn't finish in a few minutes, you can terminate it like this:

___45. When it finishes, run script #2 again and look at the ACTIVITIES_REMAPPED_IN and ACTIVITIES_REMAPPED_OUT columns. (You may have more than one remap shown.) Notice the subclasses these remaps belong to. WLM_SHORT will remap to WLM_MEDIUM. WLM_MEDIUM will remap to WLM_LONG This is an example of priority aging.

We can also use thresholds to remap based on data tags in table spaces or storage groups. We don't show that in this exercise, but it works in the same way.

Lab 09 Workload Manager (WLM)

Page 143

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

F. Working with WLM event monitors


___46. Run the CALL WLM_COLLECT_STATS() stored procedure in the script

___47. Immediately run script #2 again.

___48. Notice the in-memory statistics have been set back to zero. But, fortunately, they have been collected in our event monitors. Since our event monitors were created as tables, we can easily select from them. ___49. Run scripts #5, #6, #7 and #8. These seem familiar as they are like the in-memory statistics queries we were using earlier, only now we are using our event monitor tables. These persist after a database is deactivated, in-memory statistics dont. ___50. Now, lets look at a very special event monitor called Threshold Violations. This contains information we cannot get from in-memory statistics. Run script #9. Threshold violations event monitor summary table select

___51. Notice we show two threshold violated, one resulting in a Continue and another in Remap we discussed earlier. ___52. If you are not doing the extra exercises in the next section, you can close all the DB2 CLP Command Windows now.

Page 144

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 09 Workload Management (WLM)

IBM Software

G. Extra Exercise: Miscellaneous WLM lessons


Using the entire in-memory functions: ___53. Review and run each in-memory function that is provided in the script. These are the same functions that we were using earlier in the labs. The earlier exercises only used some of the columns, so these will give you every column, not just the ones we wanted to show you earlier.

Revisiting db2pd: ___54. Open yet another DB2 CLP window and run script WLM10.CMD. This is the db2pd script. This time you will have in-memory statistics from running workloads to review this utility with. ___55. Close all DB2 CLP Command Windows now.

H. Clean up
___56. Run WLM50.BAT to clean up resources after above lab exercises. *** End of Workload Manager (WLM) Lab by Burt Vialpando ***

Lab 09 Workload Manager (WLM)

Page 145

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10. High Availability Disaster Recovery (HADR)


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. HADR Lab Overview


In a normal situation, you would configure HADR on two separate servers. For demonstration purposes for this lab, we will create two separate instances and configure HADR on each database in each instance on the same machine. We will use the default DB2 instance to make the first connection to the HADR database. The following is an overview of how this lab will be set up:

Lab Exercise Host Machine


Primary Database (Server) Instance Name = DB2_P Create Database = HADB Turn on Archival Logging Take a backup of HADB database Enable HADR on Primary Standby Database (Server) Instance Name = DB2_S Restore Database HADB from Primary Enable HADR on Standby Start HADR in standby mode

Client Application Machine Instance Name = DB2 Make a connection to the HADB database

In this lab exercise, we set up HADR manually with a script. However, back in your own IT environment, you probably should set up HADR for the first time using the Data Studio HADR wizard, as it is very easy to use and will walk you through the required steps.

Page 146

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

B. Set up HADR
___1. ___2. ___3. ___4. Close Data Studio if it is open. Open a DB2 CLP Command Window and change the directory to: C:\POT_DB2\10HADR Run the script HADR01.CMD. While it is running, review the script which does the following steps: ___a. ___b. ___c. ___d. ___e. ___f. ___g. ___h. ___i. ___j. ___k. ___l. ___m. ___5.
Create a DB2_P instance (assume this to be an instance on the primary database server). Create an HADB database in the DB2_P instance. Turn on archival logging for this database. Take a backup of the HADB database. Create a DB2_S instance (assume this to be an instance on the secondary database server). Restore HADB database in the DB2_S instance from the backup taken in the previous step d. Catalog the DB2_S instance in the DB2_P instance and the DB2_P instance in the DB2_S instance On the DB2_P instance, catalog the local HADB database as HADBP and the remote HADB database on DB2_S as HADBS. On the DB2_S instance, catalog the local HADB database as HADBS and the remote HADB database on DB2_P as HADBP. Configure client re-route on DB2_P and DB2_S instances for HADBS and HADBP databases respectively. Configure HADR on DB2_P and DB2_S instances for HADB database. Start HADR on DB2_S database server in STANDBY mode Start HADR on DB2_P database server in PRIMARY mode.

Please note the following TCPIP ports used in this HADR lab. Type DB2 default instance DB2_P Primary HADR instance DB2_S Standby HADR instance HADR Port on Primary HADR Port on Standby Port Number 50000 51600 51700 51800 51900

Lab 10 HADR

Page 147

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Create Objects in Primary Database and Monitor HADR


___6. Open Data Studio by clicking on this icon on your desktop or in your task bar:

ORYou can open Data Studio by choosing:


Start All Programs IBM Data Studio Data Studio 3.1.1 Full client

___7.

After the perspective opens, close the Task Launcher view

___8.

In Administration Explorer, expand localhost 51600 and select HADBP. Right click and click Connect.

___9.

Specify user name dbapot and password dbapot123. Check Save Password. Click OK.

Page 148

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

___10. Notice that 51600 changes to DB2_P after establishing the connection.

___11. Expand 51700 and select HADBS. Right click and click Connect.

___12. Specify user name dbapot and password dbapot123. Check Save Password. Click OK.

___13. Right click HADBP. Select Manage Database Manage HADR

Lab 10 HADR

Page 149

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___14. You will see Manage HADR view open in the workspace. Scroll down to the standby database section. Select HADBS from the dropdown of the Connection profile.

___15. Click Refresh frequency dropdown and select 30 seconds.

Page 150

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

Create some tables to work with


We created an empty database HADB and enabled it for HADR. We will test HADR by first setting up some tables using script: HADR02.CMD to: Create the HATABLE table, primary key, indexes, and insert 50,000 rows. The HADR process should propagate the table, primary key, indexes and data to the standby as we do this. Create the HALOAD table, primary key, indexes, and load data using the DB2 LOAD utility in COPY YES mode. Since the LOAD operation does not log the data, the COPY YES option allows the standby database to execute the LOAD command using the same data that was saved by the LOAD operation. It is critical that the path used in COPY YES operation is available to the standby database. The standby database will also have the data loaded to it.

___16. Go back to your DB2 command prompt and run the command HADR02.CMD. This command will create a HATABLE and HALOAD table in HADB database on the primary server. Run the command HADR02.CMD now. ___17. After starting the above command, go back to the Data Studio Manage HADR view (you can use [Alt][Tab] to do this) ___18. Observe the Log position and Log page after creation of the two tables.

Lab 10 HADR

Page 151

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___19. Expand HADBP and select Tables. Notice that these two new tables show in the view.

___20. Right click HALOAD table and select Browse Data.

___21. Click x to close HALOAD view.

Page 152

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

___22. Expand HADBS and select Tables. Right click HALOAD table and select Browse Data.

___23. Notice the error thrown for the STANDBY database. Click OK.

Note: The standby database is available for READ only operations due to registry variable DB2_HADR_ROS=ON but only uncommitted read (UR) isolation level is allowed. Since Data Studio did not use UR, we see above error. To avoid the above error, we have to use isolation level to UR in the select statement or set DB2 registry variable DB2_STANDBY_ISO to UR. When standby database becomes primary, this variable has no effect and only applies to the database as a standby.

Lab 10 HADR

Page 153

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___24. Click down arrow next to the Task Navigator and select SQL and Routine Development. [By doing so, the perspective will change from Database Administrator to the IBM SQL and Routine Development.]

___25. Click File Open File.

___26. Change directory to C:\POT_DB2\10HADR. Select script HADR06.SQL and hit [Open]. Hit OK to accept semicolon as the statement terminator.

Page 154

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

___27. Click No Connection.

___28. Select HADBS database. Click Finish.

___29. Highlight first SQL statement, and click the Run SQL icon.

___30. From the Status tab of the SQL Results view, notice that the SQL runs successfully since we used UR isolation level. Review the Results tab as well to see it returned the data.

Lab 10 HADR

Page 155

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___31. Highlight second SQL statement and click Run SQL icon. Notice the error in the SQL Results view since we tried to do an update on a read only database.

Page 156

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

D. Monitor HADR outside the GUI


HADR monitoring can be done by using snapshots (old and expensive technique), the db2pd utility (light weight utility) and an administration view (SYSIBMADM.SNAPHADR based upon snapshots). ___32. Run the command HADR03.CMD to monitor HADR using the db2pd utility.

___33. The output from the above command shows the status of HADR on primary and standby databases.

Lab 10 HADR

Page 157

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. Switch Role of Databases


___34. Review script HADR07.CMD from the C:\POT_DB2\10HADR and notice the TAKEOVER command to switch the role of the database.

___35. Go to the DB2 Command Line Window and run HADR07.CMD.

___36. The TAKEOVER succeeds on the standby database.

___37. Go to the Manage HADR view in the Data Studio. Notice that the Primary Database role has changed to Standby now.

Page 158

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 10 HADR

IBM Software

___38. Expand DB2_P HADBP from the Administration Explorer view. Select Tables. Right click on HALOAD table and click Browse Data. The error statement confirms that the database role is now standby after we executed the TAKEOVER command in the previous section.

___39. Expand DB2_S HADBS from the Administration Explorer view. Select Tables. Right click on HALOAD table and click Browse Data.

Lab 10 HADR

Page 159

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___40. Notice that you can see the data in the HALOAD table, which was populated using the DB2 LOAD utility on the primary database.

Note: The LOAD operation is not a logged operation but COPY YES option allows HADR to apply LOAD on the Standby database. ___41. Use the Windows Explorer to review the following directories you have created with script HADR01.CMD. Make sure your REFRESH your screen to see the new directories. Note: These are shown from the C:\ root directory:

F. Cleanup
After you are done with the above exercises, you can run the following command to clean up resources. Run: HADR50.BAT *** End of HADR Lab by Vikram Khatri and Burt Vialpando ***

Page 160

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 11 pureXML

IBM Software

Lab 11. IBM DB2 pureXML


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Create a Supporting Database and an XML Ready Table


First, in order to start working with IBM DB2 pureXML, well create a database for it. ___1. ___2. ___3. Close Data Studio. (We will show you how it adds a newly cataloged database automatically.) Open a DB2 CLP Command Window and position yourself in C:\POT_DB2\11PUREXML Run script: pureXML01.CMD which executes pureXML02.DB2

___4. ___5.

Review these scripts while they are running. Wait for the scripts to complete. Launch Data Studio when the scripts are finished.

___6.

In the Database Admin perspective, find database XMLDB that was just created and right click on it. Connect with the user name dbapot and password dbapot123

Lab 11 pureXML

Page 161

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___7.

By the way, you can manage all your database user names and passwords in one screen easily by right clicking on the database and choosing: Change User Names and Passwords

___8.

Close from that screen after reviewing what it can do for you.

___9.

Next, lets create a table that will support XML. All this means is that it has at least one column with a DB2 data type called XML

___10. Review and run scripts: pureXML03.CMD which executes pureXML04.DB2 Open the Data perspective in the Data Source Explorer

Find new table DBAPOT.XML_BOOKS in Data Studio. Click on it and then review the columns in its properties view. Notice the XML column called BOOK_DOC.

Page 162

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 11 pureXML

IBM Software

B. Inserting and Exploring XML Data


___11. Review and run scripts: pureXML05.CMD which executes pureXML06.DB2 Right click on XML_BOOKS and choose: Data Edit

Notice three new records in the table. Review the XML documents by clicking on the BOOK_DOC fields in any row. Ellipses will appear. Click on the ellipses.

In the XML Cell Editor, find the XML Tree tab. Expand the tree and review.

Explore other XML documents and see if you can tell how they are different from each other. Notice how they have different element structures. For example, the XML document for the record BOOK_ID=34567 has an element called extras that the other XML documents do not have. This shows the flexibility of XML. Notice you can edit this data if you wanted to. We won't be doing that for this lab . Click [Cancel] when done reviewing.

Lab 11 pureXML

Page 163

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Exploring XQuery using the Command Editor


___12. Next we will use the SQL Script Editor to execute XML based Queries. Right click on the database XMLDB, choose: New SQL Script

___13. In your Window Explorer, find and open the file: pureXML07.SQL ___14. Copy and paste all the queries in this file into the SQL Script Editor window

___15. Run the first query only by highlighting it and then: [F5] Note: alternately, you can use the RUN icon to run the query:

Page 164

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 11 pureXML

IBM Software

___16. Do this for each query and explore the result sets from these queries to learn more about exploiting the power of XML in your DB2 hybrid pure native XML database! Notice below one that uses a complex xQuery FLWOR expression:

___17. Close the SQL Script Editor when finished. ___18. Close all editor view sessions. ___19. Clean out your SQL Results view too.

Lab 11 pureXML

Page 165

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Importing and Exporting XML Data


Importing XML data Using a script
___20. Review and run scripts: pureXML08.CMD which executes pureXML09.DB2 ___21. As the presentation already covered, the key to the import utility working for the XML data is the XML FROM keywords which point to subdirectory the XML documents are stored in.

___22. The data file itself has pointers to the exact file, and offset within that file, where the XML data is located for each row of data.

___23. Use Data Studio to view the newly imported data. (Hint: use Edit)

Page 166

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 11 pureXML

IBM Software

Extracting XML data using a UI


___24. In Data Studio and right click the table XML_BOOKS Data Extract

___25. Fill in the output file name, like below. Leave the delimiters as-is:

___26. Find and open the file in Notepad

Lab 11 pureXML

Page 167

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

D. Creating Indexes on XML Data


___27. From Data Studio, click on the table XML_BOOKS and view its indexes. Review each of the generated indexes Properties to see how XML columns automatically generate indexes to support them.

___28. Review and run script: pureXML11.CMD which executes pureXML12.DDL Note: there are MANY ways to create supporting indexes on a DB2 XML column. You must find out from your application developers how they are using their XML data to best create these. The Data Studio task assistant UI can help you do this.

___29. Refresh your index listing. Notice the new indexes you have just created. Click on each and explore them in the Properties view.

Page 168

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 11 pureXML

IBM Software

E. Extra Exercise: Registering an XSR


___30. Review and run script: pureXML13.CMD which executes pureXML14.DDL Notice the XSR is created by registering the XSD file that is in pureXML15_book.xsd ___31. Find and Refresh the Schemas in your XMLDB database

___32. Find schema BOOKS, then XML Schemas SCHEMA1

___33. Alter the XML Schema Repository to explore it some more

Lab 11 pureXML

Page 169

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

F. Extra Exercise: Validating with the XSR


___34. Copy script pureXML16.DML into the SQL Script Editor and run it. (Hint: New SQL Script) What SQL error code do you get and why? (Answer: Element nonxsdelement isnt allowed by our XSD validation check.)

___35. Edit your INSERT statement and take out the element called <nonxsdelement> that is not registered in your XSR and rerun the INSERT statement. Your INSERT statement should now validate against your XSR and succeed.

G. Cleanup
After you are done with the above exercises, you can run the following command to clean up the resources: pureXML50.BAT *** End of pureXML Lab by Burt Vialpando ***

Page 170

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

Lab 12. Multidimensional Clusters (MDCs)


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. CARS Tables Example Setup


To show MDCs in action, we need to create two otherwise identical tables in their own table spaces, but one is MDC and one is traditionally index clustered with indexes on all the key columns. The data well put into these tables is identical for each table. ___1. Open a DB2 CLP Command Window and change directory to C:\POT_DB2\12MDC

___2.

To set up this lab, run these scripts right away without reviewing them:

MDC01.CMD which executes these 3 scripts: MDC02.DB2 & MDC03.DB2 & MDC04.BAT

This should take about 4 to 5 minutes to complete. While these scripts run, take this time review what the scripts are doing. Here is an overview of what these scripts do: Overview of what the setup scripts create in database GSDB:
Table: CARS Table: CARS_MDC Table: CARS_TIMING_CHECK Table space: CARS_DATA Table space: CARS_INDEX Table space: CARS_MDC_DATA Table space: CARS_MDC_INDEX Buffer pool: CARS_BP_DATA Buffer pool: CARS_BP_INDEX Buffer pool: CARS_MDC_BP_DATA Buffer pool: CARS_MDC_BP_INDEX Procedure: CARS_INSERT_ALL Procedure: CARS_FETCH_CHK Procedure: CARS_INSERT_CHK Procedure: CARS_UPDATE_CHK Procedure: CARS_DELETE_CHK Regular table w/clustered index and regular indexes MDC table, with dimensions CARNAME, COLOR & YEAR Repository table used to collect timing statistics Each table and indexes for each table has its own table space which has its own buffer pool. (the timing table is put in the userspace1 table space)

Buffer pool for non-MDC data Buffer pool for non-MDC indexes Buffer pool for MDC data Buffer pool for MDC indexes Used to populate CARS & CARS_MDC with generated data Used to perform fetches on both tables and time the results Used to perform inserts on both tables and time the results Used to perform updates on both tables and time the results Used to perform deletes on both tables and time the results

Lab 12 Multidimensional Clusters (MDCs)

Page 171

IBM Software

DB2 10.1 Administration for the Experienced Oracle DBA

B. Explore MDC Space Usage


The table spaces First, lets explore how the space allocation is used with our table spaces by using the system catalog information. ___3. ___4. Launch Data Studio and connect to database GSDB in the Data perspective Open a SQL Script Editor window and cut and paste the SQL from file MDC10.SQL.

___5.

Run the SQL and review the output from the system catalog on table space usage

___6.

Since the data and indexes are in their own table space for the CARS and CARS MDC tables, we can easily see the usage in pages of each of these solutions. Notice that the total for CARS_DATA + CARS_INDEX table spaces take up over 4,000 pages. Notice that the total for CARS_MDC_DATA + CARS_MDC_INDEX table spaces take up less than 3,000 pages. Notice the CARS_MDC_DATA alone however, is greater than the CARS_DATA Notice it is the savings in index space that makes the CARS MDC solution smaller overall

Note: IBM does not claim to save space with MDCs in every scenario. But the very small index size required to support MDCs is helpful in offsetting the space used up by the data.

Page 172

DB2 10.1 Administration for the Experienced Oracle DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

The tables Now let's explore this at the table level from the system catalog. ___7. ___8. ___9. In the SQL Script Editor window and cut and paste the SQL from file MDC11.SQL. Run the script. Notice that in fact the MDC table uses up more data pages, even though it has the exact same data and number of rows. Why? Because some extents are allocated, but not used up. (Remember the presentation?)

The indexes Now let's explore the indexes from the system catalog. ___10. In the SQL Script Editor window and cut and paste the SQL from file MDC12.SQL. ___11. Run the script. ___12. Notice the indexes for the MDC tables are significantly smaller and less complex ___13. Notice the indexes for the MDC tables are generated names, created during table creation time

Lab 12 Multidimensional Clusters (MDCs)

Page 173

IBM Software

DB2 10.1 Administration for the Experienced Oracle DBA

C. Explore MDC Table Organization


Lets look at how these two tables differ in the way the data is added to the tables themselves: ___14. From Data Studio browse the data in the DBAPOT.CARS table first (use Return all rows)

___15. Notice what the first 10 rows looks like unfiltered and unsorted (your solution may vary as the data is randomly generated)

___16. Now browse the CARS_MDC table next (again Return all rows) ___17. Notice what the first 10 rows of this table looks like unfiltered and unsorted (your solution may vary)

___18. Notice that the CARS data look randomly put into the table. Actually, it is randomly put into the table. (Only sorting or indexing on it will give it order.)

Page 174

DB2 10.1 Administration for the Experienced Oracle DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

___19. Notice the CARS_MDC data is grouped together. This is because it is not randomly put into the table. It is organized by the CAR, COLOR and YEAR block. ___20. Scroll down in the CARS_MDC table until you see the CARNAME change. This is the very next block of data. From this example, we can conclude we were able to fit 169 records into one MDC block. That is GREAT record co-location!

___21. Is the data really identical in the data groupings between the two tables? Let's check this out by running the SQL from file MDC13.SQL. You'll notice that the results for both tables are identical meaning there are the same numbers of each car dimension. You can change the query to see the color or year dimensions too if you want to and see these too are identical.

Lab 12 Multidimensional Clusters (MDCs)

Page 175

IBM Software

DB2 10.1 Administration for the Experienced Oracle DBA

D. Using the db2batch Benchmarking Utility


Use db2batch to test the fetch timing from both the CARS and the CARS_MDC tables. DB2BATCH is a benchmark testing utility and well demonstrate how it works here as well as show how fetching with MDCs is faster: ___22. Disconnect from GSDB in Data Studio since it might be browsing or editing the tables and could affect this test. ___23. Run MDC20.CMD which executes MDC21.DB2 and MDC22.DB2 ___24. Review output in the two MDC20_OUTPUT_*.OUT files

A final note: this is a very small table compared to a typical data warehousing fact table. You can expect even much better performance from MDCs in most real-world situations.

Page 176

DB2 10.1 Administration for the Experienced Oracle DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

E. Extra Exercise - MDC Performance Timing Test


There are four stored procedures we will be using to test the timing of fetch, insert, update and delete actions against both tables. You can review the code for these stored procedures in the file MDC03.DB2 you executed earlier to satisfy yourself that they perform the exact same actions against both tables in the exact same way. The procedures further place the output of this test in the CARS_TIMING_CHECK table so you can review total time spent (in microseconds) on each action.
Before you do this exercise, make sure you are no longer browsing any of the cars tables in Data Studio or any other way. Disclaimer: The purpose of this test is not to try to prove anything specific about performance with MDCs, but is rather to get you to think about how performance will work with MDCs in your environment going through various DML types. We realize this test is neither complete enough nor large enough to be considered comprehensive. The DB2 Toronto Labs have done much more extensive testing and proving of MDC performance than what will be shown in these exercises. Contact your local IBM DB2 representative if you want more details on actual MDC performance test results.

For reference, here are the different colors used in our CARS tables: RED, BLUE, GREEN, YELLOW, BROWN, BLACK, PURPLE, SILVER, GREY, ORANGE, PINK, WHITE, CLEAR

Fetch timing test


___25. From a DB2 CLP Command Window connect to the GSDB database and type in the following: db2 call cars_fetch_chk(xxxx) Substituting xxxx with the color of your choice. (The case of the color is not important, you can use red or RED for example). This stored procedure will fetch all records from each table that has the color you indicated Use three or four different colors to get more than one result for a fetch from both tables Also, use a made up color you know that does not exist like RRRR

Lab 12 Multidimensional Clusters (MDCs)

Page 177

IBM Software

DB2 10.1 Administration for the Experienced Oracle DBA

___26. From Data Studio browse the table: CARS_TIMING_CHECK. This is the table the stored procedure puts the results in. Review the TOTAL_MICROSCNDS column for time spent on each fetch activity; which seems to run faster fetches, MDC or non-MDC? Answer: MDCs are faster at fetching data when using any dimensional column Note: values not found (like our example below "RRRR") is equally fast for both tables

Use the Data Studio SQL Script Editor to run script MDC30.SQL to get summary totals from the CARS_TIMING_CHECK table:

Generally speaking, MDC tables can be very efficient for fetching data. Though both tables are highly indexed, MDC indexes are smaller and thus more efficient. Further, you have automatic prefetching advantages for MDC tables due to collocation of like rows. The non MDC clustered table may degrade over time. Run a test of more colors to see the numbers begin to really add up and imagine warehouses with tables with millions of rows and thousands of queries.

Page 178

DB2 10.1 Administration for the Experienced Oracle DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

Insert timing test


___27. From a DB2 Command Window db2 call cars_insert_chk(xxxx) TIP: Use different colors in this test than you used in the previous test Try an insert on a made up color you know does not exist, like use: IIII Repeat as above: browse the results table again and rerun the summary script MDC30.SQL again to get a new running total

Inserting into MDC tables has a different free space search algorithm than for non MDC tables so random inserts into MDC tables can take a bit more time. Sometimes it does and sometimes it doesn't. You can improve on inserts by sorting them first. INSERT into MDCs utilizes partially filled blocks before creating new ones. The LOAD utility has been enhanced to work with MDCs at a block level so it is more efficient than INSERT. Presorting data for a LOAD helps improve performance and space usage, but it is not necessary. LOAD does not use partially filled blocks though.

Lab 12 Multidimensional Clusters (MDCs)

Page 179

IBM Software

DB2 10.1 Administration for the Experienced Oracle DBA

Delete timing test


___28. From a DB2 CLP Command Window db2 call cars_delete_chk(xxxx) Repeat as above; make sure you take into account some colors have changed Try a made up color that does not exist, like using DDDD Browse the timing table again

DELETE from an MDC table can also be quite fast. If you delete on a dimension column, MDCs know already all the blocks that qualify and can delete a block at a time. Also, it has less bid indexing to maintain. Non MDC tables must both find each record, wherever they are, and then delete them and update the rid indexes each time, thus being slower. Your test should have shown an advantage here over the non MDC table. ___29. Run script MDC30.SQL to get final totals from the CARS_TIMING_CHECK table How did your MDC table do? Your run might look like this below. Notice that MDC tables are faster on bulk fetches and deletes.

Page 180

DB2 10.1 Administration for the Experienced Oracle DBA

Lab 12 Multidimensional Clusters (MDCs)

IBM Software

Update timing test


___30. From a DB2 CLP Command Window db2 call cars_update_chk(xxxx) Repeat as above and try to use colors you have not yet used. Make sure to pick colors that are still in the database, so don't use the ones you deleted in the delete test. Note: this procedure updates the color you choose and adds an X to the front of the data.

UPDATE of MDC tables can be faster or slower depending upon what is being updated. In this exercise, we updated on one of the dimension columns which can make the MDC UPDATE fast because it changes every record in the block quickly as it reads all records together and writes it back in bulk. If our test were to only update one record on a dimension column however, it would have to relocate the data to a new or different block making it slower. How did your test do?

F. Cleanup
After you are done with the above exercises, you can run the following command to clean up the resources: MDC50.BAT *** End of MDC Lab by Burt Vialpando ***

Lab 12 Multidimensional Clusters (MDCs)

Page 181

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 13. Data Movement Utilities


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. db2relocatedb (a transportable database utility)


The db2relocatedb tool is for a copy or move of an entire or partial DB2 database at the operating system level. This tool is very useful for fast and easy copying of a large database from one machine to another without having to rely on a backup and restore operation. It is restricted to movement within the same endian platform however, so for example you could not relocate a Windows based database to a UNIX one. UNIX to UNIX moves within the same endian family are possible however. To demonstrate this feature we could call a transportable database, we will do the following steps: ___1. ___2. ___3. If Data Studio is open, go ahead and close it. Open a DB2 CLP Command Window and position yourself in directory C:\POT_DB2\13DATA Review and run script Data01.CMD to set up the database called: DATA Data01.CMD creates the following: Source database: DATA (in instance DB2) Source data table space: DATA Source index table space: IDX Source table: RELO_TB (with 1000 rows) Target instance: DB2PROD (this is where we will copy the source objects to) ___4. Open Data Studio after the above script completes and get into the Database Administration perspective.

___5. ___6.

Connect to the DATA database in the DB2 instance. (Hint: remember to connect with user dbapot, using password dbapot123.) Expand Data Table Spaces. Verify that it has two DMS user table spaces.

Page 182

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

___7.

Select Data table space. Click Containers from the Properties view and check for the container name.

___8.

Select Tables. Verify that it has RELO_TB table.

___9.

Right click on the RELO_TB table and select Browse Data.

___10. Using the Windows Explorer, check for the table spaces data files that we created in the script. We will be copying these two table space containers to the instance DB2PROD manually later.

Lab 13 Data Movement Utilities

Page 183

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___11. From the DB2 CLP Command Window run this command: C:\POT_DB2\13DATA\>db2 list db directory on c:

___12. We will copy this database from the instance DB2 to a new instance called DB2PROD using the db2relocatedb utility. ___13. Using your Windows Explorer, find the files that are under the metadata directory space for the DATA database. These are the ones that will be copied by the next copy script along with the table space data files.

___14. Review and run command Data04.CMD which will copy database metadata files and DMS table space files using an OS copy command. The above command deactivates the DATA database to allow an OS copy. It copies the DATA database metadata files from C:\DB2 directory to C:\DB2PROD directory. It copies DMS table space data files from C:\DB2 to C:\DB2PROD. After execution of the above command, please open the Windows Explorer. Check the C:\DB2PROD folder for files that this script copied from the C:\DB2 folder.

Page 184

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

___15. Go to the Windows Explorer and refresh. Check for the copied table space containers under C:\DB2PROD directory and the database metadata files under its SQL0000X directory.

___16. After copying the database files, you are now ready to make them be a new database using the db2relocatedb command. ___17. Review and run command Data06.CMD to execute db2relocatedb command and CFG file.

The db2relocatedb command uses configuration file Data07.CFG to read the parameters to move a database DATAPROD into the instance DB2PROD. ___18. Restart your Data Studio session:

Lab 13 Data Movement Utilities

Page 185

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___19. Expand the 50001 instance and connect to database DATAPROD

___20. Expand the database to find the new table RELO_TB and browse the data to see that the utility worked as desired. Find the table spaces and their data files as well.

db2relocatedb also allows you to move or rename data files for a table space in a DB2 database. To do this, you do an OS rename of the data file(s) and run the db2relocatedb command making the source and target database and instance the same.

Page 186

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

B. Converting Non-Unicode Database to Unicode Database


In this lab exercise, we will convert the non-Unicode database DATA (created in previous lab exercise) to a Unicode database using the following tools: db2move db2look to export / import database to generate the necessary DDL files to reproduce the database

This exercise also demonstrates how you can convert one database from one codepage to another from one platform to another by a mass move of all the objects out of one database into another database. ___21. From a DB2 CLP Window, enter the following command: C:\POT_DB2\13DATA\>db2 get db cfg for data | findstr code

Notice the code page and code set for this database which is NOT unicode ___22. Step #1. Review and run Data10.CMD. This script exports the DATA database using the db2move command. It puts the data in a directory called DUMP under your current lab directory:

Lab 13 Data Movement Utilities

Page 187

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___23. Review Data10_OUTPUT.TXT to see how it worked.

___24. Step #2. Review and run Data11.CMD. This generates a DDL script using the db2look command. The breakdown of this command is as follows: db2look -d data -e -o db2look_dump.ddl -l -x -f The -d option indicates the database name to be reengineered The -e option extracts all objects (entire database) The -o option indicates the output file name for the generated DDL script The -l option generates DDL for user defined table spaces, database partition groups and buffer pools, The -x option generates authorization DDL The-f option generates an update command for database configuration parameters The -m option generates statistics statements.

___25. This creates a DDL dump file DB2LOOK_DUMP.ddl. Review it to see the entire database. (note: it is located in the DUMP directory)

Page 188

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

___26. Note: Make sure to change the two CONNECT commands in this file to database DATA2 as shown below. (We we are actually creating a different database called DATA2.)

___27. Step #3. Review and run Data12.CMD. This creates a new target database DATA2 that is in Unicode. This database could actually be on any platform in any codepage as this technique can relocate a database from any platform to any platform.

It then recreates the database structures using the db2look_dump.ddl file. It then imports all the data using the db2move data2 import command.

db2move accepts two loading options, IMPORT and LOAD. If we use the IMPORT option, the table definition can be recreated from IXF files. But in case of the LOAD option, the table must be present in target database. The db2look utility should be used to generate other objects like triggers, UDFs event monitors, any DB2 object, in the target database.

Lab 13 Data Movement Utilities

Page 189

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___28. Check results in database DATA2 in Data Studio. (Hint: you can either add a new connection for it manually or restart Data Studio again.)

___29. Check out the database parameter CODESET to see the database has changed to UTF-8 (Hint: this is in the database configuration parameter listing)

Page 190

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

C. SYSPROC.ADMIN_MOVE_TABLE
___30. Review and run Data20.CMD. This will move a table from one table space to another. This only begins to show the many options of this powerful stored procedure. ___31. Review terminal output message (shown below) as well as file Data20_OUTPUT.TXT

Lab 13 Data Movement Utilities

Page 191

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

C. Data Ingestion
The INGEST utility can be run continuously to pump data into DB2 tables using SQL array inserts, updates, and deletes until data sources are exhausted. Ingest command is restartable from the last commit point. Ingest utility also works for DB2 DPF (Warehouse) and DB2 pureScale (OLTP) environments.

This exercise demonstrates the use of the INGEST utility. ___32. IMPORTANT: Close Data Studio for this exercise. The INGEST utility may need the memory used by Data Studio in your VMWare image, so to avoid a memory problem, close it now. ___33. Review and run Data30.CMD. This script creates an INGEST_SAMPLE table with 10 rows.

___34. Review data file Data31.TXT. This file has 7 rows of data having 3 new rows, updated information for 3 existing rows and indication to delete one row. Action New Update Delete Description HULU, FACEBOOK, LINKEDIN WAL MART, JC PENNY, IBM KODAK

Page 192

DB2 10.1 Administration for the Experienced SQL Server DBAs

Lab 13 Data Movement Utilities

IBM Software

___35. Review script Data33.SQL which has the INGEST command to merge the data.

___36. Review and run script Data32.CMD. This script will run the INGEST command against data file Data31.TXT and merge the data in INGEST_SAMPLE table.

Note: 3 new rows are inserted, 3 rows are updated and 1 row is deleted. We have only shown one capability of the INGEST utility and there are others like continuous data ingestion, reading from pipe etc.

D. Cleanup
Run the following command to clean up DATA database from DB2 instance and drop DB2PROD instance: Data50.BAT *** End of Data Movement Utilities Lab by Vikram Khatri and Burt Vialpando ***

Lab 13 Data Movement Utilities

Page 193

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14. Backup, Restore & Recovery


A. Offline Backup
We will create a database called BKPTEST to demonstrate DB2 core backup, restore and recovery features. ___1. ___2. ___3. ___4. ___5. Close Data Studio (if it is open) Open a DB2 CLP Command Window and go to directory C:\POT_DB2\14BACKUP Review and execute script Backup01.CMD This script creates a database BKPTEST and adds some tables to it so it will have data to work with for this lab. The next thing the script does is quiesce and deactivates that database, which is a key way to taking an offline backup. It then performs the backup as shown below. The output of all commands is stored in Backup01_OUTPUT.TXT file. Please review this file after executing the command.

___6.

Find the backup in the directory C:\DB2Backup. Notice the file name of that backup incorporates the timestamp taken from the backup. This is how DB2 names all its backups so it is impossible to accidentally write over a DB2 backup in the operating system.

Page 194

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

B. Offline Restore
___7. We need the timestamp of the BKPTEST database backup in order to restore it. There are three methods to locate the timestamp of the last backup. The first is to manually write it down from the backup script output. The second is to find the backup file itself and get the timestamp from that. The third way is to review the history file. To do this last way, we use the command db2 list history backup all for bkptest ___8. This command lists all the timestamps of all the previous backups taken on BKPTEST database. Review and execute: Backup02.CMD

___9.

Here is what the history file output looks like:

Lab 14 Backup, Restore & Recovery

Page 195

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___10. You can also use following SQL command to find most recent timestamp from history file for full backup using the view SYSIBMADM.DB_HISTORY db2 connect to bkptest db2 x SELECT max(START_TIME) FROM SYSIBMADM.DB_HISTORY where operation = 'B' and operationtype = 'F'

The value of operation and operation type can be looked from following table:
Operation B Description Backup Operation Type F Offline backup N Online Backup I Incremental offline O Incremental online D Delta offline E Delta online None E End of logs P Point in time F Offline I Incremental offline N Online O Incremental online R Rebuild

D F R

Dropped table Roll forward Restore

___11. Review and run script Backup03.CMD to restore the BKPTEST database from a backup.

Error message SQL2540W, warning 2539 is normal for restoring over an already existing database, which is what we did in this exercise.

Page 196

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

C. Offline Redirected Restore using Data Studio


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database. This is a GUI lab exercise to demonstrate redirected restore in DB2. In this lab exercise, we will change the locations of the two table space containers and modify their sizes. Please follow these steps: ___12. Open Data Studio by clicking on this icon on your desktop or in your task bar:

ORYou can open Data Studio by choosing:


Start All Programs IBM Data Studio Data Studio 3.1.1 Full client

___13. After the Database Administration perspective opens, close the Task Launcher view

___14. In Administration Explorer, under instance DB2, select BKPTEST. Right click and click Connect.

___15. Specify user name dbapot and password dbapot123. Check Save Password. Click OK.

Lab 14 Backup, Restore & Recovery

Page 197

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___16. Right click BKPTEST, select Back Up and Restore Restore

___17. The Restore Database view will open. Select Restore Objects and click to select the backup image.

___18. Select Restore Containers. Click the drop down of Table space with containers to be redirected and select HDATA16.

Page 198

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

___19. Click Add. Enter container path C:\DB2NewLoc\BackupData1601.dat and size as 10000 pages.

___20. Select HIDX16 from the drop down. Click Add. Specify container path C:\DB2NewLoc\BackupIndex1601.dat and size as 5000 pages.

___21. Expand the Command section and review the generated RESTORE command. Click Run.

___22. The redirected restore commands run and you will be able to see the output.

Lab 14 Backup, Restore & Recovery

Page 199

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___23. When this finishes, use the Windows Explorer to review the contents of the DB2NewLoc folder. The two table spaces have been moved from C:\DB2 to C:\DB2NewLoc folder during this redirected restore.

___24. Verify this by looking at the table space containers in the database BKPTEST in Data Studio as well:

Page 200

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

D. Configure Database Logging


To enable archive logging in DB2, we only have to set one database configuration parameter called logarchmeth1. After this parameter is set for the first time, DB2 will make sure we have a full database backup as a base line to work with. So, we will take that backup of the database to make sure we have this base line in order to do future restores to a point-in-time. ___25. Review and run: Backup04.CMD which uses Backup44.DB2. Review the output in file Backup04_OUTPUT.TXT.

___26. We built this lab's logging script that updates the db config parameters using the Configure Database Logging task assistant in Data Studio. We will not be using this task assistant in the lab, but you should know it is available and that it simplifies the first-time logging setup for your databases. To use it, right click on database then Set Up and Configure Configure Database. (We are not going to finish using this GUI here in this lab, but you should know about it.)

Lab 14 Backup, Restore & Recovery

Page 201

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. Online Database Backup


After turning archive logging on, it is now possible to take online backups of the database as well as take individual table space backups. This next script takes an online backup of database BKPTEST that will include all of the table spaces. ___27. Review command Backup05.CMD before running it. This lab script will kick off a secondary script that inserts data into table BACKUPTEST and then loads records into table BKPLOAD before doing the on-line backup. The purpose of this is to demonstrate the online backup being done while work (eg. the INSERTs and the LOAD) is being done on the database. It also shows how recoverable load operations are handled. ___28. Once the above lab exercise is done, please go to the Windows Explorer (refresh if necessary) and review the contents of DB2LoadData and DB2PrimaryLogArch folders. During the load operation, DB2 keeps a copy of the data in DB2LoadData folder so that the subsequent RESTORE operation can use the same data to load the table during recovery process. This is important to note as the LOAD utility does not generate DB2 LOG records but it is possible to use a recoverable load as shown in this lab exercise.

___29. Run: Backup05.CMD. Notice that two different CLP windows are doing work. One is doing the online backup while the other is doing INSERT and LOAD operations.

___30. Notice how LOAD data is kept in the DB2LoadData directory

___31. Close one of the CLP windows in order to continue with the rest of the lab exercises

Page 202

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

F. Database Recover and Restore / Roll Forward Techniques


___32. First, the database RECOVER command is demonstrated. (This is for ease of use.) Review and run: Backup06.CMD

___33. Next, the RESTORE database and ROLLFORWARD commands are demonstrated. (This is for greater control.) Next, review and run: Backup07.CMD

Check the count in BACKUPTEST and BKPLOAD tables and it should be 50,000 rows. The BACKUPTEST table gets 25,000 rows from the LOGS whereas table BKPLOAD gets its data using the RECOVERABLE load operation from C:\DB2LoadData location.

Lab 14 Backup, Restore & Recovery

Page 203

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Extra Exercises G. Recover a Dropped Table


___34. To recover a dropped table, it is necessary that we have an offline/online database/table space level backup taken previously and that the database is in RECOVER mode. These are the 7 steps used to simulate a recovery of a dropped table: 1. Take an online back up of the database to level-set this exercise 2. Create a table after the full database backup and then drop it to simulate an accidental loss 3. Pick up the dropped table ID from the history of the dropped table 4. Restore the table space (containing the table) from the previous backup image 5. Unload the dropped table's data 6. Recreate the table using the DDL from the history command 7. Load the data into the table ___35. Review and run the following command to execute all above steps: Backup08.CMD

H. Restore a History File


Sometimes we may need to restore a history file from a backup. The script Backup09.CMD restores the history file from most recent online backup. The heart of this script is the command: db2 restore database bkptest history file online from c:\db2backup taken at timestamp ___36. Review and run: Backup09.CMD

Page 204

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

I. Moving Table Space Locations with a script (Redirected Restore)


___37. You have already seen an exercise using a GUI to perform a redirected restore to move the table space container locations. The heart of the redirected restore is the RESTORE command followed by SET TABLESPACE command. Then it continues the RESTORE operation. It looks something like this: RESTORE DATABASE TEST FROM "c:\db2backup" TAKEN AT %timestamp% REPLACE HISTORY FILE REDIRECT WITHOUT PROMPTING SET TABLESPACE CONTAINERS FOR 3 IGNORE ROLLFORWARD CONTAINER OPERATIONS USING (FILE "C:\DB2\BackupData1601.dat" 2560, FILE "C:\DB2NEWLOC\BackupData1601.dat" 2560) SET TABLESPACE CONTAINERS FOR 4 IGNORE ROLLFORWARD CONTAINER OPERATIONS USING (FILE "C:\DB2\BackupIndex1601.dat" 625, FILE "C:\DB2NEWLOC\BackupIndex1601.dat" 625) RESTORE DATABASE TEST CONTINUE ROLLFORWARD DATABASE TEST TO END OF LOGS AND COMPLETE ___38. You can also perform a redirected restore using an automatically generated script through RESTORE command. Review and run: Backup10.CMD (the heart of this script is as follows):

___39. When you run the above script, the RESTORE command generates the redirected restore script to a file called redirect.CLP. Find this file and right click on it. Choose: Edit

Lab 14 Backup, Restore & Recovery

Page 205

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___40. Modify the table space container names for HDATA16 and HIDX16 as shown below. (Note: there are TWO changes you need to make!)

___41. After modifying the generated script to change container names, run this script as follows: db2 tvf redirect.clp (Please disconnect the Data Studio connection if it is open. You may get Database is in use error if applications are connected with it. The redirected restore can be performed only in offline mode with the above command.) ___42. The output from above script is kept is BKPTEST_NODE0000.out. Please verify this file and if it was executed correctly, you will see the new table space container names. Use the Windows Explorer and check the new table space containers locations. ___43. After running above script, you cannot connect to the database as it is in roll forward pending mode. Run the roll forward command as shown below. db2 rollforward db bkptest to end of logs and complete

Page 206

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 14 Backup, Restore & Recovery

IBM Software

J. Online Table Space Backup / Restore / Recovery


___44. To demonstrate online backup/restore and recovery of table spaces, we will follow these steps. 1. Insert 1000 rows in already created BACKUPTEST table. 2. Take online backup of these two table spaces. 3. Insert another 1000 rows in the BACKUPTEST table. 4. Assume that we need to recover these two table spaces due to disk failure. Do an Online Restore of these two table spaces. After the online restore, you will get an SQL0290N error if you try to access the table space. 5. Roll forward the transactions to apply changes from logs. 6. Check the count of rows and it should be 2000 rows in BACKUPTEST table. ___45. Run the following command to perform the online backup and restore of the above two table spaces. Run: Backup11.CMD

K. Incremental Backup / Restore / Recovery


___46. We will use the following steps to demonstrate incremental backup and restore: 1. Update TRACKMOD database parameter to enable incremental backup 2. Insert 1000 rows in the BACKUPTEST table. 3. Perform online backup of BKPTEST database. 4. Insert another 1000 rows in the table 5. Perform 1st incremental backup on BKPTEST database. 6. Insert another 1000 rows in the table 7. Perform 2nd incremental backup on BKPTEST database 8. Insert another 1000 rows in the table 9. Assume that the disk containing HDATA16 and HIDX16 has failed. Restore backup of BKPTEST database 10. Apply incremental backups 11. Roll forward the transactions 12. We should see 4000 rows in the table after recovery ___47. Run the following command to demonstrate online incremental backup and restore: Backup12.CMD

Lab 14 Backup, Restore & Recovery

Page 207

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

L. Delta Backup / Restore / Recovery


___48. We will use the following steps to demonstrate delta backup and restore: 1. Update TRACKMOD database parameter to enable incremental backup 2. Insert 1000 rows in the BACKUPTEST table. 3. Perform online backup of BKPTEST database. 4. Insert another 1000 rows in the table 5. Perform 1st delta backup on BKPTEST database. 6. Insert another 1000 rows in the table 7. Perform 2nd delta backup on BKPTEST database 8. Insert another 1000 rows in the table 9. Assume that the disk containing HDATA16 and HIDX16 has failed. Restore backup of BKPTEST database 10. Apply delta backups 11. Roll forward the transactions. 12. We should see 4000 rows in the table after recovery ___49. Run the following command to demonstrate online incremental backup/restore and recovery. Run: Backup13.CMD ___50. Close Data Studio.

N. Cleanup
After you are done with the above exercises, you can run the following command to clean up resources. Run: Backup60.BAT

*** End of Backup, Restore and Recovery Lab by Vikram Khatri and Burt Vialpando ***

Page 208

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

Lab 15. Monitoring


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Snapshot Monitoring
The snapshot monitoring requires turning on switches either at an instance level or at a session level. The following lab exercise shows how to turn on and off monitoring switches. ___1. Open a DB2 CLP Command Window and change your directory to C:\POT_DB2\15MONITOR

___2.

Review and run Monitor01.CMD. Review output in file Monitor01_OUTPUT.TXT to see the monitor switches set at the instance and session levels. Notice that the global monitoring switches are set using UPDATE DBM command at the DB2 Instance level and session specific monitoring switches is set using UPDATE MONITOR command.

The snapshot monitor is a classic monitoring approach accomplished by the use of the GET SNAPSHOT CLP command. ___3. Review and run the script: Monitor02.CMD to see the output from the GET SNAPSHOT command. Review output in file Monitor02_OUTPUT.TXT.

The snapshot monitoring is the technology of yesteryear. While it is going to stay around, the current preference is to use light weigh monitoring functions and the db2pd command line tool which we will demonstrate later in this lab.

Lab 15 Monitoring

Page 209

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

B. Event Monitoring
An event monitor differs from other monitor types due to the fact that information is collected at the time when that event is detected or completes. The following lab exercise is an example of event monitoring where we will capture elapsed time used by the SQL statements inside a stored procedure. ___4. Review and run the command: Monitor03.CMD which runs Monitor04.SQL that has DDL to create a table MONITOR_TAB and a stored procedure MONITOR_SP to insert and update data in that table. Review Monitor03_OUTPUT.TXT to see the results of running the stored procedure.

___5.

Review and run the command: Monitor05.CMD which runs Monitor06.SQL that creates an event monitor POT_EVEMON and a stored procedure MONITOR_RS which we will use to see the elapsed time for each SQL statements used inside MONITOR_SP procedure. The db2evtbl command line tool can help you generate event monitor command scripts to store data in target DB2 tables. In the DB2 install path, under the folder MISC, there is an example script for building DB2 Workload Manager event monitors called: wlmevmon.ddl Notice that this script turns on the statement event monitor POT_EVEMON and then runs the procedure MONITOR_SP. The statement event monitor captures the data from this procedure and populates three event monitor tables: POT_EVEMON_HEAD, POT_EVEMON_STMT and POT_EVEMON_CTRL

Now we will run the MONITOR_RS procedure in Data Studio to see the elapsed time for each SQL statement that was inside the procedure MONITOR_SP. ___6. Open the IBM Data Studio by clicking on this icon on your desktop or in your task bar:

ORYou can open Data Studio by choosing:


Start All Programs IBM Data Studio Data Studio 3.1.1 Full client

Page 210

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

___7.

After the perspective opens, close the Task Launcher view

___8.

In the Database Administration perspective, find the Administration Explorer and expand localhost DB2 and select SAMPLE. Right click and click Connect. Navigate to schema DBAPOT and expand node Stored Procedures.

___9.

___10. You should see MONITOR_SP and MONITOR_RS stored procedures. (If not, refresh this view so they will appear.) We will run MONITOR_RS procedure to pull information from event monitor tables in which we captured data from the MONITOR_SP procedure.

___11. Right click on stored procedure MONITOR_RS and open click on Run.

Lab 15 Monitoring

Page 211

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___12. In the dialog box, specify MONITOR_SP as the argument to the procedure so that it will pull the information from event tables for each SQL statement. Click Run to continue

___13. You will see results from the stored procedure in SQL Results window as shown below. Double click on SQL Results to maximize the screen.

___14.

Click Result1 tab. Expand the TEXT column width to see the statements text and review results:

Page 212

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

C. Data Studio Stored Procedure Profiling


Data Studio can also be used to gather performance data for a stored procedure. ___15. From the Data Studio, right click on the MONITOR_SP stored procedure. Choose: Run.

___16. Check Gather performance information from the database. Click Run.

___17. When the run is completed, double click SQL Results tab.

Lab 15 Monitoring

Page 213

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___18. Click tab Profile Data. Adjust the width of the SQL column. Double click User CPU Time to sort in the descending order.

This view shows the Stored Procedure body and you have options to sort in different fashion to arrive at the SQL which are expensive and needs attention.

Page 214

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

D. Light Weight Monitoring SQL functions and db2pd


There are two kinds of light weight monitoring in DB2 that is free from latch contention and does not require use of the monitoring switches. The first type of monitoring like this is accomplished using SQL based table functions. The second is using the tool: db2pd ___19. Review and run script Monitor07.CMD, which calls Monitor08.SQL ___20. Notice that the script loops and does three things in each loop: Runs two SQL table function queries Runs db2pd with the -LOGS switch Runs the stored procedure MONITOR_SP to do work in the database so the results will change on the next loop The use of these SQL table functions shown in this exercise are only 2 of 48 currently available to you in DB2.

___21. Review the output in Monitor07_OUTPUT.TXT file. Notice that we did monitoring of MONITOR_TAB table by running a work load and monitoring the table, table space containers and logs using these lightweight monitoring techniques.

db2pd additional functionality


Unlike the other monitoring tools, db2pd can also be used when the database is not available. This tools peeks inside the instance and database memory to give you unprecedented power and flexibility. ___22. To see more on this tool, open a Windows Command Prompt or a DB2 CLP Command Window. Type: db2pd osinfo db2pd -interactive edus -mempools -quit db2pd h file=help_OUTPUT.TXT NOTEPAD help_OUTPUT.TXT

Lab 15 Monitoring

Page 215

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

E. IBM Data Studio Web Console


___23. Review and run the command: Monitor09.CMD. This script will create DSWC DB2 instance and DSWC database which we will use as a repository to monitor SAMPLE and GSDB databases. ___24. Click on Start Web Console Server, then open the Data Studio Web Console icon and log in with user db2admin, password db2admin

If your web console is not starting correctly, use URL http://127.0.0.1:11086

___25. From the Getting Started section, click Select a repository database.

___26. Click Select Repository Database.

Page 216

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

___27. Specify database DSWC, host name localhost, port number 50010, user id dbapot and password dbapot123. Click Test Connection and if it succeeds, click OK.

By following the above steps, we have chosen DSWC as repository for the Data Studio Web Console. Now we will add databases to do the health monitoring. ___28. Click x to close Configuration Repository.

___29. Click Add database connections.

Lab 15 Monitoring

Page 217

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___30. Click Add.

___31. Specify database connection name SAMPLE, database name SAMPLE, host name localhost, port number 50000, user id dbapot, password dbapot123. Click Test Connection and if it succeeds, click OK.

___32. Click x to close Databases tab.

Page 218

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

___33. Click View health summary.

___34. Click down arrow to change the refresh interval.

___35. Change both the interval to 1 minute each. Click OK.

___36. The Health Summary view shows the health related alerts encountered for the monitored database.

___37. Click on the down arrow icon next for SAMPLE, select Current Application Connections.

Lab 15 Monitoring

Page 219

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___38. Review the current application connections in the view.

Note: The HEALTH_MON database manager parameter must be turned on for the health related alerts to show up in the Health Summary view. So, if nothing is showing up do the following: db2 get dbm cfg | findstr HEALTH db2 update dbm cfg using HEALTH_MON ON db2stop force db2start Now connect to SAMPLE again through Data Studio

___39. Close x Current Application Connections view.

___40. Disconnect from the SAMPLE database

Page 220

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 15 Monitoring

IBM Software

___41. Reconnect using user dbapot password dbapot123

___42. Click Open Current Table Spaces.

___43. The table spaces used by the monitored database are shown. Explore the containers too.

E. Cleanup
Please run the following script to clean up objects that we created in this lab: Monitor50.BAT *** End of Monitoring Lab by Vikram Khatri and Burt Vialpando ***

Lab 15 Monitoring

Page 221

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 16. Additional Misc. Topics


This lab assumes you have already completed the Data Studio Lab 03 exercises. If you have not, then you should complete sections A and B of Lab 03 to learn how to launch Data Studio and connect to a database.

A. Temporal Data Management & Time Travel Query


Temporal Data Management ___1. ___2. Open a DB2 CLP Command Window and change to directory C:\POT_DB2\16MISC Review and run script Temporal01.cmd. This will set up the POLICY_INFO and POLICY_INFO_HIST tables that will support temporal data management. Review Temporal01_OUTPUT.TXT. In Data Studio connect to the SAMPLE database and review the tables to see the versioning information. (Note: we could have built our temporal tables in Data Studio without the script.)

___3.

___4.

Run script Temporal03.cmd to see the data in the tables. At first, the tables are empty.

Page 222

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 16 Additional Misc. Topics

IBM Software

___5. ___6.

Find the script file Temporal04.DML and bring it into a SQL Script Editor session in Data Studio. (Hint: New SQL Script) Highlight and run each DML statement one at a time.

___7.

After each DML execution in the SQL Script Editor, run script Temporal03.CMD to see what the tables look like:

___8.

After you finish the INSERTS, find script Temporal05.DML and do the same, running the three UPDATES, one at a time and reviewing the output. Notice on the UPDATE, a new history record is generated.

Lab 16 Additional Misc. Topics

Page 223

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___9.

Finally, run the last DELETE DML operation and review the output, which should look like this:

___10. Study how the start and end times relate to each other. For example, the last history record for POLICY_ID 'A001' ends when the current record for it starts. Time Travel Query ___11. Copy and paste script file Temporal06.SQL into the SQL Script Editor. ___12. Review your own output from the Temporal03.CMD file in your CLP (shown above) to notice the timestamps of your data. Your examples will be run with later and different timestamps than the examples shown here in this lab document, so you have to adjust your timestamps in the query in order to get them to work.

___13. Examples of how this output looks when run with appropriate dates is shown below:

Page 224

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 16 Additional Misc. Topics

IBM Software

B. Federation simple DB2 to DB2 example


___14. Open a DB2 CLP Command Window and change to directory C:\POT_DB2\16MISC ___15. If you have NOT done so already, create a SAMPLE2 database by using the following CLP command: db2 create database sample2 (we will need this database to federate from) ___16. Review script Fed01.CMD to see it has 5 steps: 1) Enable federation, 2) create a wrapper, 3) create a server, 4) create a mapping and 5) create a nickname. ___17. Run script Fed01.CMD

___18. At end of the script, notice it runs a query using the nickname EMP_NICK which is in the SAMPLE2 database federated from the table EMP in the SAMPLE database

Federation is enabled ___19. In Data Studio connect to database SAMPLE2. ___20. In the Database Administration perspective, right click on Instance DB2 and choose: Configure

Lab 16 Additional Misc. Topics

Page 225

IBM Software

DB2 10.1 Administration for the Experienced SQL Server DBA

___21. Find the section called Environment, then the parameter FEDERATED and notice the script has changed this to YES. This allows for federation to be done in any database in this instance.

___22. Close this parameter edit session when done reviewing. Review federated objects ___23. From Data Studio, go to the Data perspective. ___24. In the Data Source Explorer, find and expand the entire tree for Federated Objects under SAMPLE2 database

___25. First notice the Wrappers and the one called DRDA This wrapper library is found in the BIN subdirectory of your DB2 install path, so on Windows by default this should be in C:\Program Files\IBM\SQLLIB\BIN. If we had other wrapper DLL for Oracle or SQL Server, this is where we would put them to create a wrapper. ___26. Next notice the Defined Remote Servers. We have defined one called SAMPLE for the SAMPLE database ___27. Next notice the User Mappings. We have defined one called DBAPOT for server SAMPLE.

Page 226

DB2 10.1 Administration for the Experienced SQL Server DBA

Lab 16 Additional Misc. Topics

IBM Software

___28. Finally, expand EMP_NICK under Nicknames and explore all the attributes of this object.

C. Cleanup
After you are done with the above exercises, you can run the following command to clean up the resources: Misc50.BAT *** End of Misc Additional Topics Lab by Burt Vialpando ***

Lab 16 Additional Misc. Topics

Page 227

IBM Software

Appendix A. Notices
This information was developed for products and services offered in the U.S.A. IBM may not offer the products, services, or features discussed in this document in other countries. Consult your local IBM representative for information on the products and services currently available in your area. Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM product, program, or service may be used. Any functionally equivalent product, program, or service that does not infringe any IBM intellectual property right may be used instead. However, it is the user's responsibility to evaluate and verify the operation of any non-IBM product, program, or service. IBM may have patents or pending patent applications covering subject matter described in this document. The furnishing of this document does not grant you any license to these patents. You can send license inquiries, in writing, to: IBM Director of Licensing IBM Corporation North Castle Drive Armonk, NY 10504-1785 U.S.A. For license inquiries regarding double-byte (DBCS) information, contact the IBM Intellectual Property Department in your country or send inquiries, in writing, to: IBM World Trade Asia Corporation Licensing 2-31 Roppongi 3-chome, Minato-ku Tokyo 106-0032, Japan The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or implied warranties in certain transactions, therefore, this statement may not apply to you. This information could include technical inaccuracies or typographical errors. Changes are periodically made to the information herein; these changes will be incorporated in new editions of the publication. IBM may make improvements and/or changes in the product(s) and/or the program(s) described in this publication at any time without notice. Any references in this information to non-IBM Web sites are provided for convenience only and do not in any manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the materials for this IBM product and use of those Web sites is at your own risk. IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you. Any performance data contained herein was determined in a controlled environment. Therefore, the results obtained in other operating environments may vary significantly. Some measurements may have been made on development-level systems and there is no guarantee that these measurements will be the same on generally available systems. Furthermore, some measurements may have been estimated through extrapolation. Actual results may vary. Users of this document should verify the applicable data for their specific environment.

Page 228

DB2 10.1 Administration for the Experienced SQL Server DBA

IBM Software

Information concerning non-IBM products was obtained from the suppliers of those products, their published announcements or other publicly available sources. IBM has not tested those products and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products. All statements regarding IBM's future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives only.

Appendix

Page 229

IBM Software

Appendix B. Trademarks and copyrights


The following terms are trademarks of International Business Machines Corporation in the United States, other countries, or both:
IBM Lotus IBM logo pureXML 1-2-3 Sametime AIX DB2 IMS

Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others.

Page 230

DB2 10.1 Administration for the Experienced SQL Server DBA

IBM Software

OPTIONAL TOPICS VOTING SHEET


Indicate which topics you prefer to cover with the optional time in the PoT session

Name: (optional)

_____________________________________________________________

Rules and guidelines for voting: You can talk with others on your team about how they want to vote, so you can vote similarly You can only vote once for yourself You can refrain from voting if you want to TOPIC or LAB DB2 Compression DB2 Compression LAB Explain Facilities Explain Facilities LAB DB2 Workload Manager (WLM) DB2 Workload Manager (WLM) LAB DB2 HADR DB2 HADR LAB DB2 pureXML DB2 pureXML LAB DB2 Multidimensional Clusters DB2 Multidimensional Clusters LAB DB2 Data Movement DB2 Data Movement LAB DB2 Backup, Restore & Recovery DB2 Backup, Restore & Recovery LAB DB2 Monitoring DB2 Monitoring LAB Misc Additional Topics & LABs
* Ranking

Time to complete 20 minutes 20 minutes 60 minutes 45 minutes 60 minutes 40 minutes 40 minutes 30 minutes 30 minutes 30 minutes 30 minutes 30 minutes 60 minutes 30 minutes 45 minutes 75 minutes 30 minutes 30 minutes 40+ minutes

Ranking *

Give 10 to your highest, 9 to your next highest and so on down to your lowest with a ranking of 1 If you want to do a lab for a topic, rank the lab for a topic the same as the topic The time to complete is listed to remind you of how much time each topic usually takes We will tally up the rankings of each, and the ones with the highest go first, and so on, until we run out of time for our PoT session

Optional Topics Voting Sheet

Copyright IBM Corporation 2012. The information contained in these materials is provided for informational purposes only, and is provided AS IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, these materials. Nothing contained in these materials is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software. References in these materials to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates. This information is based on current IBM product plans and strategy, which are subject to change by IBM without notice. Product release dates and/or capabilities referenced in these materials may change at any time at IBMs sole discretion based on market opportunities or other factors, and are not intended to be a commitment to future product or feature availability in any way. IBM, the IBM logo and ibm.com are trademarks of International Business Machines Corp., registered in many jurisdictions worldwide. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at Copyright and trademark information at www.ibm.com/legal/copytrade.shtml.

S-ar putea să vă placă și