DB2 Overview

20
Department of Computer and System Science, Stockholm University Tommi Teofilovski Foreword............................................................2 The Database Environment at Stockholm University....................2 Server..........................................................2 Client..........................................................2 DB2 - A General Overview............................................3 Using DB2...........................................................4 GUI.............................................................4 Command Window..................................................6 Configuring your Database and Database Manager......................8 Database Manager (Instance) Configuration.......................8 Database Configuration for Database.............................8 How to Access and Manage the Configuration Parameters...............9 GUI.............................................................9 Command Window..................................................9 Performance and Monitoring Your System.............................10 Optimizer and Explain..........................................10 Snapshots......................................................11 DB2batch.......................................................13 How to Save Your Database..........................................14 Finding Documentation..............................................14 Locally........................................................14 Internet.......................................................15 Db2diag.log and Db2trace.......................................15 Dictionary.........................................................16 References.........................................................16

Transcript of DB2 Overview

Page 1: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

Foreword......................................................................................................................................... 2The Database Environment at Stockholm University.............................................................2

Server....................................................................................................................................... 2Client........................................................................................................................................ 2

DB2 - A General Overview...........................................................................................................3Using DB2....................................................................................................................................... 4

GUI............................................................................................................................................ 4Command Window................................................................................................................ 6

Configuring your Database and Database Manager...............................................................8Database Manager (Instance) Configuration....................................................................8Database Configuration for Database................................................................................8

How to Access and Manage the Configuration Parameters..................................................9GUI............................................................................................................................................ 9Command Window................................................................................................................ 9

Performance and Monitoring Your System............................................................................10Optimizer and Explain......................................................................................................... 10Snapshots............................................................................................................................. 11DB2batch............................................................................................................................... 13

How to Save Your Database......................................................................................................14Finding Documentation............................................................................................................. 14

Locally.................................................................................................................................... 14Internet................................................................................................................................... 15Db2diag.log and Db2trace..................................................................................................15

Dictionary...................................................................................................................................... 16References.................................................................................................................................... 16

ForewordThe 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

Page 2: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

University (DSV). The paper also gives an introduction on how to monitor performance and different aspects of it. This paper is intended to be used by teachers and students at the DSV. Where and how to find this documentation is specified in section “Finding documentation”

The Database Environment at Stockholm UniversityDB2 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

ServerServer 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 MBOS: Windows NT 4.0 Server, Service Pack 9Database Storage: System Managed Storage (SMS), unless something else is specified.

ClientLevel of DB2: Universal Database Personal Edition 6.1, (it not possible to access a database residing on Personal Edition remotely)CPU: Pentium 400 MhzRAM: 128 MB OS: Windows NT 4.0 Workstation, service pack 9Database 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 OverviewThis part will introduce general components of the Database environment and briefly describe them.

2

Page 3: DB2 Overview

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.

3

Page 4: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

figure 1.1

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.

GUIThere 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 StartProgramsDB2 for Windows NTsee figure 1.2

4

Page 5: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

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.

figure 1.3 Control Center

Command CenterFrom the Command Center you can issue DB2 commands and SQL statements.

5

Page 6: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

figure 1.3 Command Center

Command WindowThe special DB2 “Command Window” has to be opened from StartProgramsDB2 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 StartProgramsDB2 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.

6

Page 7: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

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 DatabaseTo initializes a new database just type:

DB2 CREATE DATABASE SU on d

in the Command Window.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 TablesThe 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.

InsertThe INSERT statement inserts rows into a table or view.

INSERT INTO DSV (STUDNO, STUDNAME, PROGRAM )VALUES ('123', 'TOMMI', 'DSVL4')

7

Page 8: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

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 ManagerThere are more than hundred configuration parameters in DB2, which are used to control your system. These parameters fall into two general categories:

- Database Manager Configuration- Database Configuration

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

Parameter Performance Impact

Additional Information

Aslheapsz High "Application Support Layer Heap Size (aslheapsz)"

Authentication Low "Authentication Type (authentication)"

Cpuspeed Low "CPU Speed (cpuspeed)"

Dftdbpath None "Default Database Path (dftdbpath)"

java_heap_sz High "Maximum Java Interpreter Heap Size (java_heap_sz)"

Maxcagents High "Maximum Number of Concurrent Agents (maxcagents)"

