Professional Documents
Culture Documents
Tommi Teofilovski
Foreword....................................................................................................................................................1
The Database Environment at Stockholm University...............................................................................2
Server.................................................................................................................................................2
Client..................................................................................................................................................2
DB2 - A General Overview.......................................................................................................................2
Using DB2.................................................................................................................................................4
GUI.....................................................................................................................................................4
Command Window............................................................................................................................6
Configuring your Database and Database Manager.................................................................................7
Database Manager (Instance) Configuration ...................................................................................7
Database Configuration for Database ...............................................................................................8
How to Access and Manage the Configuration Parameters......................................................................8
GUI.....................................................................................................................................................8
Command Window............................................................................................................................8
Performance and Monitoring Your System...............................................................................................9
Optimizer and Explain....................................................................................................................10
Snapshots.........................................................................................................................................11
DB2batch.........................................................................................................................................12
How to Save Your Database.....................................................................................................................13
Finding Documentation ..........................................................................................................................13
Locally..............................................................................................................................................13
Internet.............................................................................................................................................14
Db2diag.log and Db2trace...............................................................................................................15
Dictionary................................................................................................................................................15
References................................................................................................................................................15
Foreword
The purpose of this paper is to give an introduction and general overview of the
database DB2 UDB (will be referred to as DB2), how to use DB2 and the
environment at the Institution of Computer and System Science at Stockholm
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
The Database Environment at Stockholm University
DB2 is running under Windows NT, and is integrated in the Local Area Network at
DSV. There is one Server machine, only to be used by the DB2 UDB Database
Administrator, and 189 Client machines distributed along fifth and seventh floor to be
used by the students. These Client machines can be found in DSV datorsalar
Server
Server name: DB-SRV-1
Level of DB2: Universal Database Enterprise Edition 6.1 and UDB SDK 6.1.
CPU: uniprocessor, Pentium 400 Mhz
RAM: 512 MB
OS: Windows NT 4.0 Server, Service Pack 9
Database Storage: System Managed Storage (SMS), unless something else is
specified.
Client
Level of DB2: Universal Database Personal Edition 6.1, (it not possible to access a
database residing on Personal Edition remotely)
CPU: Pentium 400 Mhz
RAM: 128 MB
OS: Windows NT 4.0 Workstation, service pack 9
Database Storage: System Managed Storage (SMS), by default.
To be able to use DB2 as a student at DSV you have to have a special DB2 user
account issued by DMC. This account gives you System Administration Authority
(SYSADM) which is the highest level of authentication in DB2. As such you can do
more or less everything with DB2, that is create, administer and work with databases
on your local machine.
Once you have a DB2 user account, you will be able to access databases residing on
the Server. But you cannot do any operations on these databases unless you have
DBADM authority for the database you are interested in. This authority will only be
given to you if you are registered on a course. This means that you will get your own
database on the Server that only you as a student (or group) can access.
As a DB2 user registered on a course there will be space allocated on the server for
you to save your data on, like back-ups, scripts and other matters.
The environment at DSV is special because the client users are able to use different
client machines every time they use DB2.
DB2 A General Overview
This part will introduce general components of the Database environment and briefly
describe them.
2
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
System is the machine where you are using DB2. In the system you can see your own
machine called LOCAL or other machines that you have a connection to, like for
instance the Server.
Database manager (also referred to as the instance) is part of DB2 that manages
data. It controls what can be done with the data, and manages system resources
assigned to it. Each instance is a complete environment. An instance has its own
databases (which other instances cannot access), and all its database partitions share
the same system directories. It also has separate security from other instances on the
same machine.
A database is organized into parts called table spaces. A table space's definition and
attributes are recorded in the database system catalog. Once a table space is created,
you can then create tables within this table space. A container is assigned to a table
space. A container is an allocation of physical storage (such as a file or device). Table
spaces reside in nodegroups.
A table consists of data logically arranged in columns and rows. The data in the table is
logically related, and relationships can be defined between tables. Data can be viewed
and manipulated based on mathematical principles and operations called relations.
Table data is accessed via SQL, a standardized language for defining and manipulating
data in a relational database. All database and table data is assigned to table spaces.
figure 1.1
3
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Using DB2
DB2 commands and SQL statements in DB2 for NT can be done through the
Graphical User Interface (GUI) or through the Command Window. In most of the
examples below it will be assumed that the Command Window will be used. Although,
the GUI part will be briefly described.
The purpose is to make you aware of all the different ways DB2 can be used. It is up
to you as a user to decide what you prefer.
GUI
There are a number of GUI tools in DB2, but this paper will only describe and mention
some of them. The GUI tools can normally be found under Start→Programs →DB2
for Windows NT → , see figure 1.2
figure 1.2 Where to find the GUI tools for DB2 UDB
Control Center
This tool is pretty intuitive and easy to use, see figure 1.3. It shows you all the objects
relevant for DB2 in your system. Under Systems the different machines in the network
can be found. Each machine has one or more Instances, under which you can find a
folder with all the databases for that instance. Under all the databases you can find the
content of them. By clicking on any of these objects with the right mouse button, a list
of options will appear.
4
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Command Center
From the Command Center you can issue DB2 commands and SQL statements.
5
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Command Window
The special DB2 “Command Window” has to be opened from Start→Programs→
DB2 for Windows NT→ or by typing “db2cmd.exe” at any MS-DOS Command
Prompt. If the special DB2 Command Window is to be used, it is advised to change the
color properties for this Command Window by right clicking on the Command
Window titlebar, this is to distinguish it from the MS-DOS Command Prompt. Notice
that when issuing DB2 commands or SQL statements from the Command Window the
command has to be preceded by “db2”. If you want to avoid this type “db2”, hit Enter
and then issue your commands or statements. Alternatively the “Command Line
Processor” can be called from Start→Programs→DB2 for Windows NT→.
figure 1.4 Command Window opened by typing “db2cmd” from a MS-DOS prompt or by calling “Command
Window” from Windows NT’s Start menu.
figure 1.5 Command Line Processor, can be created by typing “db2” + Enter or by calling ”Command Line
Processor” from Windows NT’s Start menu.
Below follows some simple examples that gives you an idea of how DB2 can be used.
There are of course a lot more to it, more options and more commands.
Create a Database
To initializes a new database just type:
DB2 CREATE DATABASE SU on d
6
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
SU is the name of the database that you want to create and d is the drive where you
want to create the database on.
Observe that the CREATE DATABASE statement is considered as a command in
DB2, thus the information on this command can be found in the Command Reference.
Create Tables
The CREATE TABLE statement defines a table. The definition must include its name
and the names and attributes of its columns.
CREATE TABLE DSV
(STUDNO CHAR(3) NOT NULL,
STUDNAME VARCHAR(36) NOT NULL,
PROGRAM CHAR(6),
PRIMARY KEY(STUDNO))
A database connection (DB2 CONNECT TO SU) has to exists in order for this
statement to work. If this SQL statement is entered in the Command Window it has to
be proceeded by DB2 and submitted as one line. The CREATE TABLE statement is
considered as a SQL statement and is documented in the SQL reference.
Insert
The INSERT statement inserts rows into a table or view.
INSERT INTO DSV (STUDNO, STUDNAME, PROGRAM )
VALUES ('123', 'TOMMI', 'DSVL4')
As before this command can be issued from the Command Center or a Command
Window. The INSERT statement is a SQL statement.
Configuring your Database and Database Manager
There are more than hundred configuration parameters in DB2, which are used to
control your system. These parameters fall into two general categories:
Some parameters are more important than others. In the Administration Guide chapter
19, you can find all the parameters available in DB2, a detailed description and
recommendations. To give you an idea of these parameters some of them are shown
below.
Database Manager (Instance) Configuration
7
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Database Configuration for Database
How to Access and Manage the Configuration Parameters
GUI
The configuration parameters can be accessed through the Control Center by right-
clicking on the Instance (Database Manager) or Database and choosing “Configure…”
from the menu. In the GUI version the parameters are nicely categorized by
functionality.
Command Window
Open a Command Window as previously described and issue,
DB2 GET DBM CONFIGURATION
to get the configuration parameters for your Database Manager (DBM) and
DB2 GET DB CONFIGURATION FOR <DATABASENAME>
8
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
DB2 UPDATE DBM CONFIGURATION USING <PARAMETER> <VALUE>
DB2 UPDATE DB CONFIGURATION FOR <DATABASE> USING <PARAMETER> <VALUE>
Notice, because the environment at DSV is set up so that users can sit at different
machines every time they logon DB2 there is a big probability that someone has
changed the Database Manager Configuration parameters so they are not compliant
with your wishes. Therefore it is recommended that you run fixDBenv.exe every time
you start using DB2 from your “new” client machine. This program will set all the
Database Manager configuration parameters to the default settings. This program also
UNCATALOG’s all the remote connections to the server.
If you have made changes to the configuration parameters and want to save them so
you can use them on a new client machine you can do it by redirecting the “DB2 GET
DBM CFG” output to a file and saving this file. To redirect it to a file just type “DB2
GET DBM CFG > dbm.out” in a Command Window, where dbm.out is the file where
the output is saved. To make things easier it might be desirable to create a little batch
file dbm.bat that sets your parameters automatically. This dbm.bat file has to be run
from the Command Window and should contain for example,
Performance and Monitoring Your System
When it comes to performance there are many aspects that has to be considered, for
example: applications, use of utilities, caching, environmental considerations,
operational considerations, programming considerations and how the database is set
up. Some of these aspects can be controlled by changing the configuration parameters
described in the previous section. This paper will only give an overview of how the
performance can be controlled and how you can monitor the affect of changed
parameters.
9
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Optimizer and Explain
The optimizer is the component within DB2 that determines the access path to be
used, that is how the SQL statement should be executed. Before execution of the
actual SQL statement a executable access plan is generated. The access plan contains
information on how the SQL statement will be executed. It tells you what index will be
used, sort methods, locking semantics and join methods. There are different tools in
DB2 to generate explain data, which is the access plan visualized.
DB2vexp.exe or better known as Visual Explain is a GUI tool and is by far the easiest
tool to use. To use Visual Explain go to the Control Center as described in the
previous section, right click on a database you want to get the explain for and select
“Explain SQL…”, and type the SQL statement and submit. This will result in a graph
Figure xx, this is the Visual Explain output for “select * from dsv”.
The other tools that can be used to generate the access plan are db2expln, dynexpln
and db2exfmt. The output and purpose of these other tools can differ. For more
information on the other tools see Administration guide or Command Reference.
How to interpret the explain plan? There is good documentation in the Administration
guide that describes and explains all the different parts. But the fastest and easiest way
is to right click on the object and select “Help on Operator”.
10
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Snapshots
Snapshots tells you what state or what is happening in your environment and system at
a given time after a given operation. This can be very useful when tuning your system
and finding bottlenecks.
In DB2 there are different kind of monitor switches, Buffer pool, Lock, Sort,
Statement, Table and Unit of work. (see dbm configurations).
By default the snapshot monitor switches in the Database Manager Configuration is set
to OFF. Before you get snapshots you have to turn the parameter ON, by issuing for
example:
DB2 UPDATE DBM CONFIGURATION USING DFT_MON_BUFPOOL ON
After this is done, do some operation for example “DB2 SELECT * FROM DSV”. DB2 will
now monitor and get snapshot of the bufferpool (DB2’s cache). To view the snapshot
information issue:
DB2 GET SNAPSHOT FOR ALL BUFFERPOOLS
Bufferpool Snapshot
Bufferpool name = IBMDEFAULTBP
Database name = SU
Database path = D:\DB2\NODE0000\SQL00001\
Input database alias =
Buffer pool data logical reads = 27
Buffer pool data physical reads = 12
Buffer pool data writes = 0
Buffer pool index logical reads = 60
Buffer pool index physical reads = 27
Total buffer pool read time (ms) = 2
How to interpret this information? In the “System Monitor Guide and Reference” you
can find a description off all the snapshots, their meaning and recommendations, what
configuration parameter that affect respective snapshot and so on.
As an example, Buffer pool data logical reads, indicates the number of logical read
requests for data pages that have gone through the buffer pool, while “Buffer pool
data physical reads” indicates the number of read requests that required I/O to get
data pages into the buffer pool.
It is desirable to do little I/O operations and more buffer pool operations because
reading from a physical disk takes more time than reading from memory. Changing the
buffer pool size (see CREATE BUFFERPOOL in SQL Reference) will affect these
snapshots.
11
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
DB2 RESET MONITOR ALL
Notice, after you are finished working with the snapshots, make sure that you turn
them OFF, since having them turned can affect the performance.
DB2 UPDATE DBM CONFIGURATION USING DFT_MON_BUFPOOL OFF
Snapshots can also be made by choosing “Snapshot Monitoring” from the menu that
appears when right clicking on the database in the Control Center.
DB2batch
Db2batch is a command-only benchmark tool that reads SQL statements from either a
flat file or standard input, dynamically prepares and describes the statements, and
returns an answer set. This tool gives you the elapsed time of the SQL statement in
seconds. DB2batch is very useful when you want to see what affect your changes had
on performance. There is also an option in DB2batch that enables snapshots to be
generated.
Example:
Create an input file infile.sql for db2batch containing your SQL Statement,
DB2BATCH –d SU –f infile.sql
Running in Embedded Dynamic mode.
Statement number: 1
select * from dsv
STUDNO STUDNAME PROGRAM
12
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
123 TOMMI DSVL4
Number of rows retrieved is: 1
Number of rows sent to output is: 1
Elapsed Time is: 0.040 seconds
Summary of Results
==================
Elapsed Agent CPU Rows Rows
Statement # Time (s) Time (s) Fetched Printed
1 0.040 Not Collected 1 1
Arith. mean 0.040
Geom. mean 0.040
For more information on how to use this tool, see “Command Reference”,
“Administration Guide” or type “db2batch –h” from the Command Window.
How to Save Your Database
In a normal environment a backup is simply enough. But in our environment we
recommend a simpler way of saving your database, using db2move. To move (save)
your database, create a special directory where you want to save your database. From
that directory issue,
DB2MOVE <DATABASE> EXPORT
This will EXPORT and copy the specified database to the current directory. To restore
the database you have to create a database to which you want to import the exported
files and then import your data to that database,
DB2 CREATE DATABASE SU2 ON <DRIVE>
DB2MOVE SU2 IMPORT
The DB2move command has to be issued from the directory where the database has
been saved. DB2move is only command based.
Finding Documentation
There is a lot of information available on DB2 that can be found either on your system
or on the Internet. Below is explained how and where this information can be found.
Locally
The best place to find information on DB2 is through Information Center that can be
found under the “DB2 for Windows NT” folder or be accessed by typing db2ic from
the command line.
Here you can find most of the documentation available on the product, like the
Administration Guide, SQL Reference, Command Reference, Message Reference and
so on.
13
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
A lot of information on usage can also be found in the programs, for example to find
information on how to use db2move, just type “db2move –h”. More detailed
information on the tools can also be found in the on-line documentation. To list all the
commands available type “db2 ?” in the Command Window. To find information about
a particular DB2 command type DB2 ? <command>, for example “DB2 ? CREATE” and
information on the command will be listed on the screen. For more detailed
information on the command see the Command Reference.
If you get an SQL error message in the Command Window for example after issuing
DB2 LIST TABLES FOR ALL
SQL1024N A database connection does not exist. SQLSTATE=08003
you can find out what this error message means by looking in the Message Reference
or the easiest way just to type
DB2 ? SQL1024
SQL1024N A database connection does not exist.
Cause: There is no connection to a database. Other SQL
statements cannot be processed unless an SQL CONNECT was
previously executed.
The command cannot be processed.
Action: If the error occurred when disconnecting from the
database, continue processing. If the error occurred on
another SQL statement, issue an SQL CONNECT statement and
resubmit the command or statement.
sqlcode: 1024
sqlstate: 08003
and says that it cannot process the previous command unless the CONNECT statement
is executed. For information on the CONNECT statement see the SQL Reference.
To get an introduction to DB2 UDB v5.2, run the UDBDEMO program found under
“DB2 for Windows NT”.
Internet
On IBM’s homepage you can find the latest information and documentation on DB2,
“http://www.software.ibm.com/data/db2/library/”. Two links that are recommended
here are “DB2 Publications” (contains links to all the documentation) and “DB2
Universal Database” (contains a tool for searching all the documentation)
14
Department of Computer and System Science, Stockholm University
Tommi Teofilovski
Db2diag.log and Db2trace
In DB2 there is a log file, db2diag.log , that keeps track of everything that is going on
within DB2. If a problem occurs that you cannot find a explanation for the db2diag.log
file is a good place to start looking for what can be causing the problem. The log file
can be found under “C:\SQLLIB\<INSTANCE>”. You can control the level of
information in this file by setting the Database Manager Configuration parameter
DIAGLEVEL (described later in this paper). Using the db2diag.log file as a source of
information is normally a job for the Administartor.
It might turn out that the information in the db2diag.log file is not sufficient, and you
might do a trace. To do a trace you have to use the db2trc tool. DB2 uses its trace
facility to trace events, dump trace data to a file, and format trace data into a readable
form. Normally the trace facility is only used when directed by DB2 Customer Service
or by a technical support representative.
Dictionary
References
15