db2udb_the_basics
TRANSCRIPT
IBM Software Group
© 2005 IBM Corporation
DB2 UDB Fundamentals
Day1
IBM Software Group
© 2005 IBM Corporation
DB2 Overview Instance Database DB2 Architecture
2
IBM Software Group
© 2005 IBM Corporation3
DB2 Overview
DB2 is a database product from IBM. Initially DB2 product is developed for Mainframe ,
Since 1990’s It is decided to develop a UDB which can run on any Platform(Unix, Linux, Windows)
Latest version of db2 is 10.5 (added feature of BLU acceleration).
3.4 (Code web), 8.1,8.2 (Stinger) , 9.1 (Viper), 9.5 (Viper2) , 9.7 (Cobra), 9.8 (added feature with pure scale) , 10.1 (Galileo), 10.5 (Capler)
IBM Software Group
© 2005 IBM Corporation4
Products
DB2 Personal Edition (Remote feature NA)
DB2 Workgroup Server Edition
DB2 Enterprise Server Edition
DB2 Workgroup Server Unlimited Edition
DB2 UDB Express Edition(lowest price fully functional)
IBM Software Group
© 2005 IBM Corporation
DB2 Installation
Download the software from IBM site. gunzip v9.7fp10_linuxia32_server.tar.gz tar -xvf v9.7fp10_linuxia32_server.tar After extraction of file Server folder will be displayed. In server folder , You will find db2_install script U have to invoke that script using ./ DB2 installation will start.
5
IBM Software Group
© 2005 IBM Corporation
Installation Process
6
IBM Software Group
© 2005 IBM Corporation
Creation of Group and User
This is very important concept as We can not directly use the DB2 commands after installation of DB2.
We have to create a group and allocate it to users. groupadd –g 999 db2iadm1, groupadd –g 998 db2fadm1, groupadd
–g 997 dasadm1 useradd -g db2iadm -m -d /home/db2inst1 db2inst1 , useradd -g
db2fadm -m -d /home/db2fenc1 db2fenc1 .
7
IBM Software Group
© 2005 IBM Corporation8
Instance Manages one or more databases Must have root or Administrator Authority to create db2icrt is used to create an instance db2idrop is used to drop an instance db2iupdt is used to update the instance
db2icrt -u fencedusr instance_name
db2start will start an instance
db2stop will stop an instance
./db2icrt –u db2fenc1 db2inst1
IBM Software Group
© 2005 IBM Corporation
Configuration Parameters
When a DB2 database instance or a database is created, a corresponding configuration file is created with default parameter values.
There are two types of configuration files:
The database manager configuration file for each DB2 instance (dbm cfg)
The database configuration file for each individual database.(db cfg)
9
IBM Software Group
© 2005 IBM Corporation10
IBM Software Group
© 2005 IBM Corporation
Instance Level Parameter
db2 get dbm cfg db2 update dbm cfg using parm db2 get dbm cfg | grep –i SVCENAME db2 get dbm cfg > dbm_instance.cfg db2 get dbm cfg | grep -i agent (AGENT_STACK_SZ), (MAX_CONNECTIONS), (CPUSPEED) = 3.975564e-07,
11
IBM Software Group
© 2005 IBM Corporation
Instance Level Parameters
db2 get dbm cfg | grep –i diag
This parameter specifies the type of diagnostic errors that will be recorded in the db2diag.log file.
Diagnostic data directory path (DIAGPATH) = /home/db2inst2/sqllib/db2dump/
Diagnostic error capture level (DIAGLEVEL) = 3
db2 get dbm cfg | grep –i notify
This parameter specifies the type of administration notification messages that are written to the administration notification log.
• Notify Level (NOTIFYLEVEL) = 3
12
IBM Software Group
© 2005 IBM Corporation
Valid Values of Diag level
0 – Only critical errors, event messages, and administration notification messages are captured on the server side. You cannot set the value of this parameter to 0 on the client side.
1 – Only severe errors, critical errors, event messages, and administration notification messages are captured.
2 – All errors, event messages, and administration notification messages are captured.
3 – All errors, warnings, event messages, and administration notification messages are captured.
4 – All errors, warnings, informational messages, event messages, and administration notification messages are captured.
13
IBM Software Group
© 2005 IBM Corporation
Valid Values of Notify Level
Valid values for this parameter are:0 - No administration notification messages captured. (This setting is not recommended.)
1 - Fatal or unrecoverable errors. Only fatal and unrecoverable errors are logged. To recover from some of these conditions, you might need assistance from DB2 service.
2 - Immediate action required. Conditions are logged that require immediate attention from the system administrator or the database administrator. If the condition is not resolved, it could lead to a fatal error. Notification of very significant, non-error activities (for example, recovery) might also be logged at this level. This level will capture Health Monitor alarms. Informational messages will be shown at this level.
3 - Important information, no immediate action required. Conditions are logged that are non-threatening and do not require immediate action but might indicate a non-optimal system. This level will capture Health Monitor alarms, Health Monitor warnings, and Health Monitor attentions.
4 - Informational messages.
14
IBM Software Group
© 2005 IBM Corporation15
Creating the Database
Create db database-name Creates three table spaces Creates System Catalog Tables There are two types of database
(i) Local (ii) Remote
Local Database : db2 create db Sales Remote Database : db2 catalog tcpip node mynode1 remote
aries.myacme.com server 50000 db2 catalog db rmtdb as myrmtdb at node mynode1
IBM Software Group
© 2005 IBM Corporation
Local Database and Remote Database
If entry type = Indirect ie Local Database
If entry type = Remote i.e Remote Database
Db2 list db directory
Database alias = MYRMTDB
Database name = RMTDB
Node name = NODE1
Database release level = d.00
Comment =
Directory entry type = Remote
Catalog database partition number = -1
16
IBM Software Group
© 2005 IBM Corporation17
Create Database Command
db2 get db cfg db2 update db cfg using parm db2 get db cfg | grep –i reorg db2 get db cfg > db_instance.cfg db2 get db cfg | grep -i log
db2 connect to Localdb[db2inst2@localhost ~]$ db2 connect to PRANAV
Database Connection Information
Database server = DB2/LINUX 9.7.10
SQL authorization ID = DB2INST2
Local database alias = PRANAV
IBM Software Group
© 2005 IBM Corporation
Db2 get db cfg example
Db2 get db cfg | grep –i auto
Automatic maintenance (AUTO_MAINT) = ON
Automatic database backup (AUTO_DB_BACKUP) = OFF
Automatic table maintenance (AUTO_TBL_MAINT) = ON
Automatic runstats (AUTO_RUNSTATS) = ON
Automatic statement statistics (AUTO_STMT_STATS) = ON
Automatic statistics profiling (AUTO_STATS_PROF) = OFF
Automatic profile updates (AUTO_PROF_UPD) = OFF
Automatic reorganization (AUTO_REORG) = OFF
18
IBM Software Group
© 2005 IBM Corporation
DB2 Architecture
19
IBM Software Group
© 2005 IBM Corporation
System Database Directory
the system database directory is like a table of contents: it shows you all the databases you can connect to from your system. The system database directory is stored in a binary file with name SQLDBDIR and is in the following location:
• DB2_instance_home/sqllib/sqldbdir on Linux and UNIX systems.
db2 list db directory
Type = Indirect , Type = Remote
20
IBM Software Group
© 2005 IBM Corporation
Node Directory
The node directory stores information about how to communicate to a remote instance where a given database resides. It is stored in a file called SQLNODIR and is in:
DB2_instance_home/sqllib/sqlnodir
21
IBM Software Group
© 2005 IBM Corporation
Data Connection Services
The DCS directory is required only when connecting to a host server like DB2 for OS/390, z/OS, and i5/OS.
Db2 list dcs directory
22
IBM Software Group
© 2005 IBM Corporation23
Table SpaceLogical layer between Hardware and Database
Comprised of one or more containers
A container is a file or a directory
REGULAR
CREATE TABLESPACE name
LARGE
SYSTEM
TEMPORARY
USER
MANAGED BY SYSTEM system-containers
DATABASE database-containers
IBM Software Group
© 2005 IBM Corporation24
System ContainersSMS Containers
USING (‘container string’)
DMS Containers
USING (FILE ‘container string’ number of pages)
(DEVICE ‘container string’ number of pages)
IBM Software Group
© 2005 IBM Corporation25
Table Space Example
CREATE TABLESPACE TS1 MANAGED BY SYSTEM
USING (‘/home/inst01/database/ts1’)
CREATE TABLESPACE DMS01D MANAGED BY DATABASE
USING (FILE ‘C:\DMS\DATABASE\DMS01D’ 1000)
IBM Software Group
© 2005 IBM Corporation26
Create Tables
Command Line
db2 create table ARTISTS (
ARTNO SMALLINT NOT NULL,
NAME VARCHAR(40),
COMPANY CHAR(20), BIO CLOB (10K) NOT LOGGED,
PICTURE BLOB (2700K) NOT LOGGED )
IN DMS01D INDEX IN DMS01I LONG IN DMS01L
IBM Software Group
© 2005 IBM Corporation27
Create Table Script File
create table warehouse (itemno smallint, warehousename char(20), qty integer ) in dms03d;
The script file cr_warehouse contains the above info
db2 –tvf cr_warehouse
IBM Software Group
© 2005 IBM Corporation28
Table CharacteristicsDescribe command
db2 describe table ARTISTSColumn Type Typename schema name Length Scale Nulls--------------- --------- ------------------ -------- ----- ------ARTNO SYSIBM SMALLINT 2 0 NoNAME SYSIBM VARCHAR 40 0 YesCOMPANY SYSIBM CHARACTER 20 0 YesBIO SYSIBM CLOB 20480 0 YesPICTURE SYSIBM BLOB 2764800 0 Yes
IBM Software Group
© 2005 IBM Corporation29
Describe Indexes
IBM Software Group
© 2005 IBM Corporation
Moving Data
IBM Software Group
© 2005 IBM Corporation31
Moving Data
IMPORT EXPORT LOAD db2move
IBM Software Group
© 2005 IBM Corporation32
Import / Export
FileImport
Export
IBM Software Group
© 2005 IBM Corporation33
IMPORT utility
IMPORT FROM filename OF IXF
DEL
ASC
LOBS FROM lob-path MODIFIED BY options
MESSAGES
INSERT INTO table-name
INSERT_UPDATE
REPLACE
REPLACE_CREATE
IBM Software Group
© 2005 IBM Corporation34
Export
EXPORT TO file OF IXF MESSAGES message-file
DEL
WSF
select statement
IBM Software Group
© 2005 IBM Corporation35
LOAD
1) Load Loads data, collects index keys
2) Build creates the indexes
3) Delete Delete unique key violations place into exception tables.
4) Index Copy – copy indexes from temp table space
IBM Software Group
© 2005 IBM Corporation36
LOAD Command
LOAD FROM filename OF IXF
ASC
DEL
LOBS FROM lob-path MODIFIED BY options
MESSAGES message-file
INSERT INTO table-name
REPLACE
RESTART
TERMINATE
IBM Software Group
© 2005 IBM Corporation37
LOAD from Cursor
Create nickname sales for
another database SAMPLE table SALES
Create nickname employee for
another database SAMPLE table EMPLOYEE
DECLARE C1 CURSOR FOR SELECT SALES.SALES_PERSON, LASTNAME, FIRSTNME FROM SALES, EMPLOYEE
WHERE SALES_PERSON = EMPLOYEE.LASTNAME
LOAD FROM C1 OF CURSOR INSERT INTO LOCAL_SALES
IBM Software Group
© 2005 IBM Corporation38
db2move
db2move
Database
db2move.lst
table.ixf
DB2MOVE
IBM Software Group
© 2005 IBM Corporation39
db2move syntax
db2move database-name import
export
load
tc table-creators
tn table-name
sn schema-names
ts table space-names
IBM Software Group
© 2005 IBM Corporation
GUI Tools Using Graphical User Interface
IBM Software Group
© 2005 IBM Corporation41
Using the Control Center
IBM Software Group
© 2005 IBM Corporation42
Control Center – Create Database
IBM Software Group
© 2005 IBM Corporation43
Examples of Table Space
CREATE TABLESPACE TS1 MANAGED BY SYSTEM
USING (‘C:\SMS\MUSICKEG\TS1’)
CREATE TABLESPACE DMS01D MANAGED BY DATABASE
USING (FILE ‘C:\DMS\MUSICKEG\DMS01D’ 161)
EXTENTSIZE 8 PREFETECHSIZE 8
CREATE TABLESPACE DMS01I MANAGED BY DATABASE
USING (FILE ‘C:\DMS\MUSICKEG\DMS01I’ 48)
EXTENTSIZE 4 PREFETCHSIZE 4
IBM Software Group
© 2005 IBM Corporation44
Table Space Using GUI
IBM Software Group
© 2005 IBM Corporation45
Table Space Using GUI (2)
IBM Software Group
© 2005 IBM Corporation46
Table Space Using GUI (3)
IBM Software Group
© 2005 IBM Corporation47
Table Space Using GUI (4)
IBM Software Group
© 2005 IBM Corporation48
Command Editor
IBM Software Group
© 2005 IBM Corporation49
Visual Explain
IBM Software Group
© 2005 IBM Corporation50
Visual Explain
IBM Software Group
© 2005 IBM Corporation51
Task Center
IBM Software Group
© 2005 IBM Corporation52
Journal
IBM Software Group
© 2005 IBM Corporation
RECOVERY
IBM Software Group
© 2005 IBM Corporation54
RecoveryDefining logs
Recovery of database
Recovery of a table space
Offline versus Online
IBM Software Group
© 2005 IBM Corporation55
Database Recovery LOGRETAIN = NO
Database
S0000000.log
S0000001.log
S00000002.log
S0000003.log (Secondary Log)
S0000004.log (Secondary Log)
IBM Software Group
© 2005 IBM Corporation56
Database Recovery LOGRETAIN = RECOVERY
Database
S0000000.log
S0000001.log
S00000002.log
S0000003.log
S0000004.log
IBM Software Group
© 2005 IBM Corporation57
Backup
If LOGRETAIN = Recovery you may backup table space or database
If LOGRETAIN = NO you may only backup database
BACKUP DB database-name ONLINE to C:\backup
INCLUDE LOGS
IBM Software Group
© 2005 IBM Corporation58
Recovery
If LOGRETAIN = NO, you may only recover the database If LOGRETAIN = RECOVERY, you may recover a table space
or a database from a full database backup
IBM Software Group
© 2005 IBM Corporation59
Modes of Recovery Offline Online
RESTORE DB database-name FROM file TAKEN AT time
ROLLFORWARD DATABASE database-name
TO isotime AND STOP
END OF LOGS
IBM Software Group
© 2005 IBM Corporation60
HADR – High Availability Disaster Recovery
Database_standby
Database1
Laptop computer
IBM Software Group
© 2005 IBM Corporation
Performance and Tuning
IBM Software Group
© 2005 IBM Corporation62
Performance and Tuning
Database Configuration parameters Database Structure SQL Statements
IBM Software Group
© 2005 IBM Corporation63
Database Configuration parameters
IBM Software Group
© 2005 IBM Corporation64
Configuration Parameters
IBM Software Group
© 2005 IBM Corporation65
Buffer Pool
Database
Buffer Pool
Select * from Staff
IBM Software Group
© 2005 IBM Corporation66
Monitoring Buffer Pool Hit Ratio
db2 get snapshot for bufferpools on database-name
Bufferpool name = IBMDEFAULTBP
Database name = MUSICKEG
Database path = C:\DB2\NODE0000\SQL00002\
Input database alias = MUSICKEG
Snapshot timestamp = 05/04/2005 13:11:37.329018
Buffer pool data logical reads = 336
Buffer pool data physical reads = 129
IBM Software Group
© 2005 IBM Corporation67
Sort Heap
Sorts are done in sortheap If no space for sort data is moved to TEMPSPACEn GET SNAPSHOT FOR ALL ON database
IBM Software Group
© 2005 IBM Corporation68
Package Cache
Dynamic SQL statements
Package Cache
Select * from Staff where ID = 10Select * from Staff where ID = 10
Update Staff Set Salary = Salary + 100 where ID = 10
Select * from EMPLOYEE
IBM Software Group
© 2005 IBM Corporation69
LOCK
Locks are held to prevent loss of data Lock Row / Table / Table Space LOCKLIST MAXLOCKS ALTER TABLE table-name LOCKSIZE TABLE
IBM Software Group
© 2005 IBM Corporation70
Show Locks
IBM Software Group
© 2005 IBM Corporation71
SQL Statements
Determine which statement is causing the majority of problems
Determine what might be causing the problem Testing the solution
IBM Software Group
© 2005 IBM Corporation72
SQL Activity Monitor
IBM Software Group
© 2005 IBM Corporation73
Visual Explain
IBM Software Group
© 2005 IBM Corporation74
Table Statistics
IBM Software Group
© 2005 IBM Corporation75
Performance Summary
Buffer pools Numerous Database Configuration parameters SQL Statement Tuning
IBM Software Group
© 2005 IBM Corporation
Security
IBM Software Group
© 2005 IBM Corporation77
Security
Security is used at the operating system level Table access is through the database
IBM Software Group
© 2005 IBM Corporation78
Instance Level
SYSADM_GROUP SYSCTRL_GROUP SYSMAINT_GROUP SYSMON_GROUP
IBM Software Group
© 2005 IBM Corporation79
GRANT / REVOKE
GRANT access to an object/program REVOKE access to an object/program GRANT SELECT ON TABLE ARTISTS TO USER1
IBM Software Group
© 2005 IBM Corporation80
Summary
The Relational Database can be simple or complex The database structure is simple, Table spaces, Tables, etc. Recovery is straight forward Database maintenance can be automated Tuning the database is a life long endeavor
IBM Software Group
© 2005 IBM Corporation81
DB2 UDB Proof of Technology
July 12, 13 DB2 UDB Administration Proof of Technology IBM – McClean Tec 8401 Greensboro Drive McClean, VA 22102 Suite 120 First Floor WebSphere Information Integrator July 14, 2005 Contact: Keith E. Gardenhire [email protected]
IBM Software Group
© 2005 IBM Corporation82
DB2 UDB Proof of Technology Con’t