intra_parallel High "Enable Intra-Partition Parallelism (intra_parallel)"

Sheapthres High "Sort Heap Threshold (sheapthres)"

Svcename None "TCP/IP Service Name (svcename)"

Sysadm_group None "System Administration Authority Group Name (sysadm_group)"

Table 1, example of Database Manager configuration parameters

Database Configuration for Database

Parameter Performance Impact Additional Information Applheapsz Medium "Application Heap Size (applheapsz)"

Buffpage High "Buffer Pool Size (buffpage)"

dft_degree High "Default Degree (dft_degree)"

dft_queryopt Medium "Default Query Optimization Class (dft_queryopt)"

8

Page 9: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

Logbufsz High "Log Buffer Size (logbufsz)"

Maxlocks High "Maximum Percent of Lock List Before Escalation (maxlocks)"

Sortheap High "Sort Heap Size (sortheap)"

util_heap_sz Low "Utility Heap Size (util_heap_sz)"

Table 2, example of database configuration parameters

How to Access and Manage the Configuration Parameters

GUIThe 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 WindowOpen 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>

to get the configuration parameters for your database.To modify a Database Manager configuration parameter type:

DB2 UPDATE DBM CONFIGURATION USING <PARAMETER> <VALUE>

To modify a Database configuration parameter type:

DB2 UPDATE DB CONFIGURATION FOR <DATABASE> USING <PARAMETER> <VALUE>

Example: DB2 UPDATE DBM CONFIGURATION USING UTIL_HEAP_SZ 100000

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

9

Page 10: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

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 SystemWhen 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.

Optimizer and ExplainThe 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

10

Page 11: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

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”.

It is possible to control the optimizer by changing the Optimization class either through Visual Explain or by setting the database configuration parameter DFT_QUERYOPT.

SnapshotsSnapshots 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

11

Page 12: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

from the Command Window.

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

This will give you an output something like this:

Bufferpool Snapshot

Bufferpool name = IBMDEFAULTBPDatabase name = SUDatabase path = D:\DB2\NODE0000\SQL00001\Input database alias =Buffer pool data logical reads = 27Buffer pool data physical reads = 12Buffer pool data writes = 0Buffer pool index logical reads = 60Buffer pool index physical reads = 27Total 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.

Working with snapshots,Resetting the monitor switches effectively starts all of the counters at zero and further snapshots are based on the new counter values. This should be done every time you want to conduct new fresh snapshots with the following command,

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.

12

Page 13: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

DB2batchDb2batch 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,

From the Command Window issue:

DB2BATCH –d SU –f infile.sql

This will give you an output looking something like this:

Running in Embedded Dynamic mode.

---------------------------------------------Statement number: 1select * from dsvSTUDNO STUDNAME PROGRAM-------------------------------------------------------123 TOMMI DSVL4Number of rows retrieved is: 1Number of rows sent to output is: 1

Elapsed Time is: 0.040 seconds

Summary of Results==================

Elapsed Agent CPU Rows RowsStatement # Time (s) Time (s) Fetched Printed1 0.040 Not Collected 1 1

Arith. mean 0.040Geom. 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.

13

Page 14: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

How to Save Your DatabaseIn 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.

LocallyThe 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.

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 ALLSQL1024N 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

in the DB2 Command Window. This will generate

SQL1024N A database connection does not exist.

14

Page 15: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

Cause: There is no connection to a database. Other SQLstatements cannot be processed unless an SQL CONNECT waspreviously executed.

The command cannot be processed.

Action: If the error occurred when disconnecting from thedatabase, continue processing. If the error occurred onanother SQL statement, issue an SQL CONNECT statement andresubmit 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”.

Under C:\SQLLIB\SAMPLES you can find example files on how to do embedded programming, stored procedures, calling API’s and much more.

InternetOn 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)

TPC organizations homepage http://www.tpc.org is worth a visit. This is a non profitable organization that has all the major computer and database vendors as members. These vendors measure their performance by running benchmarks put together by the TPC organization. The result is then put in relation to each other.

Db2diag.log and Db2traceIn 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.

For more information on this see “Troubleshooting Guide”.

15

Page 16: DB2 Overview

Department of Computer and System Science, Stockholm University Tommi Teofilovski

Dictionary

References

16