Post on 01-Jan-2016
Bina Nusantara University 3
• There are two general forms of scripting:– SQL scripts– Operating system (shell) scripts
• SQL Scripts– Simple to understand– Platform independent– Script parameters not supported
• Operating system scripts– Greater flexibility– Additional logic possibilities– Supports parameters/variables– Platform-dependent
Scripting
Bina Nusantara University 4
• Suppose the following commands are saved in a file called script1.db2
CONNECT TO EXPRESS;
CREATE TABLE user1.mytable
( col1 INTEGER NOT NULL,
col2 VARCHAR(40),
col3 DECIMAL(9,2));
SELECT * FROM user1.mytable FETCH FIRST 10 ROWS ONLY;
COMMIT;
A Basic SQL Script
Bina Nusantara University 5
• An SQL script can be executed from the Command Editor or the DB2 Command Window/Linux shell
• To run the previous script from the command line (DB2 Command Window), you would use the following command:db2 –t –v –f script1.db2 –z script1.log
-t indicates statements use the default statement termination character (semicolon)
-v indicates verbose mode; causes db2 to output the command being executed
-f indicates the following filename contains the SQL statements
-z indicates the following message filename should be used for appending screen output for later analysis (optional, but recommended)
Note: It is a good idea to delete these message files before the execution of DB2 scripts so that output from a previous script execution is not mixed with output from the current script execution
Executing SQL Scripts
Bina Nusantara University 6
When a different statement termination character is needed
•A script containing SQL PL code needs to use a different statement termination character other than the default (semicolon)
•This is due to the fact that semicolons are used within SQL PL object definitions to terminate procedural statements
Delimit the end of each SQL PL application object with a different statement termination character
Bina Nusantara University 7
When a different statement termination character is needed
– e.g. CREATE PROCEDURE P1() declare x int;
….END!CREATE PROCEDURE P2()…END!
• When executing the script, inform DB2 that a different statement termination character is being used:
db2 –td! –v –f procs.db2 –z procs.log
Bina Nusantara University 8
• Suppose the following statements are saved in a file called create_database.batset DBPATH=c:set DBNAME=PRODEXPRset MEMORY=25db2 CREATE DATABASE %DBNAME% ON %DBPATH% AUTOCONFIGURE
USING MEM_PERCENT %MEMORY% APPLY DB AND DBMdb2 CONNECT TO %DBNAME% USER %1 USING %2del schema.log triggers.log app_objects.logdb2 set schema user1db2 –t –v –f schema.db2 –z schema.logdb2 –td@ -v –f triggers.db2 –z triggers.logdb2 –td@ -v –f functions.db2 –z functions.log
• To execute this script from the command line, you would issue the following command:
create_database.bat db2admin ibmdb2
Password
UserID
A Simple Operating System (Shell) Script
Bina Nusantara University 9
Remote Listeners
db2ipccm
db2sysc
db2aiothr
Instance Level
db2pfchr
db2pclnr
db2dlock
db2loggrdb2loggw
Database LevelApplication Level
Remote Client
Application
Local Client
Application
DB2 Data Server
db2start
Other EDUs
Other EDUs
db2agent
db2agntp
db2agent
db2agntp
Idle Agent Pool
Per Request
db2lmr
db2med
Other EDUsdb2agnta db2agnta
db2sysc
db2wdog
db2vend
db2acd
db2fmp
FIREWALL
DB2 Process Model – Threaded engine infrastructure
Bina Nusantara University 10
• db2sysc– The main DB2 system controller or engine. In DB2 9.5, there is only one multi-
threaded main engine process for the entire partition. All Engine Dispatchable Units (EDUs) are threads inside this process. Without this process, the database server cannot function. On Windows the process name is db2syscs.
• db2acd – The autonomic computing daemon. It is used to perform client side automatic tasks,
such as health monitor, automatic maintenance utilities, and the admin scheduler. This process was formerly called db2hmon. It runs multiple threads that establish connections to the database engine to perform this work.
• db2wdog – The DB2 watchdog. The watchdog is the parent of the main engine process, db2sysc.
It cleans up resources if the db2sysc process abnormally terminates, and spawns db2fmp processes as well as the health monitor process.
• db2vend – The fenced vendor process introduced in DB2 9.5 All vendor code runs in this process
outside of the engine. • db2fmp
– Fenced processes that run user code on the server outside the firewall for both stored procedures and user defined functions. This process replaces both the db2udf and db2dari processes that were used in previous versions of DB2.
Common Processes
Bina Nusantara University 11
• db2sysc – The system controller thread. This thread is responsible for the start-up
and shut-down and the management of the running instance. Its EDU name is the same as the process name of the main db2sysc process which spawned this thread.
• db2tcpcm– tcpip communication/listener
• db2agent– Coordinator agent that performs database operations on behalf of
applications (at least 1 per connection, depending if Connection Concentrator is enabled).
• db2agntp– Active subagent spawned if INTRA_PARALLEL is set to YES. Will perform
database operations for the application. db2agent will coordinate the work between the different db2agntp subagents.
• db2pfchr– DB2 asynchronous I/O data prefetcher (NUM_IOSERVERS)
• db2pclnr– DB2 asynchronous I/O data writer (NUM_IOCLEANERS)
Common Threads
Bina Nusantara University 12
Audit buffer size(audit_buf_sz)
Monitor heap(mon_heap_sz)
Database Manager Shared Memory(including FCM)
(app_ctl_heap_sz)
Application Global Memory
Agent stack(agent_stack_sz)
DRDA heap(drda_heap_sz)
Statistics heap(stat_heap_sz)
UDF memory(udf_mem_sz)
Sort heap(sortheap)
Statement heap(stmtheap)
Applicationheap
(applheapsz)
Query heap(query_heap_sz)
Java heap(java_heap_sz)
Client I/O block(rqrioblk)
Agent Private Memory
(remote)
Application support layer heap(aslheapsz)
Client I/O block(rqrioblk)
Agent/Application Shared Memory
(local)
Database heap(dbheap)
Utility heap(util_heap_sz)
Backup buffer(backbufsz)
Restore buffer(restbufsz)
Package cache(pckcachesz)
Log buffer(logbufsz)
Catalog cache(catalogcache_sz)
Bufferpools(buffpage)
Extended memory cache
Locklist(locklist)
Database Global Memory
DB2 Memory Model
Bina Nusantara University 13
• Real memory cache for table/index data
• Reduces direct sequential I/O– Promotes asynchronous reading (pre-fetching) and
writing
• Allocates memory in units of 4K,8K,16K and 32K pages
• At least one buffer pool required per database
• At least one matching bufferpool for a table space based on page size
Buffer Pool Basics
Bina Nusantara University 14
Control Center > (expand) All Databases folder > (right-click) Buffer Pools folder > Create
Creating a Buffer Pool
Bina Nusantara University 15
Buffer Pool Name
Page Size
Number of Pages
Area for Block-based Buffer Pools
When Buffer Pool Is Created
Create Buffer Pool Dialog
Bina Nusantara University 16
• A place to store tables - abstracts the details of physical storage
• Composed of one or more "containers" (files, directories, or raw devices)
• Characteristics– page size of 4KB, 8KB, 16KB, or 32KB
• Must correspond to a buffer pool with same page size– extent size
• the number of pages written to one container before writing to the next
– pre-fetch size• the number of pages read data when pre-fetching is
performed– buffer pool
• the buffer pool to use as cache
Table Space Basics
Bina Nusantara University 17
• REGULAR– for user tables– example: USERSPACE1
• LARGE– optionally separate out LOB data into its own table
space. – Use also for XML.
• TEMPORARY– SYSTEM TEMPORARY
• used by DB2 for internal operations, such as sorts• example: TEMPSPACE1
– USER TEMPORARY• Used for User Defined Global Temporary tables (in-memory)• Often confused with system temporary table spaces
Table Space Types
Bina Nusantara University 18
• MANAGED BY SYSTEM– System Managed Storage (SMS)– Easy to manage– Containers:
• are file system directories• space not pre-allocated, files grow dynamically • fixed at creation time (containers cannot be added later)
– Cannot split index and LOB data across different table spaces
• MANAGED BY DATABASE– Database Managed Storage (DMS)– Containers:
• Pre-allocated files or raw devices– If raw devices, data is written directly without O/S caching
• containers can be added/dropped/resized
– Can place index, table and LOB data in separate table spaces
Table Space Management
Bina Nusantara University 19
• MANAGED BY AUTOMATIC STORAGE
– Intended as a “single point of storage management” for table spaces
– Create a database and associate a set of storage paths with it
– No explicit container definitions are provided– Containers automatically created across the storage
paths– Growth of existing containers and addition of new ones
completely managed by DB2– Redefine those storage paths during a database
RESTORE
Table Space Management
Bina Nusantara University 20
– CREATE DATABASE DB1 AUTOMATIC STORAGE YES
– CREATE DATABASE DB3 ON /data/path1, /data/path2
– CREATE TABLESPACE TS2 MANAGED BY AUTOMATIC STORAGE
– CREATE TEMPORARY TABLESPACE TEMPTS
– CREATE USER TEMPORARY TABLESPACE USRTMP MANAGED BY AUTOMATIC STORAGE
– CREATE TABLESPACE TS1INITIALSIZE 500 K INCREASESIZE 100 K MAXSIZE 100 M
Automatic Storage Examples
Bina Nusantara University 21
"This is a 4K table space with an extent size of 8 and uses 3 raw containers" ƒ Translation: DMS table space. 4K page size. 32K of data is written to one
disk before writing to the next. (4K x 8 pages per extent = 32K) Tables do not share extents
0036912
0 14710
25811
Extents Stripedacross containers
Table2
Table1
Pages and Extents
Bina Nusantara University 22
Creating a Table Space
Control Center > (expand) All Databases folder > (right-click) Table Spaces folder > Create
Bina Nusantara University 23
• Inter-query Parallelism– Multiple queries running at the same time
• Intra-query Parallelism– Intra-partition parallelism
• Turned on by the INTRA_PARALLEL dbm cfg parameter
– Inter-partition parallelism
Query parallelism
Bina Nusantara University 25
Data
Database Partition
A query is dividedinto parts, each beingexecuted in parallel.
SELECT... FROM...
Data
Database Partition
Data
Database Partition
Data
Database Partition
Inter-partition Parallelism (DPF option in EE)