Professional Documents
Culture Documents
Information Management
V2012-04-18
Table of Contents
DB2 10 for Linux, UNIX and Windows Course Workbook Page 2 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 3 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 Suggested Reading .............................................................................................................................................................................3
3 About this Lab......................................................................................................................................................................................3
4 Environment Setup Requirements.....................................................................................................................................................3
5 Initial Steps...........................................................................................................................................................................................4
5.1 The SAMPLE Database...................................................................................................................................................................................... 4
9 Summary ............................................................................................................................................................................................28
10 Cleanup...............................................................................................................................................................................................28
DB2 10 for Linux, UNIX and Windows Course Workbook Page 4 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
DB2 for Linux, UNIX and Windows is a relational database management system developed by IBM®. Since its inception, DB2
has received constant enhancements in areas such as compression, autonomics, security, XML data handling, manageability,
and performance.
With the advent of IBM Data Studio comes a major advance in the way DB2 developers and administrators alike carry out their
day to day functions. Historically, depending on the tasks to be completed, it was common to switch back and forth between
disparate tools such as Control Center, Health Monitor, Developer Workbench, and even the DB2 Command Line Processor
(CLP).
IBM Data Studio version 3.1.1 changes all this, facilitating DB2 administration, design, development, and monitoring all within an
integrated Eclipsed-based environment. By leveraging the power of Data Studio, users are certain to enjoy increased
productivity as they find themselves able to perform a majority of their tasks within a single environment.
In this lab, you will explore the basic concepts of DB2 with Data Studio 3.1.1 from the perspective of both a developer and a
database administrator.
2 Suggested Reading
IBM Data Studio version 3.1 Information Center: http://publib.boulder.ibm.com/infocenter/dstudio/v3r1/index.jsp
DB2 10 for Linux, UNIX and Windows Course Workbook Page 5 of 335.
IBM Software
Information Management Technology Ecosystem
5 Initial Steps
4. Ensure that the DB2 Database Manager has been started by issuing the following
db2start
A message like the following should be displayed to if the DB2 Database Manager had not been started yet:
db2inst1@db2v10:~/Desktop> db2start
12/14/2011 16:36:11 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
The SAMPLE database can be used for various purposes like testing your own applications, or trying different features of DB2
using the provided sample programs. During this lab, we will use the SAMPLE database for demonstrating various features of
DB2 that makes it easy to understand the technology.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 6 of 335.
IBM Software
Information Management Technology Ecosystem
The data and database objects are created using a real time environment on a small scale.
1. Use the DB2 command db2sampl to create a sample database that we will be using in the subsequent sections. Enter
the following command in the terminal window:
db2sampl –sql –xml –name DB2FUND
Note: -sql: creates tables, triggers, functions, procedures, and populates the tables with data.
-xml: creates tables with columns of data type XML, creates indexes on the XML columns, registers XML schemas, and
populates these tables with data including XML document values.
-name: defines the database name.
The following message should be displayed, indicating that the DB2FUND database has been created:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 7 of 335.
IBM Software
Information Management Technology Ecosystem
2. If this is the first time Data Studio is launched, it may take a moment. Click OK to accept the default workspace.
3. By default, the Database Administration perspective is opened. Your workspace should look like the following :
DB2 10 for Linux, UNIX and Windows Course Workbook Page 8 of 335.
IBM Software
Information Management Technology Ecosystem
• Administration Explorer
• Object List
• Properties / Results view
Note: Perspective defines the initial set and layout of the components in your screen. Each perspective provides a set of
functionality aimed at accomplishing a specific type of task or works with specific types of resources.
For instance, the “Database Administration” perspective allows you carry on administrative tasks, while the “Query
Tuning” perspective provides the tools for you to optimize SQL queries.
Views are screen components that present information and allows you to navigate through the system. Eg: “Administration
Explorer”and “SQL Results” views.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 9 of 335.
IBM Software
Information Management Technology Ecosystem
Note: in case if you are not in Database Administration perspective, change it using at the upper right
corner of your Data Studio workspace.
If you are familiar with Control Center, which has been deprecated since DB2 9.7, the following table shows the mapping of the
main display areas between Control Center and Data Studio.
Content pane Object List area Lists database objects. In Data Studio, several tabs can be
open for different purposes.
Content Details pane Properties view After selecting an object, its properties can be seen in this area.
Data Studio can have multiple tabs in this area for different
purposes.
1. In Administration Explorer, expand the folder All Databases until you see DB2FUND.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 10 of 335.
IBM Software
Information Management Technology Ecosystem
Note: To connect to a remote database, users can click on the toolbar in the Administration Explorer view, select
, fill in required fields, and connect. The new database connection will be available in the
Administration Explorer view.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 11 of 335.
IBM Software
Information Management Technology Ecosystem
4. Click OK.
5. After a connection has been successfully established, expand the db2inst1 instance folder in Administration Explorer,
you should see this icon beside the DB2FUND database, which indicates a connected database. Disconnected
databases are listed with . Expanding the DB2FUND database folder will list supported database object types, as
seen in the following screen capture :
DB2 10 for Linux, UNIX and Windows Course Workbook Page 12 of 335.
IBM Software
Information Management Technology Ecosystem
Now that you have connected to database DB2FUND, let’s see how you can browse database objects in Data Studio.
In the Administration Explorer, database objects are grouped by type, and they are displayed as folders. Clicking any folder will
have its content automatically displayed in the Object List area. We’ll use tables as an example.
1. Expand DB2FUND in Administration Explorer, click the Tables folder. The list of content is automatically displayed in
the Object List area on the right.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 13 of 335.
IBM Software
Information Management Technology Ecosystem
Notice that the current path and the total number of objects are
shown at bottom of the view.
2. Click on the EMPLOYEE table in the Object List, its properties are automatically displayed in the Properties view at the
bottom of your workspace, and they are read-only. The information displayed is dependant on the type of the database
object you select. For tables, you can view properties such as columns definition, privileges, table spaces used, etc.
At the top of the Object List area, you can see a multi-purpose navigation bar. It consists of the following sections:
• Web-browser-like back / forward navigation.
• Breadcrumb navigation, which facilitates moving through the hierarchy of objects.
• “Create new object” button .
• In-place search function. Simply type the object name or part of it, and the list would be dynamically updated.
Wildcard characters ‘%’ and ‘_’ (underscore) are supported.
• Options to sort, filter, refresh objects list.
3. Clicking any of the arrows on the current path would give you a list of all objects at that hierarchical level. For example,
click the arrow next to gives you access to all objects at the DB2FUND database level.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 14 of 335.
IBM Software
Information Management Technology Ecosystem
The in-place search feature allows users easily find a particular object they look for.
4. Using the navigation bar, move to the “Table Name Search” field (since we were looking at tables in DB2FUND) and
type ‘E%E’. All tables with a name that starts with E and ends with E are returned.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 15 of 335.
IBM Software
Information Management Technology Ecosystem
Note: When using the option “Browse Data”, the data is read-only.
Note: By default in Data Studio 3.1, the maximum number of records to be displayed is set to 500. To change this behavior, on the
top menu bar in Data Studio, go to Window > Preferences > Data Management > SQL Development > SQL Results View Options.
To modify individual records in a table, a simple way is the Edit Data option.
7. Move back to the DB2FUND tab. Right click on the EMPLOYEE table and select the option . A new tab is
opened just like browsing data, but this time the records can be modified. Double click the phone number cell for
employee Michael Thompson, change the number from 3476 to 3477, and then press enter.
The modified cell is highlighted. Now the EMPLOYEE table should look like the following:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 16 of 335.
IBM Software
Information Management Technology Ecosystem
Notice that an asterisk (*) has been added to the tab name. This is to indicate that you have uncommitted changes in
that editor.
8. Click at the upper right corner of the tab to deploy changes to the target database. Click “Yes” to confirm. Then a
message should appear notifying you that the data change has been successfully committed.
As mentioned earlier, right clicking on an object would bring out a context-based menu of administrative tasks. If you then select
a particular task, a dialog may appear in a tab in the Object List area to help you execute the task you want; such dialogs are
called Task Assistants in Data Studio. If you are familiar with DB2 Control Center, task assistants work in a similar fashion as
the wizards and advisors. Since task assistants appear in tabs, multiple task assistants can be open simultaneously. This means
that you can navigate to other parts of Data Studio then come back to the task assistants that you opened earlier.
We will take the “Run Statistics” task assistant as an example to illustrate how task assistants help database administrators in
managing databases. This task assistant updates statistics about the characteristics of a table and/or associated indexes, or
statistical views. These characteristics include number of records, number of pages, and average record length. The optimizer
uses these statistics when determining access paths to retrieve the data.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 17 of 335.
IBM Software
Information Management Technology Ecosystem
1. Go to the list of tables. Right click table SALES, go to Manage, select Run Statistics.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 18 of 335.
IBM Software
Information Management Technology Ecosystem
As it is number labeled in the above screen capture, a task assistant dialog consists of 4 sections: Connection, Settings,
Command, and Messages.
1. Connection: shows database connection details
2. Settings: specifies options for the database commands to be run
3. Command: displays the generated commands for the selected settings. Users can edit these generated
commands before execution or save them for later use
4. Messages: shows the status of executing the commands
2. You can review the generated command to be run in the Command section. For this exercise, we keep the auto-
As the commands are being executed, status messages will be displayed in the Messages section. When the
commands finished executing, the Messages section displays the overall result.
In Data Studio, all changes made to a database are first collected in a “Change Plan” in the local workspace. A database can
have multiple change plans, but only one plan can be active at a time, which means that changes are added to the current active
plan. When a change plan is active, the Object List area includes an additional toolbar for the plan. A change plan remains
active until the user deploys the changes to the target database, closes the plan to work on it later, or makes another plan the
active plan. The list of change plans for the database can be found in the Change Plans folder in Administration Explorer.
1. Go back to the list of tables. Right click on table EMPLOYEE and select . The properties of the table should be
displayed in the Properties view. You can now make changes directly in this view and deploy them.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 19 of 335.
IBM Software
Information Management Technology Ecosystem
3. Click to add a new column. A new column with default name “Column1” is added at the bottom of the list. Click its
name and change it to LOCATION.
4. Click its data type, and select VARCHAR from the drop-down menu.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 20 of 335.
IBM Software
Information Management Technology Ecosystem
As you are making changes to the EMPLOYEE table, a change plan is created in Data Studio. This can be verified by the
additional toolbar for change plans in the Object List area.
A change plan is created with a default name of “Default Change Plan timestamp”, where the timestamp has the form yyyy-mm-
dd hh-mm-ss.
In the Object List, icons indicate whether objects for the active change plan are being created ( ), altered ( ), or dropped ( ).
For example, the delta icon on the left of the EMPLOYEE table indicates that the table has been altered.
Closes the change plan. If any changes have not been saved, you are prompted to
save or discard the changes when the change plan is closed.
Before deploying changes, you can compare the object before and after the changes.
8. In the Properties view, click to compare both versions. A new window appears and the changes are outlined.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 21 of 335.
IBM Software
Information Management Technology Ecosystem
Note: options such as generating a difference report, an impact analysis report, etc. are available on the toolbar.
10. Then, in the Object List area, click the red icon on the change plan toolbar to close
the plan. Finally, select Save. The change plan you just created has been saved in your local workspace so you can
continue working on it in the future.
Note: changes you made in your change plan are kept local in your workspace. They do not affect the actual target database.
11. Go to Administration Explorer, expand the database folder DB2FUND and click Change Plans. The change plan you
just saved should be listed in the Object List area.
In Data Studio, the SQL Script Editor offers development and debugging capabilities for database routines. It also provides
syntax highlighting, content assist, and several other well integrated tools to facilitate database application development.
1. In Administration Explorer, ensure that database DB2FUND is selected. On the menu bar, click and select
“New SQL Script”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 22 of 335.
IBM Software
Information Management Technology Ecosystem
There are 4 tabs at the upper half of the editor to provide users with more options:
• Configuration: specify execution environment, commit control, error control, etc.
• Validation: validate syntax against a specific data server, set statement terminator, etc.
• Special Registers: specify values for CURRENT SCHEMA and CURRENT PATH special registers.
• Performance Metrics: specify an Optim Performance Manager profile for gathering performance metrics.
The integrated toolbar at the top provides convenient links to tools such as: Visual Explain, Tuning, Import / Export,
Job Manager, etc.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 23 of 335.
IBM Software
Information Management Technology Ecosystem
Before executing the query, you can ask Data Studio to properly format your query.
3. Inside the editor, press CTRL+SHIFT+F on your keyboard, or right click within the editor and select
from the context menu. Your query should be transformed into the following format:
4. Right click at the margin of the editor and select Show Line Numbers. This feature is especially useful if you have many
lines of SQL statements.
5. Press F5 on your keyboard or click the Run SQL icon to execute your statement.
The query result is displayed in the SQL Results view. The Status tab shows the status of the query execution. The
Result tab shows actual data records returned by your query. You can also view the history of all previously executed
commands in the left panel of the “SQL Results” tab.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 24 of 335.
IBM Software
Information Management Technology Ecosystem
6. Toggle the icon at the top right of the SQL Results view to display the query result in text mode. This is especially
useful if you need to copy and paste the results to an external text editor.
7. To display query results in a single tab, i.e. not to make the execution status tab the default opening tab, toggle the
button.
8. Execute the query again by pressing button. This time you should see the results right away.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 25 of 335.
IBM Software
Information Management Technology Ecosystem
9. Toggle the icon again to turn off the text mode and display the results in the graphical tabular format.
You can use Visual Explain to view the access plan generated by DB2’s SQL compiler as a graph. You can use the information
in the graph to tune your SQL queries for better performance. The nodes in the graph represent tables and indexes and each
operation on them. The links between the nodes represent the flow of data.
1. We want to find out which department has contributed how many sales. Clear your previous SQL statements in the
editor and enter the following SQL statement.
SELECT d.deptno, d.deptname, SUM(s.sales) as total_sales
FROM sales s INNER JOIN employee e ON s.sales_person = e.lastname
INNER JOIN department d ON d.deptno = e.workdept
GROUP BY d.deptno, d.deptname
ORDER BY d.deptno;
2. To view the access plan diagram of this query, click the Open Visual Explain icon on the integrated toolbar to
open Visual Explain. In the pop-up window, keep the default values, click Finish.
3. The diagram is then displayed in the Access Plan Diagram view at the lower section in your workspace. Double-click
DB2 10 for Linux, UNIX and Windows Course Workbook Page 26 of 335.
IBM Software
Information Management Technology Ecosystem
Zoom in
Zoom out
DB2 10 for Linux, UNIX and Windows Course Workbook Page 27 of 335.
IBM Software
Information Management Technology Ecosystem
4. Minimize the Access Plan Diagram view by double-clicking again the tab.
5. In the SQL Editor, press F5 on your keyboard or click to execute this statement. The query result is displayed in the
SQL Results view.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 28 of 335.
IBM Software
Information Management Technology Ecosystem
Exporting data is a way to output data to a spreadsheet or other formats for sharing, archiving, or further calculations.
1. Export this result set by right-clicking in the Result1 tab, select Export, then select Current Result
2. Enter “/home/db2inst1/query_result” in the File Name field, and select CSV File as the Format. The file extension .csv
will automatically be appended to the file name when you select the CSV file format.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 29 of 335.
IBM Software
Information Management Technology Ecosystem
9 Summary
IBM DB2 for Linux, UNIX and Windows is an industry-leading database management system that has constantly delivered new
features to address ever-increasing demands and requirements on data. Database administrators and application developers
can reap immediate benefits from using DB2.
IBM Data Studio is a comprehensive database management tool that allows administrators and developers to effectively work
with DB2 databases.
In this lab, you have learned how to leverage Data Studio to perform basic tasks on DB2 such as:
• Establish a database connection
• Browse database objects
• Leverage task assistants to carry out administrative tasks
• Manage database changes with change plans
• Create and execute queries in SQL
• Analyze data access plan using Visual Explain
• Export query result for later use
10 Cleanup
DB2 10 for Linux, UNIX and Windows Course Workbook Page 30 of 335.
IBM Software
Information Management Technology Ecosystem
1. To clean your environment after completing the exercise, right-click on DB2FUND database in the Administration
Explorer view and select Disconnect.
2. Next, close Data Studio and switch back to the terminal window that you have been using so far.
3. Finally, execute the commands below. Use “password” as password.
su –
cd /home/db2inst1/Documents/LabScripts/Fundamentals/setup
./cleanup.sh
4. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/Fundamentals/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 31 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 32 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 33 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 About this Lab......................................................................................................................................................................................3
2.1 Environment Setup Requirements ...................................................................................................................................................................... 3
2.2 Preparing for the lab ........................................................................................................................................................................................... 3
4 Multi-temperature Storage................................................................................................................................................................14
4.1 Create Storage Groups and Table Spaces for Q1 and Q2 ............................................................................................................................... 14
4.2 Start of the New Quarter ................................................................................................................................................................................... 15
5 Summary ............................................................................................................................................................................................17
6 Cleanup...............................................................................................................................................................................................17
DB2 10 for Linux, UNIX and Windows Course Workbook Page 34 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
With data increasing at an exponential rate, management of the volumes of information in data warehouses is ever more critical.
There is a strong tendency for a relatively small proportion of data, within data warehouses, to be frequently accessed or be hot
data. Most of the data stored is infrequently access or cold data. This classification of multi-temperature data poses considerable
challenges for optimization of storage management. As the data warehouse consumes increasing amounts of storage,
optimizing the use of fast storage becomes increasingly important in managing storage costs.
The multi-temperature database storage feature in DB2 version 10 introduces a new way of prioritizing data and all activities
touching the data. It allows users to manage multiple tiers of storage devices effectively by introducing the concept of storage
groups.
Currently, you can only specify a single set of storage paths for an automatic storage managed database. This restricts the
efficient use of drives of different speed and performance for data of varying priority. Through the usage of storage groups, the
user is allowed to group together storage paths for devices with similar characteristics and can control where the data resides.
This lab illustrates the ease in administration and management of storage groups and migration to start using storage groups.
1. Start the VMware image by clicking the button in VMware Workstation if it is not already on.
2. Login to the VMware virtual machine using the following information:
• User: db2inst1
• Password: password
3. Open a terminal window by right-clicking on the Desktop area and choose the “Open in Terminal” item.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 35 of 335.
IBM Software
Information Management Technology Ecosystem
6. All scripts used during the lab reside within the LabScripts folder. Navigate to the following location:
cd /home/db2inst1/Documents/LabScripts/Multi_Temp_Storage/
A storage group is a new layer of abstraction encompassing table spaces. It acts as a named set of storage paths where data
can be stored and can be configured to represent different classes of storage. Each storage group can have its own unique
characteristics, and you can define which table spaces reside within each storage group.
In this section, we will demonstrate how to create and alter the characteristics of a storage group. To demonstrate how to create
and manage storage groups we will perform a series of operations on the database MTSDB, which we have created for your
convenience.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 36 of 335.
IBM Software
Information Management Technology Ecosystem
When you create a database, a default storage group named IBMSTOGROUP is automatically created. This storage group is
created within the storage paths that were specified at the time of the database creation. There can only be one storage group
designated as the default storage group.
NOTE: A database created with the AUTOMATIC STORAGE NO clause does not have a default storage group. The first storage
group created with the CREATE STOGROUP statement becomes the designated default storage group
2. To view the default, IBMSTOGROUP storage group, execute the following SQL statements.
db2 "SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP,
VARCHAR(DB_STORAGE_PATH, 40) AS STORAGE_PATH
FROM TABLE(ADMIN_GET_STORAGE_PATHS('',-2)) AS T"
The ADMIN_GET_STORAGE_PATHS function returns a list of automatic storage paths for each database storage
group, including file system information for each storage path. /db2fs is the storage path specified as the storage path
for the default storage group.
db2inst1@db2v10:~> db2 "SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP,
VARCHAR(DB_STORAGE_PATH, 40) AS STORAGE_PATH FROM TABLE(ADMIN_GET_STORAGE_PATHS('',-2)) AS
T"
STOGROUP STORAGE_PATH
------------------------------ ----------------------------------------
IBMSTOGROUP /home/db2inst1
1 record(s) selecte
Figure 3: storage path for the default IBMSTOGROUP
This default storage group is used when an automatic storage managed table space is created without explicitly
specifying the storage group.
3. To view the table spaces automatically created for the MTSDB database, within IBMSTOGROUP, execute the following
command. You will see that all table spaces created within the database reside in the DB2 default storage group.
db2 "SELECT varchar(tbsp_name, 30) as tbsp_name, storage_group_name FROM
TABLE(MON_GET_TABLESPACE('',-2)) AS t WHERE t.tbsp_using_auto_storage = 1 and
t.storage_group_name = 'IBMSTOGROUP'"
DB2 10 for Linux, UNIX and Windows Course Workbook Page 37 of 335.
IBM Software
Information Management Technology Ecosystem
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ --------------------------------------------------------------------------------------------------------------------------------
SYSCATSPACE IBMSTOGROUP
TEMPSPACE1 IBMSTOGROUP
USERSPACE1 IBMSTOGROUP
IBMDB2SAMPLEREL IBMSTOGROUP
IBMDB2SAMPLEXML IBMSTOGROUP
SYSTOOLSPACE IBMSTOGROUP
6 record(s) selected.
1. Create two different storage groups each with its own media attributes.
db2 "create stogroup sg_high on '/home/db2inst1/mtsdb/fastpath1' device read
rate 500 overhead 0.75"
db2 "create stogroup sg_medium on '/home/db2inst1/mtsdb/medpath1'"
When no media attributes are defined, DB2 uses the following defaults.
Media Attribute Default Setting
Data Tag NONE
Overhead 6.725 ms
Attention: as all storage paths are defined on the same machine, this lab will not show the performance aspect
of data accessed on storage groups with varying performance characteristics
2. View the newly created storage groups along with the storage paths specified.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 38 of 335.
IBM Software
Information Management Technology Ecosystem
STOGROUP STORAGE_PATH
------------------------------ ----------------------------------------
IBMSTOGROUP /home/db2inst1
SG_HIGH /home/db2inst1/mtsdb/fastpath1
SG_MEDIUM /home/db2inst1/mtsdb/medpath1
3 record(s) selected.
1. Alter the SG_HIGH storage group and assign it as the new default storage group.
db2 alter stogroup sg_high set as default
Any new automatic storage table space created will be assigned to this storage group, unless a storage group
assignment was specified, at the time of creation.
2. Create a table space without specifying a storage group. It will be placed in SG_HIGH
db2 create tablespace tbsp_test_default
3. Now check the table spaces residing within the storage group SG_HIGH using the MON_GET_TABLESPACE
administrative view. It provides table space metrics and can be used to verify the table spaces assigned to particular
storage groups.
db2 "SELECT varchar(tbsp_name, 30) as tbsp_name, storage_group_name FROM
TABLE(MON_GET_TABLESPACE('',-2)) AS t WHERE t.tbsp_using_auto_storage = 1 and
t.storage_group_name = 'SG_HIGH'"
DB2 10 for Linux, UNIX and Windows Course Workbook Page 39 of 335.
IBM Software
Information Management Technology Ecosystem
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ ------------------------------------
TBSP_TEST_DEFAULT SG_HIGH
1 record(s) selected.
In this sub-section we will demonstrate how to associate a table space to a storage group. A new table space can be assigned
to a storage group using the CREATE TABLESPACE statement or an existing table space can be associated using the ALTER
TABLESPACE statement to specify or change the storage group the table space uses.
We will create a new table space TBSPC_HIGH and assign it to the SG_HIGH storage group. This table space has the option to
inherit the media attributes from the storage group or specify its own.
In the statement above, we have chosen to inherit the media attributes of the storage group. The automatic storage
table space created, inherits the overhead and transfer rate attributes from the storage group it is assigned to. When a
table space inherits the transfer rate, the storage group’s device read rate (sg_high: 500 MB/sec) is converted to
milliseconds per page read, using the page size setting of the table space.
2. To check if this table space has been correctly assigned to the storage group SG_HIGH, execute the statement from
section 3.3, step 3. The newly added table space is shown in the result below.
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ ----------------------------------
TBSP_TEST_DEFAULT SG_HIGH
TBSPC_HIGH SG_HIGH
2 record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 40 of 335.
IBM Software
Information Management Technology Ecosystem
Pre-existing table spaces can be moved to a different storage group. In this scenario we will show you how an existing table
space can be converted to start using a new storage group. We will be converting the existing table space
IBMDB2SAMPLEXML which currently resides within the storage group; IBMSTOGROUP
1. Use the ALTER TABLESPACE statement below to move an existing table space to a different storage group.
db2 "ALTER TABLESPACE IBMDB2SAMPLEXML USING STOGROUP sg_medium"
After the ALTER TABLESPACE statement is committed, containers are allocated on the new storage group’s storage
paths, the existing containers residing in the old storage group are marked as drop pending, and an implicit
REBALANCE operation is initiated. This operation allocates containers on the new storage path and rebalances the
data from the existing containers into the new containers. Once the move is complete, the old containers are dropped.
2. To verify the move is complete, check the storage group assignment of the table space by calling the
MON_GET_TABLESPACE administrative view.
db2 "SELECT varchar(tbsp_name, 30) as tbsp_name, storage_group_name FROM
TABLE(MON_GET_TABLESPACE('',-2)) AS t WHERE t.tbsp_using_auto_storage = 1 and
t.storage_group_name = 'SG_MEDIUM' "
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ -------------------------------------------------------
IBMDB2SAMPLEXML SG_MEDIUM
1 record(s) selected.
Attention: All paths assigned to a storage group should have similar media characteristics: underlying disks,
latency, device read rate, and size. Non-uniform media characteristics may result in performance inconsistency.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 41 of 335.
IBM Software
Information Management Technology Ecosystem
After adding one or more storage paths to the storage group, you can optionally use the ALTER TABLESPACE
statement to rebalance table spaces to immediately start using the new storage paths. Otherwise, the new storage
paths are used only when there is no space in the containers on the existing storage paths.
2. To determine the table spaces which require an explicit REBALANCE, you can issue the command shown in 3.4.2 to
list the table spaces within the storage group SG_MEDIUM. You do not have to re-execute this command, as we know
that the table space IBMDB2SAMPLEXML resides within this storage group.
Rebalance the data within the table space.
db2 "ALTER TABLESPACE IBMDB2SAMPLEXML REBALANCE"
When intending to drop a storage path, you must rebalance all permanent table spaces that use the storage path. The rebalance
is needed to move data off the path to be dropped. In this situation, the rebalance operation moves data from the storage path
that you intend to drop to the remaining storage paths and keeps the data striped consistently across those storage paths,
maximizing I/O parallelism. To demonstrate this procedure, we will proceed to drop the newly added storage path.
1. To drop storage paths from a storage group, issue the following ALTER STOGROUP statement:
db2 "ALTER STOGROUP sg_medium drop '/home/db2inst1/mtsdb/medpath3'"
Figure 12: Drop-pending state for storage path after being dropped
2. The following command shows you the storage path marked as DROP_PENDING
db2 "SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP,
VARCHAR(DB_STORAGE_PATH, 40) AS STORAGE_PATH, DB_STORAGE_PATH_STATE AS STATE
FROM TABLE(ADMIN_GET_STORAGE_PATHS('',-1)) AS T WHERE T.STORAGE_GROUP_NAME =
'SG_MEDIUM'"
db2inst1@db2v10:~> db2 "SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP, VARCHAR(DB_STORAGE_PATH,
40) AS STORAGE_PATH, DB_STORAGE_PATH_STATE AS STATE FROM TABLE(ADMIN_GET_STORAGE_PATHS('',-1)) AS T
WHERE T.STORAGE_GROUP_NAME = 'SG_MEDIUM'"
3 record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 42 of 335.
IBM Software
Information Management Technology Ecosystem
3. To remove the path, rebalance the containers of the storage path being dropped. To determine all the affected
permanent table spaces in the database that have containers residing on a "Drop Pending" path, call the
MON_GET_TABLESPACE administrative view. (not shown)
Once the table spaces have been identified, you can perform the following statement for each of the table spaces listed:
db2 "ALTER TABLESPACE IBMDB2SAMPLEXML REBALANCE"
After the rebalance operation is complete, /home/db2inst1/mtsdb2/medpath3 is removed from the storage
group.
4. To confirm that the storage path was dropped, verify the assigned storage paths for storage group SG_MEDIUM
db2 "SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP,
VARCHAR(DB_STORAGE_PATH, 40) AS STORAGE_PATH FROM
TABLE(ADMIN_GET_STORAGE_PATHS('',-1)) AS T
WHERE T.STORAGE_GROUP_NAME = 'SG_MEDIUM'"
STOGROUP STORAGE_PATH
------------------------------ ----------------------------------------
SG_MEDIUM /home/db2inst1/mtsdb/medpath1
SG_MEDIUM /home/db2inst1/mtsdb/medpath2
2 record(s) selected.
1. Find the table spaces that are using the storage group. In our case we are going to drop SG_MEDIUM and it currently
holds the IBMDB2SAMPLEXML table space, as shown in the previous sections.
In the case of temporary table spaces residing within the storage group, you would be required to drop the temporary
table spaces and re-create them within the target storage group, where the data is being transferred to.
2. In earlier operations, we have not shown a way to monitor the rebalance activity for the data movement involved in
assigning a table space to a different storage group.
The next sequence of steps showcases monitoring of the rebalance activity for the table space being moved. Follow the
series of steps outlined.
a. Open a new Terminal, by Right-Clicking on the Desktop screen.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 43 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 44 of 335.
IBM Software
Information Management Technology Ecosystem
f. When you execute the REBALANCE query again, you will notice an empty state with no record. The move is
complete.
g. To verify this, you can execute the ADMIN_GET_STORAGE_PATHS command, showcased in the earlier section;
3.2 step 2. You will see that SG_MEDIUM is no longer in use. The result is shown below for your convenience.
STOGROUP STORAGE_PATH
------------------------------ ----------------------------------------
IBMSTOGROUP /home/db2inst1
SG_HIGH /home/db2inst1/mtsdb/fastpath1
SG_MEDIUM /home/db2inst1/mtsdb/medpath1
SG_MEDIUM /home/db2inst1/mtsdb/medpath2
4 record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 45 of 335.
IBM Software
Information Management Technology Ecosystem
4 Multi-temperature Storage
Having covered the basics involved in managing storage groups and table spaces within them, we proceed to a scenario where
storage of data is managed leveraging the concepts introduced in the earlier sections. We illustrate the use of storage groups
with multi-temperature data.
Assume that you are the DBA for a business that does most of its processing on current-fiscal-quarter data. This business has
enough solid-state drive (SSD) capacity to hold data for an entire quarter and enough Fiber Channel-based (FC) and Serial
Attached SCSI (SAS) drive capacity to hold data for the remainder of the year.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 46 of 335.
IBM Software
Information Management Technology Ecosystem
1. An ALTER TABLESPACE statement is used to move the existing table space q2_tbsp to SG_WARM.
db2 "ALTER TABLESPACE q2_tbsp USING STOGROUP SG_WARM"
2. To monitor the move of data, you can follow step 2 of section 3.6. The output is shown below.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 47 of 335.
IBM Software
Information Management Technology Ecosystem
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ ------------------------------------------------------------------
Q1_TBSP SG_WARM
Q2_TBSP SG_WARM
2 record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 48 of 335.
IBM Software
Information Management Technology Ecosystem
TBSP_NAME STORAGE_GROUP_NAME
------------------------------ ----------------------------------------------------------------------
----------------------------------------------------------
Q1_TBSP SG_WARM
Q2_TBSP SG_WARM
Q3_TBSP SG_HOT
3 record(s) selected.
5 Summary
The intent of this lab was to acquaint the user with the new concepts introduced in DB2 10. Multi-temperature data management
allows the user to configure the database in a manner which optimizes the use of fast storage and effectively reduces storage
costs. The addition of storage groups as sets of storage paths with similar qualities now allows the user to map different classes
of storage according to their respective performance and reliability requirements.
To further improve the performance of data, the power of data tags and the new data-centric view for workloads can be
leveraged. Different table spaces within the same storage group can be prioritized based on their data tag values.
6 Cleanup
1. To clean your environment after completing the exercise, execute the command below. Use “password” as password.
su –
cd /home/db2inst1/Documents/LabScripts/Multi_Temp_Storage/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/Multi_Temp_Storage/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 49 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 50 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 51 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 About this Lab......................................................................................................................................................................................3
3 Basic Set up and Start of Lab.............................................................................................................................................................3
3.1 Environment Setup Requirements ...................................................................................................................................................................... 3
3.2 Preparation Step ................................................................................................................................................................................................. 4
3.3 Launching Data Studio........................................................................................................................................................................................ 4
3.4 The LABDB Database......................................................................................................................................................................................... 6
4 Configuration Advisor.........................................................................................................................................................................7
4.1 Optimize Database Using Configuration Advisor................................................................................................................................................ 7
10 Summary ............................................................................................................................................................................................33
11 Cleanup...............................................................................................................................................................................................33
DB2 10 for Linux, UNIX and Windows Course Workbook Page 52 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
Today's business challenges require that databases be optimized for the best possible performance. The database engine must
be highly tuneable and easily configurable for the best performance regardless of the skill level of the DBA. To face these
challenges, DB2 comes with a rich set of autonomic features for tuning the configuration parameters adaptively and accurately.
Some of the commands and queries required to perform certain parts of certain tasks are quite long. In order to avoid having to
type these longer commands, we have provided you with scripts for some of these tasks. They are located in the
/home/db2inst1/Documents/LabScripts/Autonomics directory.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 53 of 335.
IBM Software
Information Management Technology Ecosystem
1. Start the VMware image by clicking the button in VMware Workstation if it is not already on.
2. At the login prompt, login with the following credentials:
• Username: db2inst1
• Password: password
3. Open a terminal window as follows by right-clicking on the Desktop and choosing the Open in Terminal item.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 54 of 335.
IBM Software
Information Management Technology Ecosystem
3. Data Studio should now start in the Database Administration perspective as shown below.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 55 of 335.
IBM Software
Information Management Technology Ecosystem
If you are not in Database Administration perspective, you can easily change the perspective of Data Studio by click the
Open Perspective button as shown below and selecting Database Administration from the list.
Throughout this lab we will be working with a database named LABDB that contains several table spaces and is managed by
automatic storage. We will use this database to describe all of the autonomics components described above by executing
various queries on this database throughout the lab. This database has already been set up and is ready to use.
1. Open up a new terminal window or use the terminal window from the beginning of the lab.
2. Change your current directory to the directory containing all the scripts for this lab:
cd /home/db2inst1/Documents/LabScripts/Autonomics
3. Use gedit to view the contents of the create_db_labdb.sql script by executing the following command:
gedit create_db_labdb.sql &
The following should display:
drop db labdb;
create db labdb on /db2fs/db2inst1/sp1 , /db2fs/db2inst1/sp2 using codeset UTF-8
territory US;
connect to labdb;
create schema vps;
create schema dupvps;
create bufferpool VPSBUFF immediate size automatic pagesize 16k;
create tablespace VPSLARGE pagesize 16k managed by database using (file
'/db2fs/db2inst1/tempspace' 300) bufferpool VPSBUFF AUTORESIZE YES;
create tablespace vpsspc1 pagesize 16k managed by automatic storage autoresize yes
bufferpool VPSBUFF;
create tablespace vpsspc2 pagesize 16k managed by automatic storage bufferpool
VPSBUFF;
create tablespace vpsspc3 pagesize 16k managed by automatic storage bufferpool
VPSBUFF;
update dbm cfg using SHEAPTHRES 0;
Note:
• Automatic Storage is enabled by default when creating a database unless it is explicitly turned off.
• The database manager SHEAPTHRES parameter is set to zero. This is to enable sort memory tuning
(SORTHEAP) for optimal configuration parameter settings when using the configuration advisor.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 56 of 335.
IBM Software
Information Management Technology Ecosystem
4 Configuration Advisor
DB2 configuration parameters play an important role in performance as they affect the operating characteristics of a database or
database manager. The Configuration Advisor makes recommendations on the initial settings for configuration parameters that
can be easily adopted by inexperienced administrators or fine-tuned by more experienced administrators. We will see in this
section how the Configuration Advisor (invoked via the AUTOCONFIGURE command) can generate values for various
performance critical parameters.
A good place to start with this database is to run the AUTOCONFIGURE command on a database which will recommend
enablement of the Self Tuning Memory Manager (STMM). We will be covering STMM later in this lab.
Although when a database is created, the Configuration Advisor runs automatically, explicitly invoke the Configuration Advisor
using the command below.
1. Execute the following two commands in a terminal:
db2 connect to labdb
db2 "AUTOCONFIGURE APPLY DB AND DBM"
More configuration parameters are available for the AUTOCONFIGURE command; however, by not specifying them, the
defaults are accepted. The following should output:
Former and Applied Values for Database Manager Configuration
Note: These results may vary since the system defaults are being used by the AUTOCONFIGURE statement.
Also, the above command can be executed anytime if you feel your system performance is not optimized to its
maximum capabilities.
2. Restart the instance for the changes to take affect by issuing the following commands in the terminal:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 57 of 335.
IBM Software
Information Management Technology Ecosystem
Note: We issued the first command to force/release all applications (users) off the database for proper
restart of the instance.
You can view and verify the database and database manager configuration parameters by issuing the statements GET DB
CFG and GET DBM CFG respectively.
1. In the terminal window, ensure that you are in directory /home/db2inst1/Documents/LabScripts/Autonomics, execute
script PopulateDB.sh as the following
./PopulateDB.sh
2. You will be prompted for some information regarding the size of the relational data that you would like to load. For this
particular section in the lab we will input 100MB as the amount of data that we would like to insert into our database.
Size of Relational Data (MBs):
100
After some time, the following should display, indicating that everything has been processed successfully:
- Summary -
OLTP Size: 100
XML Size: 0
User: db2inst1
Pass: password
Port: 50001
All of the above steps should complete successfully, with the [DONE] tag beside them
DB2 10 for Linux, UNIX and Windows Course Workbook Page 58 of 335.
IBM Software
Information Management Technology Ecosystem
5.1 REORGCHK
The REORGCHK command interrogates database metadata and statistics on the database to determine and report on if tables or
indexes, or both, need to be reorganized or cleaned up.
You will run the REORGCHK command twice. The first time you will use the current statistics for the database then you will supply
an option that will update the statistics before checking to see if a REORG or cleanup is recommended.
1. Run the reorgchk command to verify whether reorganizing the table is necessary.
db2 REORGCHK CURRENT STATISTICS ON TABLE vps.stock
2. Results show that the table does not require a reorg. But a reorg was being recommended for one of the indexes F4.
… …
3. The UPDATE STATISTICS parameter calls the RUNSTATS routine first to update table and index statistics, and then
uses the updated statistics to determine if table or index needs reorganization. Enter the following command on a single
line.
db2 REORGCHK UPDATE STATISTICS ON TABLE vps.stock
Both files show similar results for both commands. This is due to the fact that a small table with relatively simple
statistics has been checked for reorganization.
Note: You should note that updating the statistics at the wrong time can result in moderate to significant
resource contention on your system. An alternate approach would be to enable automatic statistics collection.
This will ensure that statistics are accurate, current and run with an internally capped limit (about 7%) on the
amount of resources it will consume. The RUNSTATS command when run on its own has an option to run with
throttled resource consumption (Utility Impact Priority). It can also employ sampling techniques to reduce its
impact while maintaining accuracy. The update statistics command will use the default setting for updating
statistics and has no throttling available, but it is a pretty fast way to update statistics for all tables or all tables
in a particular schema.
INDEX
Reorganizing indexes rebuilds the index’s leafs eliminating deleted RIDs, reclaiming space and possibly reducing the number of
levels in structure of the index.
DB2 Essential Maintenance & Practical Autonomics
© Copyright IBM Corp. 2012. All rights reserved Page 9 of 35
DB2 10 for Linux, UNIX and Windows Course Workbook Page 59 of 335.
IBM Software
Information Management Technology Ecosystem
1. Execute the following in the terminal window to reorganize all indexes for table INVENT.
TABLE
Running a reorg for a table, reconstructs, or rebuilds the rows, compacting the table, eliminating fragmented and unused space.
In addition, if your table is defined with row compression ON, you can rebuild the data compression dictionary and recompress
the data based on the new dictionary. Lastly a reorganization of a table can be used to physically sort the data according to a
specified clustering index to improve performance.
Database statistics should be gathered regularly as required and preferably in a throttled mode so as to not use excessive
resources in the process. There are other special circumstances when you may want to explicitly run an update of a table’s and
indexes’ statistics:
1. After a table had a lot of updates like a batch update for a new column.
2. A reload of data or after you reorganize the table.
You can base the statistics on a sample or a percentage of the data in a table or an index. DB2's RUNSTATS command offers
two types of sampling: Bernoulli and System. Bernoulli sampling evaluates a sample set of rows from the table. System
sampling evaluates a sample set of data pages. System sampling introduces less overhead than Bernoulli sampling but may not
produce as accurate results when your data is highly clustered since it may sample pages where data is clustered such that
statistics are skewed
1. Enter the following command in your terminal window to update STOCK table statistics on 1.5 percent of the data pages
and index statistics on 2.5 percent of the index pages. Both table data pages and index pages are sampled.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 60 of 335.
IBM Software
Information Management Technology Ecosystem
Automatic maintenance is enabled by default however, only the automatic RUNSTATS feature is ON by default.
Configure Automatic Maintenance for other maintenance activities like BACKUP, REORG, STATS_PROF and PROF_UPD by
following the sections below.
1. Go back to Data Studio and connect to the LABDB database by navigating to the Administration Explorer on the top left
hand side of Data Studio. Navigate under All Databases > localhost > 5001 to find the LABDB database. Right-click on
LABDB and click Connect.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 61 of 335.
IBM Software
Information Management Technology Ecosystem
2. After the connection has successfully completed, launch DB2 Automatic Maintenance by right-clicking on the LABDB
artefact and select Set Up and Configure > Configure Automatic Maintenance….
DB2 10 for Linux, UNIX and Windows Course Workbook Page 62 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 63 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 64 of 335.
IBM Software
Information Management Technology Ecosystem
Note: If AUTO_MAINT is turned off then all other options will become disabled. This is because this option is
necessary for the others to be active, hence the hierarchy. A similar situation occurs for the AUTO_TBL_MAINT
option and AUTO_REORG, AUTO_STATS_PROF, AUTO_PROF_UPD, AUTO_RUNSTATS, and AUTO_STMT_STATS.
By defining the online maintenance window we will be able to select a time when the maintenance activities can be performed
while the database is online. This is only used when DB2 determines that maintenance is necessary.
1. Select the Online Maintenance Window menu option to receive the following screen.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 65 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 66 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 67 of 335.
IBM Software
Information Management Technology Ecosystem
It is also possible to define an offline maintenance window to perform maintenance on the database when it goes offline.
1. Select the Offline Maintenance Window menu option to receive the following screen.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 68 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 69 of 335.
IBM Software
Information Management Technology Ecosystem
All of the selections defined in the previous sections have not yet been committed. Make sure that you are satisfied with all of
your selections and perform the following steps:
1. Click on the link located underneath the Settings header. This will allow you to see the code
that will be executed against the database.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 70 of 335.
IBM Software
Information Management Technology Ecosystem
Utility throttling regulates the performance impact of maintenance utilities so that they can run concurrently during production
periods. The throttling system ensures that the throttled utilities are run as frequently as possible without violating the impact
policy. You can throttle statistics collection, backup operations, rebalancing operations, and asynchronous index cleanups. Let’s
execute a command to limit the impact of these utilities, and view the current configuration for the LABDB database.
Specifying a util_impact_lim (impact policy) value of 10 signifies that a throttled backup invocation will not impact the
workload by more than 10 percent. The following output should display (note that this is not the entire output) the new
value:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 71 of 335.
IBM Software
Information Management Technology Ecosystem
7 Design Advisor
The task of selecting which indexes, MQTs, clustering dimensions, or database partitions to create for a complex workload can
be quite daunting. The Design Advisor identifies all of the objects needed to improve the performance of the workload.
Create Explain tables to capture access plans when the Explain facility is activated. The Explain tables must be created before
Explain can be invoked.
cd /home/db2inst1/sqllib/misc
db2 -tvf EXPLAIN.DDL
Note: DB2 must be started and a connection to the LABDB database must exist for this procedure to
execute successfully.
The Design advisor can be used to get recommendations to tune individual SQL statements or a workload of statements. The
Design Advisor analyzes a specified workload and considers factors such as the type of workload statements, the frequency
with which a particular statement occurs, and characteristics of your database to generate recommendations that minimize the
total cost to run the workload.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 72 of 335.
IBM Software
Information Management Technology Ecosystem
3. In Data Studio, click on database LABDB, from within the Administration Explorer. Then, click New on the menu bar
and select New SQL Script from within this menu.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 73 of 335.
IBM Software
Information Management Technology Ecosystem
5. After inputting the above statement, click on the Visual Explain icon from within this menu.
The Visual Explain screen lets you view the access plan for explained SQL or XQuery statements as a graph. You can
use the information available from the graph to tune your queries or create objects within your database for better
performance.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 74 of 335.
IBM Software
Information Management Technology Ecosystem
Accept the defaults in the pop-up by clicking Finish. A diagram like the following should appear in the Access Plan
Diagram view at the lower half of your Data Studio workspace. Click node RETURN in the graph to reveal the
cumulative total cost of the access plan.
We can see from the graph that a table scan on DUPDEALCUST is done. We can now invoke the Design Advisor to
see if there are any suggestions that can help us improve this query plan.
6. Invoke the Design advisor by executing the following command in the terminal window:
db2advis -d labdb -s "SELECT c_first, d_name FROM dupvps.dupdealcust,
dupvps.dupcustomer, dupvps.dupdealer WHERE f_c_id=c_id AND f_d_id=d_id AND
d_id=548"
Design Advisor is used to suggest the necessary things to help us improve performance. An output similar to the
following should display:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 75 of 335.
IBM Software
Information Management Technology Ecosystem
…
Optimization finished.
3 indexes in current solution
[641.0000] timerons (without recommendations)
[428.0000] timerons (with current solution)
[33.23%] improvement
--
--
-- LIST OF RECOMMENDED INDEXES
-- ===========================
-- index[1], 0.884MB
CREATE INDEX "DB2INST1"."IDX1112072206560" ON "DUPVPS "."DUPDEALCUST"
("F_D_ID" ASC, "F_C_ID" ASC) ALLOW REVERSE SCANS COLLECT SAMPLED DETAILED STATISTICS;
COMMIT WORK ;
-- index[2], 0.013MB
CREATE INDEX "DB2INST1"."IDX1112072206490" ON "DUPVPS "."DUPDEALER"
("D_ID" ASC, "D_NAME" DESC) ALLOW REVERSE SCANS COLLECT SAMPLED DETAILED STATISTICS;
COMMIT WORK ;
…
We will now create the recommended indexes and confirm the improvement in the query plan by executing the
crt_index_recmd.sql script.
7. Execute the crt_index_recmd.sql script in DB2 by issuing the following command:
db2 –tvf crt_index_recmd.sql
8. View the new execution plan for the query by going through steps 3-5.
You can the see the query plan is better since DB2 has avoided a table scan and the total cost, timerons, has
significantly decreased.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 76 of 335.
IBM Software
Information Management Technology Ecosystem
7.3 Executing the Query and Seeing Buffer Pool Memory Changes
Now that we have optimized the database for that particular query, let’s execute this query and use memory tracker to see what
happens with the buffer pools and all other memory components as this query is executed.
Let’s start by decreasing the current size of the buffer pool to something very small (1.3 MB).
1. Execute the command below to change the buffer pool size and immediately take snapshot of the memory.
db2 alter bufferpool vpsbuff immediate size 50 automatic; db2mtrk –d >
bfr_query.snap;
Note: db2mtrk is a memory tracker command used to provide complete report of memory status, for
instances, databases, agents, and applications.
bph (S32K) bph (S16K) bph (S8K) bph (S4K) shsorth lockh
832.0K 576.0K 448.0K 384.0K 192.0K 56.9M
dbh apph (711) apph (465) apph (462) apph (461) apph (460)
58.7M 128.0K 192.0K 64.0K 64.0K 64.0K
Note: This is system dependent and therefore your values may be slightly different.
BPH (2) should be the VPSBUFF buffer pool associated with the LABDB database. We can verify this by executing
SELECT * FROM SYSCAT.BUFFERPOOLS which provides the ID of each buffer pool.
During creation we used the SIZE AUTOMATIC clause for the buffer pool meaning that it is allowed to expand if needed.
Let’s see how this buffer pool will expand when we run the select query from the pervious section.
3. Execute the query by issuing the following command from the terminal:
db2 "SELECT c_first, d_name FROM dupvps.dupdealcust, dupvps.dupcustomer,
dupvps.dupdealer WHERE f_c_id=c_id AND f_d_id=d_id AND d_id=548"
This query should return all of the records with these specifications.
4. We can now check how the BP memory allocation has changed during this query:
db2mtrk -d > after_query.snap
DB2 10 for Linux, UNIX and Windows Course Workbook Page 77 of 335.
IBM Software
Information Management Technology Ecosystem
5. Use gedit to view the after_query.snap file. Something similar to the following output should display:
bph (S32K) bph (S16K) bph (S8K) bph (S4K) shsorth lockh
832.0K 576.0K 448.0K 384.0K 192.0K 56.9M
dbh apph (758) apph (465) apph (462) apph (461) apph (460)
58.7M 128.0K 192.0K 64.0K 64.0K 64.0K
Therefore, it is evident that this buffer pool has increased in size to accommodate the query that was being performed.
STMM can be enabled for all of the major memory consumers within DB2 or it can be individually enabled for each of the
following memory consumers:
• Buffer pools (controlled by the ALTER BUFFERPOOL and CREATE BUFFERPOOL statements)
• Package cache (controlled by the pckcachesz configuration parameter)
• Locking memory (controlled by the locklist and maxlocks configuration parameters)
• Sort memory (controlled by the sheapthres_shr and the sortheap configuration parameters)
• Database shared memory (controlled by the database_memory configuration parameter)
In this section of the lab, we will enable STMM for the LABDB database and explore a few of the related database configuration
parameters.
8.1 Connect to the LABDB Database and Examine the Database Configuration Parameters
DB2 10 for Linux, UNIX and Windows Course Workbook Page 78 of 335.
IBM Software
Information Management Technology Ecosystem
Database territory = US
Database code page = 1208
Database code set = UTF-8
Database country/region code = 1
Database collating sequence = IDENTITY IDENTITY
Alternate collating sequence (ALT_COLLATE) =
Number compatibility = OFF
Varchar2 compatibility = OFF
Date compatibility = OFF
Database page size = 4096 4096
… … … … …
--More--
Note:
• Description: Description of the database configuration parameter.
• Parameter: Name of database parameter.
• Current Value: Current value of the database parameter that is active.
• Delayed Value: Delayed value of the database parameter which will be applied the next time the instance is
started.
4. Press the space bar to scroll down until you see the section regarding STMM similar to the below screen.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 79 of 335.
IBM Software
Information Management Technology Ecosystem
Note: STMM is already enabled for the LABDB database. This is because the following are enabled by default
during database creation:
• The value of the SELF_TUNING_MEM database parameter is ON (Active) which means STMM is enabled.
• Notice that the value of the DATABASE_MEMORY database parameter is AUTOMATIC. STMM can dynamically
ask for and give back memory to the operating system.
• Notice that the memory consumers that we have enabled for STMM all shows database parameter of value
AUTOMATIC.
5. Enter “q” to exit this view or hit the space bar until you have scrolled through the output on screen.
To enable self tuning memory for buffer pools, you have to set the buffer pool size to AUTOMATIC. You can do this
using the ALTER BUFFER POOL statement for existing buffer pools or the CREATE BUFFER POOL statement for new
buffer pools. In this lab, we’ve already seen the automatic buffer pool increase in size when we used the db2mtrk
command to check the memory status. This was seen in the previous section.
Note:
Changes resulting from self-tuning operations are recorded in memory tuning log files that are located in the
stmmlog subdirectory. These log files contain summaries of the resource demands from each memory consumer
during specific tuning intervals, which are determined by timestamps in the log entries.
The export utility extracts data using an SQL select or an XQuery statement, and places that information into a file. You can use
the output file to move data for a future import or load operation or to make the data accessible for analysis.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 80 of 335.
IBM Software
Information Management Technology Ecosystem
3. You can look into car.txt for all exported records by using gedit or msgs.txt for any warning message. Close the gedit
editor after you have finished viewing the files.
The IMPORT utility populates a table, typed table, or view with data using an SQL INSERT statement. If the table or view
receiving the imported data already contains data, the input data can either replace or be appended to the existing data. The
IMPORT utility inserts data from an external file with a supported file format into a table, hierarchy, view or nickname. Like export,
import is a relatively simple data movement utility. It can be activated by issuing CLP commands, by calling the ADMIN_CMD
stored procedure, or by calling its API, db2Import, through a user application.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 81 of 335.
IBM Software
Information Management Technology Ecosystem
Note:
ALLOW WRITE ACCESS: runs import in the online mode. An intent exclusive (IX) lock on the target table is acquired
when the first row is inserted. This allows concurrent readers and writers to access table dat.
COMMITCOUNT: performs a COMMIT after every n records are imported. When a number n is specified, import performs
a COMMIT after every n records are imported.
A message similar to the following should be displayed after successful execution of the command:
… …
SQL3149N "51" rows were processed from the input file. "51" rows were successfully
inserted into the table. "0" rows were rejected.
The LOAD utility is best suited to situations where performance is your primary concern. It is faster then the IMPORT utility
because it writes formatted pages directly into the database rather than using SQL INSERTS. The load utility also allows you the
option to not log the transactions. Load operations can fully exploit resources such as memory and multiple processors.
The LOAD, like the EXPORT and IMPORT utilities, is cross platform compatible. This utility can be used as an alternative to the
import utility. Use the LOAD utility if you want to move large quantities of data into empty tables or tables that already contain
data.
To demonstrate how the LOAD utility is used, you’ll use the utility to import data into the database.
1. In terminal, start the LOAD utility by executing the command below. Note that the LOAD command should be entered
on a single line.
db2 connect to labdb
cd /home/db2inst1/Documents/LabScripts/Autonomics/input
db2 load from order2.txt of del savecount 500 messages ../output/load.msg
insert into vps.order cpu_parallelism 2;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 82 of 335.
IBM Software
Information Management Technology Ecosystem
The table was placed in the Set Integrity Pending state at the beginning of a load operation. After the load operation is
complete, the SET INTEGRITY statement must be used to take the table out of Set Integrity Pending state.
2. In terminal, execute the command below to bring the table out of set integrity pending state.
db2 SET INTEGRITY FOR VPS.ORDER IMMEDIATE CHECKED;
The ingest utility (sometimes referred to as continuous data ingest, or CDI) is a high-speed client-side DB2 utility that streams
data from files and pipes into DB2 target tables. Because the ingest utility can move large amounts of real-time data without
locking the target table, you do not need to choose between the data currency and availability.
The ingest utility ingests pre-processed data directly or from files output by ETL tools or other means. It can run continually and
thus it can process a continuous data stream through pipes. The data is ingested at speeds that are high enough to populate
even large databases in partitioned database environments.
An INGEST command updates the target table with low latency in a single step. The ingest utility uses row locking, so it has
minimal interference with other user activities on the same table.
10 Summary
You can see by now that DB2’s autonomic features help to make database administration as easy and low-cost as possible. By
automating tasks such as memory allocation (Self Tuning Memory), storage management (Automatic Maintenance and
Automatic storage features) and business policy maintenance, DB2 is able to perform many management tasks itself, freeing up
DBAs to focus on new projects.
11 Cleanup
1. To clean your environment after completing the exercise, right-click on LABDB database in the Administration
Explorer view and select Disconnect.
2. Next, close Data Studio and switch back to the terminal window that you have been using so far.
3. Finally, execute the commands below. Use “password” as password.
db2 force application all
DB2 10 for Linux, UNIX and Windows Course Workbook Page 83 of 335.
IBM Software
Information Management Technology Ecosystem
su -
cd /home/db2inst1/Documents/LabScripts/Autonomics/setup
./cleanup.sh
4. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/Autonomics/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 84 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 85 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 86 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 Basic Set up and Start of Lab.............................................................................................................................................................3
2.1 Environment Setup Requirements ...................................................................................................................................................................... 3
2.2 Preparation Step ................................................................................................................................................................................................. 3
2.3 Launching Data Studio........................................................................................................................................................................................ 4
2.4 Preparing the Database ...................................................................................................................................................................................... 6
DB2 10 for Linux, UNIX and Windows Course Workbook Page 87 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
An effective backup and recovery solution is critical for organizations to be protected against hardware/ software failures. Power
interruptions, storage problems, or application failures can render a database useless, requiring a different recovery action for
each of these failure scenarios. DB2 for LUW has a rich set of flexible backup and recovery features that make it easy for the
user to backup and recover their data.
Note: You many skip this section if you have already started your VM and DB2 instance in a previous lab
session.
1. Start the VMware image by clicking the button in VMware Workstation if it is not already on.
2. At the login prompt, login with the following credentials:
• Username: db2inst1
• Password: password
3. Open a terminal window as follows by right-clicking on the Desktop and choosing the Open in Terminal item.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 88 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 89 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 90 of 335.
IBM Software
Information Management Technology Ecosystem
4. From Data Studio, under the Database Administration perspective and within the Administration Explorer, expand
the nodes All Databases > localhost > db2inst1. Right click on the DS2 database and Connect.
In this lab, you will be working with the DS2 database. It has three user defined table spaces. The following table provides the
relationship between the table space and tables they contain. All tables are created under the DS2 schema.
You have the flexibility to decide where to store these objects, for how long, etc. Therefore, one of the first things you should do
before taking any backups is to update database configuration to match your needs. In this section we will look at important
database recovery parameters and configure them appropriately.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 91 of 335.
IBM Software
Information Management Technology Ecosystem
4.2.1 LOGFILSIZ
It is the size of each transactional log file and it is given in 4KB pages. The default size is 1000 pages or 3.9 MB, which implies it
can hold up to 3.9 MB of transactional data.
For the purposes of this exercise, make it smaller because transaction rate is low.
Note: SQL 1363W is returned on issuing the command; it indicates that the database needs to be reactivated
for the parameter change to take effect.
This message will be returned for most of the commands in this section. The last step in this section is to restart the
database for all the configuration changes to take effect.
LOGPRIMARY is the number of primary log files. By default LOGPRIMARY is 3, therefore if you have 3 log files worth of
uncommitted transactions, any new transactions would start utilizing the secondary log files.
LOGSECOND is the number of secondary log files. You can have this configured to accommodate spikes in transactional
activity. By default LOGSECOND is 2 and it has a special setting of -1 which indicates infinite logging space, more on that later.
Set LOGPRIMARY to 6 and LOGSECOND to 2.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 92 of 335.
IBM Software
Information Management Technology Ecosystem
4.2.3 LOGBUFSZ
It is the memory buffer for log records. This is where all log records get written in memory before getting flushed to disk. Default
of 256 (4KB) pages is small for most scenarios. Set the LOGBUFSZ to 512.
4.2.4 NEWLOGPATH
Log path is a directory location on the server where the log files get created. By default it is in the same location as the database
control files.
By default, when a new database is created; it is configured for circular logging. Circular logging causes log files to be reused in
a circular fashion with no history of log files preserved. For example, if you have LOGPRIMARY set to 5, after the 5th file is used
up, DB2 will start reusing the first log file, considering it is available. With circular logging only crash recovery and offline
backups are possible. In order to take online backups and perform roll-forward recovery, the database needs to be enabled for
archival logging.
1. Enable database for archival logging and provide an archival location by issuing the following commands from the
terminal window:
This enables the database for archival logging and at the same time sets the archival location. We specified a disk
location but it could be TSM server or another vendor API library.
LOGARCHMETH2 is a secondary log archival method. If this path is specified, log files will be archived to both this destination
and the destination specified by LOGARCHMETH1. FAILARCHPATH is yet another layer of protection. If both archival methods
fail, database will try to archive to this location. Leave these two parameters blank for this exercise.
1. Issue the following commands from Linux terminal to shutdown and restart the database:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 93 of 335.
IBM Software
Information Management Technology Ecosystem
2. From Data Studio, under the Database Administration perspective and within the Administration Explorer, expand
the nodes All Databases > localhost > db2inst1. Right click on the DS2 database and Connect.
This message is received because archival logging has just been enabled, which by design puts the database in
BACKUP PENDING mode. This mode indicates that you need to do a full backup of the database before accessing it.
Recall that once archival logging is enabled for the database, roll forward recoveries can be performed. However, roll
DB2 10 for Linux, UNIX and Windows Course Workbook Page 94 of 335.
IBM Software
Information Management Technology Ecosystem
forward recovery can only be performed once a backup image has been restored and database is placed in Roll
Forward Pending status. For this reason, a backup operation is imposed after setting archival logging.
3. Right click on the DS2 database, Back Up and Restore > Back Up….
4. Within the Back up DS2 tab, select the Backup Image settings, and click Add… to specify a backup location.
5. Select the newly added backup location as shown above and click the Browse… button. Navigate to
/home/db2inst1/backups and press Ok to select this location.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 95 of 335.
IBM Software
Information Management Technology Ecosystem
6. Click on . This will allow you to see the command to be executed. You might have to expand the
task assistant window to view the command displayed at the bottom.
7. Click Run button to execute the command. You will be requested to provide connection details for DS2.
8. The results of the backup command are provided in the SQL results pane at the bottom right of the screen.
Every time there is a successful backup in DB2, a backup image timestamp is also returned. The Restore utility uses this
timestamp to differentiate between multiple available backup images.
Note: Timestamp is dumped in the following format: yyyymmddhhmmss. Note the timestamp for the backup as T1
We have already taken a full offline backup in previous section. Now we’ll take a full online backup of database DS2.
1. Go back to the terminal and connect to the DS2 database and verify there is a connection to the database by issuing
the following commands:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 96 of 335.
IBM Software
Information Management Technology Ecosystem
2. Now try to perform a back up using Data Studio in the same way as we did in the previous section. Then select a
location for the backup (as shown below) and click Run.
The following error should be returned. The command failed as an offline backup is being performed while the database
is online due to the connection in the terminal.
3. To take an online backup, first connect to the DS2 database from Data Studio (Right click DS2 > Connect)
4. Open the Back Up task assistant by following the directions outlined in step 2.
5. Repeat the same procedure as before to add the backup location: within the Back up DS2 tab.
6. Also, under the Backup Options settings, verify that Online is selected within the Availability section.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 97 of 335.
IBM Software
Information Management Technology Ecosystem
7. Click on the . This will allow you to see the command to be executed.
Backup attempt should now be successful. Note the timestamp, it will be referred to as T2 in the next section.
In this section, we will demonstrate the ease of restoring your database from a backup.
1. Go back to the terminal, issue the following command to close all database connections:
Make sure you are still connected to the DS2 database through Data Studio. Open the Restore task assistant by right
clicking on the DS2 database, Back Up and Restore > Restore….
DB2 10 for Linux, UNIX and Windows Course Workbook Page 98 of 335.
IBM Software
Information Management Technology Ecosystem
2. Under the Restore Objects settings, navigate to the Backup image section, and select the backup image to restore
which corresponds to timestamp T2. T2 is the timestamp for the online backup taken in previous section.
3. Click on the . This will allow you to see the command to be executed.
5. The deployment status of the command is displayed in the Restore Database DS2 tab.
a. The command has been executed successfully but with a warning. This warning is ok; essentially it is stating
that you are overwriting an existing database.
b. The attempt to connect to the database fails. If we try connecting again (through Data Studio), we receive the
following message:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 99 of 335.
IBM Software
Information Management Technology Ecosystem
When an online backup is taken, any activity that may be going on during the online backup is recorded into the
database transaction logs. Therefore, once you restore from an online backup there is a mandatory step needed to do a
Roll Forward operation to a minimum Point in Time before the database can be made available.
6. In Data Studio, open the Roll Forward task assistant by right clicking on the DS2 database, Back Up and Restore >
Roll Forward….
7. Under the Roll-forward Final State settings, within the Final state options section, select Complete the roll-forward
operation and return to the active state.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 100 of 335.
IBM Software
Information Management Technology Ecosystem
8. Click on . The COMPLETE keyword at the end of ROLLFORWARD command indicates that
once all available logs have been applied the database should be made available for user connections.
9. Click the Run button to execute the command. You will be prompted to connect to the database, enter database
password.
Rollforward Status
Input database alias = DS2
Number of members have returned status = 1
Member ID = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = S0000000.LOG - S0000000.LOG
Last committed transaction = 2011-12-22-22.51.06.000000 UTC
DB20000I The ROLLFORWARD command completed successfully.
Note: Rollforward status is set to NOT PENDING, meaning that database has been taken out of roll forward pending
state and it is now connectable.
Restoring from an offline backup is simpler, as there is no hard requirement to perform a roll forward but you can do one if
needed. Let us restore the database from an offline backup image taken at T1. If you do not recall, you can always check the
Recovery history file to find the backup timestamp.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 101 of 335.
IBM Software
Information Management Technology Ecosystem
00001 SYSCATSPACE
00002 USERSPACE1
00003 CUSTTBS
00004 MISCTBS
00005 ORDERTBS
00006 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: DB2 BACKUP DS2 OFFLINE
Start Time: 20111222174229
End Time: 20111222174231
Status: A
----------------------------------------------------------------------------
EID: 10 Location: /home/db2inst1/backups
Note “Timestamp” for the backup image and use it in restore operation as follows:
2. Connect to the DS2 database from Data Studio and open the Restore Database task assistant
3. Within the Restore Database tab, navigate to the Restore Objects settings. From the Backup image section, select
the offline backup image which corresponds to timestamp T1.
4. Under the Restore Options settings, within the Roll-forward options, select the check box “Do not put the database
in roll forward pending state after restore”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 102 of 335.
IBM Software
Information Management Technology Ecosystem
5. Click on the . This will allow you to see the command to be executed.
Note: When restoring an offline backup image, a roll forward is not mandatory. Therefore, the clause WITHOUT ROLLING
FORWARD is allowed in the restore command. This is useful when a roll forward is not needed and restore can finish in just
one step. After restore finishes, you are able to connect without having to do a roll forward explicitly.
We briefly touched upon the Roll-forward task assistant in the last section. Roll-forward is the process of applying transaction log
files after a restore has been performed. For example, last backup was taken Sunday and the database was lost on the following
Tuesday. Once the backup from Sunday is restored, transactions in log files need to be applied in order to recover transactions
that were done after the backup. This is achieved by rolling forward to “END OF LOGS”.
There might be a situation where it is not desired to apply all the transactions. For example, a large set of records are deleted
from the database mistakenly by the user. In such a case, in order to recover all the deleted records, rolling forward to a POINT
IN TIME before the deletions took place would be more appropriate.
1. Create an online database backup exactly as we did in the previously using the same backup location. Refer to section
5.1 if needed.
2. Note the time stamp returned by the backup command. It will be needed for the restore operation later on. We’ll refer to
this timestamp as T3.
3. Go back to the terminal window and check the number of rows in the ORDERS table:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 103 of 335.
IBM Software
Information Management Technology Ecosystem
4. Run “date” command and note the time stamp before issuing a delete statement, it will be needed for point in time
recovery. This timestamp will be referred to as T4 within this section. Issue the following command:
date
5. Now run the following commands to delete some of the data in the ORDERS table:
Imagine these rows were deleted mistakenly and they need to be recovered. This can be done by restoring from the
backup image taken at T3.
8. Make sure you are still connected to the DS2 database through Data Studio and than open the Restore task assistant
by right clicking on the DS2 database, Back Up and Restore > Restore….
9. Under the Restore Objects settings, within the Backup image section, select the backup image to restore which
corresponds to timestamp T3. T3 is the timestamp of the online backup taken previously in this section:
10. Click on the . This will allow you to see the command to be executed.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 104 of 335.
IBM Software
Information Management Technology Ecosystem
Now that the database is restored, roll forward to a point in time before the delete statement was issued, which in this case
is T4.
12. Open the Roll Forward task assistant by right clicking on the DS2 database, Back Up and Restore > Roll Forward….
13. Under the Roll-forward Type settings, within the Type of roll-forward operation section, select A point in time
specified in local time.
14. Also, under the Restore Objects settings, within the Backup image section, specify the transaction time which
corresponds to timestamp T4.
15. Click on . This will allow you to see the command to be executed.
Note: timestamp for roll forward has to be provided in this format: yyyy-mm-dd-hh.mm.ss.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 105 of 335.
IBM Software
Information Management Technology Ecosystem
Rollforward Status
Input database alias = DS2
Number of members have returned status = 1
Member ID = 0
Rollforward status = DB working
Next log file to be read = S0000005.LOG
Log files processed = S0000000.LOG - S0000004.LOG
Last committed transaction = 2011-12-23-10.50.43.000000 Local
DB20000I The ROLLFORWARD command completed successfully.
17. Open the Roll-forward task assistant by right clicking on the DS2 database, Back Up and Restore > Complete Roll
Forward….
18. Leave the defaults and click . This will allow you to see the command to be executed.
CONNECT RESET;
ROLLFORWARD DATABASE DS2 COMPLETE ;
20. Go back to the terminal and check the number of rows in the ORDERS table again:
The expected number of rows in the ORDERS table is 12000, which means the deleted rows have been recovered.
This was possible because the roll-forward was done up to a time before the delete statement was issued, thus all
transactions since the backup was taken were applied except the delete statements in question
DB2 10 for Linux, UNIX and Windows Course Workbook Page 106 of 335.
IBM Software
Information Management Technology Ecosystem
If an “end of logs” roll forward was done in this case, it would have also replayed the delete statement, thereby deleting
the rows again.
db2 terminate
Backups can be performed at either the database level or at the table space level. In large environments, it may not be desirable
to perform a full database backup. You may have some table spaces where data is static and other table spaces where data
changes regularly. With the table space backups you can backup only subsets of a database. If there is a requirement to only
backup tables individually, it can be achieved by placing only 1 table per table space and then backing up the table spaces.
We take the same scenario in the last section of recovering deletes, however in this case table space backups will be used to
perform recovery. Take a table space backup of ORDERTBS table space. This table space contains the ORDERS table. Do the
following to verify:
1. Connect to the DS2 database from Data Studio. Under the DS2 database node within Administration Explorer, click on
Tables as shown:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 107 of 335.
IBM Software
Information Management Technology Ecosystem
2. Than within the DS2 tab check the Regular Tablespace column corresponds to table ORDERS to verify the associated
table space:
3. Connect to the DS2 database from Data Studio and open the Backup task assistant (Right click DS2 > Back Up and
Restore > Back Up…).
4. Under the Backup Type settings, within the Type of backup section, select the Back up selected table spaces radio
button.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 108 of 335.
IBM Software
Information Management Technology Ecosystem
5. Also under the Backup Type settings, within the Table spaces section, select the ORDERTBS table space.
6. Within the Back up DS2 tab, select the Backup Image settings, and click Add… to specify a backup location.
10. Go back to the terminal window and list directory for the backups:
ls –ltrh ~/backups
total 550M
-rw------- 1 db2inst1 db2grp1 183M 2011-12-22 17:42 DS2.0.db2inst1.DBPART000.20111222174229.001
-rw------- 1 db2inst1 db2grp1 177M 2011-12-22 17:51 DS2.0.db2inst1.DBPART000.20111222175105.001
-rw------- 1 db2inst1 db2grp1 181M 2011-12-23 10:50 DS2.0.db2inst1.DBPART000.20111223105040.001
-rw------- 1 db2inst1 db2grp1 8.6M 2011-12-26 20:00 DS2.3.db2inst1.DBPART000.20111226200017.001
DB2 10 for Linux, UNIX and Windows Course Workbook Page 109 of 335.
IBM Software
Information Management Technology Ecosystem
From the output two things should be noted. First, the latest backup image is smaller compared to the others since it
contains only the data for ORDERTBS table space. Second, the backup type in the file name is 3 instead of 0 like the
others: DS2.3.db2inst1.DBPART000.<timestamp>.001
12. Run date command and note the time stamp before issuing the delete statement, it will be needed for point in time
recovery. It will be referred to as T6.
date
13. Run the following commands to delete some of the data in the ORDERS table:
Instead of restoring and disrupting the entire database, we can restore only the ORDERTBS table space backup taken
at T5.
db2 terminate
The transactions carried out after the backup require a roll forward to be performed on the table space after the restore
operation is carried out. This sub-section shows how a restore and roll forward are carried out on the table space
ORDERTBS
DB2 Backup & Recovery
© Copyright IBM Corp. 2012. All rights reserved Page 25 of 38
DB2 10 for Linux, UNIX and Windows Course Workbook Page 110 of 335.
IBM Software
Information Management Technology Ecosystem
1. Connect to the DS2 database from Data Studio and open the Restore task assistant.
2. Under the Restore Objects settings, within the Restore objects section, select the Restore the selected table
spaces radio button and than select ORDERTBS from the Table spaces list.
3. Also under the Restore Objects settings, within the Backup image section, select the backup image which
corresponds to timestamp T5.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 111 of 335.
IBM Software
Information Management Technology Ecosystem
6. After table space restore, database is available but ORDERTBS table space is in roll forward pending. To demonstrate
this, issue the following command:
Name = ORDERTBS
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x0080
Detailed explanation:
Roll forward pending
Roll forward the ORDERTBS table space to timestamp T6 which was prior to the time the delete statement was issued:
7. Open the Roll Forward task assistant by right clicking on the DS2 database, Back Up and Restore > Roll Forward….
8. Under the Roll-forward Type settings, within the Type of roll-forward operation section, select A point in time
specified in local time.
9. Also, under the Restore Objects settings, within the Backup image section, specify the transaction time which
corresponds to timestamp T6.
10. Under the Roll-forward Scope settings, select the Roll the selected table spaces forward radio button and select the
ORDERTBS table space.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 112 of 335.
IBM Software
Information Management Technology Ecosystem
11. Under the Roll-forward Final State settings, within the Final state options section, select Complete the roll-forward
operation and return to the active state.
12. Click on the . This will allow you to see the command to be executed.
ROLLFORWARD DATABASE DS2 TO <T6> USING LOCAL TIME AND COMPLETE TABLESPACE (
ORDERTBS ) ONLINE;
Since the roll-forward operation was performed until a point in time before the delete operation had taken place, deleted rows
should have been recovered. Check the count:
db2 terminate
DB2 10 for Linux, UNIX and Windows Course Workbook Page 113 of 335.
IBM Software
Information Management Technology Ecosystem
So far we have looked at full database and table space backups. As database sizes grow larger it can be quite costly to run full
backups, both in terms of storage for the backup images and time required to execute the backups. This is where incremental
backups come in.
In order to use the incremental backup functionality, database has to be enabled for it. This is done by turning the TRACKMOD
database configuration parameter on. When TRACKMOD is turned on, database keeps track of table spaces that have been
modified. When an incremental backup command is issued, it will skip the table spaces that have not been modified since last
backup.
1. From Data Studio, right click on the (connected) DS2 database, Set Up and Configure > Configure….
2. Under the Configure Parameters settings, use the Filter to search for the parameter TRACKMOD. Double click on the
parameter and use the drop down menu to change the Pending Value to ON.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 114 of 335.
IBM Software
Information Management Technology Ecosystem
3. Click on the . This will allow you to see the command to be executed.
CONNECT TO DS2;
UPDATE DB CFG USING TRACKMOD ON DEFERRED ;
CONNECT RESET;
5. Disconnect from the database and re-connect to perform a full backup. Incremental backups require a full backup to act
as a reference point for incremental changes.
7. Click on . This will allow you to see the command to be executed. Click the Run button to
execute the command.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 115 of 335.
IBM Software
Information Management Technology Ecosystem
9. Make changes to ORDERS which resides in ORDERTBS and CUSTOMERS table which resides in CUSTTBS. From
terminal window run:
10. With the database enabled for incremental backups, an incremental backup can be now taken to just include the
changes made. Ensure you are connected to the DS2 database from Data Studio
12. Click on the . This will allow you to see the command to be executed. Click the Run button to
execute the command.
14. Check the backups directory for size of this incremental backup image. Issue this command:
ls –ltrh ~/backups
…
-rw------- 1 db2inst1 db2grp1 84M 2011-12-28 10:08 DS2.0.db2inst1.DBPART000.20111228100849.001
15. Image backup size is much smaller than prior full backups because it contains only the changes since last full backup.
Note that any table space that was not modified since the last full backup, will be skipped in incremental database
backups. In this case it only backed up changes in the two table spaces ORDERTBS and CUSTTBS.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 116 of 335.
IBM Software
Information Management Technology Ecosystem
Incremental backups can also be taken at table space level. Since the only change in the database was against the
ORDERS table in ORDERTBS, let’s back up the ORDERTBS table space.
Note: Recall that incremental backup backs up all changes since the last full backup. With the incremental delta
option, only changes since the last backup (either incremental or full) will be backed up.
16. Ensure you are connected to the DS2 database from Data Studio and open the Back Up task assistant:
a. Under Backup Type, within the Type of backup section, select Back up selected table spaces radio button.
b. In the table spaces section, select the ORDERTBS table space.
c. Under the Backup Image settings, specify a backup location.
d. Under the Backup Options settings, within the Backup type section, select the Delta radio button to back up
data that has changed since the most recent backup:
17. Click on . This will allow you to see the command to be executed. Click the Run button to
execute the command.
18. Run more updates against the CUSTOMERS table, from terminal session issue:
19. Take incremental backup of CUSTTBS table space. Ensure you are connected to the DS2 database from Data Studio
and open the Back Up task assistant
a. Under the Backup Type settings, select the Back up selected table spaces radio button. Then within the
Table spaces section, select the CUSTTBS table space.
b. Select a backup location from the Backup Image settings
c. Backup Options: Select Incremental from the Backup type section.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 117 of 335.
IBM Software
Information Management Technology Ecosystem
20. Click on the . This will allow you to see the command to be executed, and click RUN.
Note: we only took an incremental backup and not an incremental delta backup; this would cover all the changes made
since the full database backup in CUSTTBS table space.
When restoring from incremental backups, the right sequence of full, incremental and incremental delta backups has to be
applied. This can become very complex very quickly in a real environment. For this reason, there is an AUTOMATIC option
available with the restore command such that DB2 figures out the right sequence for applying backups and then applies them.
There is also a MANUAL option available, but AUTOMATIC option is highly recommended.
1. Restore ORDERTBS table space using the incremental backups up to the last image. “db2ckrst” utility is available to
see what backup images will be applied, prior to actually running the automatic restore.
This output is showing that last incremental delta image will be read first to get the control and header information only (T9).
Then the table space ORDERTBS will be restored from the full backup image (T7). Following, that first incremental image will be
applied (T8). Lastly, the incremental delta image will be read again, this time applying the data in the delta image (T9)..
Note: The backup image that is being restored to is read twice; once in the beginning to obtain control information and
decide how far the restore has to go and then again at the end to apply the data in last image.
2. Now, let’s restore ORDERTBS to the last delta backup image. Make sure you are still connected to the DS2 database
through Data Studio and than open the Restore task
DB2 10 for Linux, UNIX and Windows Course Workbook Page 118 of 335.
IBM Software
Information Management Technology Ecosystem
a. Under the Restore Objects settings, select the Restore the select table spaces radio button than select
ORDERTBS from the Table spaces list.
b. Also under the Restore Objects settings, within the Backup image section, select the backup image to restore
which corresponds to timestamp T9.
c. Under the Restore Options settings, select the Allow other applications to connect to the database radio
button, as shown below.
3. Click on the . This will allow you to see the command to be executed and click RUN.
ORDERTBS will now be in roll forward pending because it has been restored. Take it out of RF pending:
4. Open the Roll Forward task assistant by right clicking on the DS2 database, Back Up and Restore > Roll Forward….
5. Under the Roll-forward Scope settings, select the Roll the selected table spaces forward radio button and select the
ORDERTBS table space.
6. Click on the . This will allow you to see the command to be executed and click RUN.
7. ORDERTBS should now be available and online. Verify using following commands:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 119 of 335.
IBM Software
Information Management Technology Ecosystem
1. Make sure you are still connected to the DS2 database through Data Studio and than open the Restore task assistant
a. Within Restore Objects settings, select the Restore the select table spaces radio button than select
CUSTTBS.
b. Within the Backup image section, select the backup image to restore which corresponds to timestamp T10.
c. Under the Restore Options settings, select the Allow other applications to connect to the database radio
button.
2. Click on . This will allow you to see the command to be executed. Click RUN
T10 is the timestamp for the last incremental backup image of CUSTTBS.
3. CUSTTBS should now be in Roll forward pending. Open the Roll Forward task assistant:
a. Under the Roll-forward Scope settings, select the Roll the selected table spaces forward radio button and
select the CUSTTBS table space:
4. Click on the . This will allow you to see the command to be executed, and RUN.
5. Close the connection from data studio. Go to the terminal and close all connections to DS2
1. At its default configuration, db2ckbkp only verifies if the image is good. For instance, issue the following command with
any one of the backup images taken (you find the backup image name by listing out the ~/backups directory.):
cd ~/backups
db2ckbkp DS2.3.db2inst1.DBPART000.<TIMESTAMP>.001
DB2 10 for Linux, UNIX and Windows Course Workbook Page 120 of 335.
IBM Software
Information Management Technology Ecosystem
2. You can optionally use the “-h” argument to display media header information including the name and path of the image
expected by the restore utility. For example:
db2ckbkp -h DS2.3.db2inst1.DBPART000.<TIMESTAMP>.001
=====================
MEDIA HEADER REACHED:
=====================
Server Database Name -- DS2
Server Database Alias -- DS2
Client Database Alias -- DS2
Timestamp -- 20120116155426
Database Partition Number -- 0
Instance -- db2inst1
Database Configuration Type -- 0
Sequence Number -- 1
Database Member ID -- 0
Release ID -- F00
Database Seed -- 929C6DE2
DB Comment's Codepage (Volume) -- 0
DB Comment (Volume) --
DB Comment's Codepage (System) -- 0
DB Comment (System) --
Authentication Value -- -1
Backup Mode -- 1
Includes Logs -- 0
Compression -- 0
Backup Type -- 3
Backup Gran. -- 48
Merged Backup Image -- 0
Status Flags -- 20
System Cats inc -- 0
Catalog Partition Number -- 0
DB Codeset -- UTF-8
DB Territory -- US
LogID -- 1326744538
LogPath -- /home/db2inst1/dblogs/NODE0000/LOGSTREAM0000/
Backup Buffer Size -- 4460544
Number of Sessions -- 1
Platform -- 12
The above output contains a lot of important information. Just to highlight a few above, we can see if the backup image
is online or offline; an incremental or an incremental delta backup; whether it is a compressed or uncompressed image,
etc.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 121 of 335.
IBM Software
Information Management Technology Ecosystem
i.e.,:
Backup Mode -- 0 represents an offline backup
Backup Mode -- 1 represents an online backup
Backup Gran -- 0 represents a full backup
Backup Gran -- 16 represents an incremental backup
Backup Gran – 48 represents an incremental delta backup
Compression – 0 represents an uncompressed backup
Compression -- 1 represents a compressed backup
3. For details about other options, run db2ckbkp with no arguments or check the IBM Information Center.
db2ckbkp
8 Summary
With Database Backup and Recovery in DB2, you can increase application availability and ensure resiliency with reliable
subsystem backups and point-in-time recovery. This lab outlined some of the main backup and recovery features found within
DB2. By now, you should have a basic understanding of DB2 recovery architecture and how to use it with the new Data Studio
tool to make your life as a database administrator simpler.
9 Cleanup
To clean your environment after completing the exercise, if you would like to redo this lab in the future, please execute the
following in a terminal window:
cd ~/Documents/LabScripts/Backup/setup/
./cleanup_script.sh
exit
DB2 10 for Linux, UNIX and Windows Course Workbook Page 122 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada IBM products are warranted according to the terms and
8200 Warden Avenue conditions of
Markham, ON the agreements (e.g. IBM Customer Agreement, Statement
L6G 1C7 of Limited
Canada Warranty, International Program License Agreement, etc.)
under which
IBM, the IBM logo, ibm.com and Tivoli are trademarks or they are provided.
registered
trademarks of International Business Machines Corporation
in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence
in this
information with a trademark symbol (® or ™), these
symbols indicate
U.S. registered or common law trademarks owned by IBM
at the time
this information was published. Such trademarks may also
be registered or common law trademarks in other countries.
A current list of IBM trademarks is available on the Web at
“Copyright and trademark information” at
ibm.com/legal/copytrade.shtml
DB2 10 for Linux, UNIX and Windows Course Workbook Page 123 of 335.
IBM Software
Information Management Technology Ecosystem
Hands-On Lab
DB2 10 for Linux, UNIX and Windows Course Workbook Page 124 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 Suggested Reading .............................................................................................................................................................................3
3 About this Lab......................................................................................................................................................................................3
4 DB2 Storage Optimization ..................................................................................................................................................................4
4.1 Environment Setup Requirements ...................................................................................................................................................................... 4
4.2 Preparing for the lab ........................................................................................................................................................................................... 4
4.3 Examine Table Properties................................................................................................................................................................................... 5
4.4 Inline Large Object (LOB) Data........................................................................................................................................................................... 7
4.5 Run Sample Workload ........................................................................................................................................................................................ 9
4.6 Estimate Compression Savings ........................................................................................................................................................................ 13
4.7 Enable Classic Row Compression.................................................................................................................................................................... 14
4.8 Inspecting Compression Results ...................................................................................................................................................................... 16
4.9 Run Sample Workload on Compressed Data ................................................................................................................................................... 17
4.10 Applying Adaptive Compression ....................................................................................................................................................................... 19
5 Summary ............................................................................................................................................................................................22
6 Cleanup...............................................................................................................................................................................................22
DB2 10 for Linux, UNIX and Windows Course Workbook Page 125 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
Every business model looks for the best way to minimize production costs in order to maximize profits. One of the most
prominent features introduced in DB2 is the ability to perform row compression. The goal of row compression is to reduce
database storage needs. When using this feature, DB2 is able to shrink row sizes by building and utilizing a compression
dictionary, which is used as an alphabet to compress the rows of data in the database tables. Since more data can reside on a
data page, fewer data pages are allocated per table. The net effect is a reduced rate of storage consumption which may lead to
fewer disks needed when allocating storage requirements. Since disk storage is one of the most expensive components on any
data server, this feature can significantly reduce Total Cost of Ownership (TCO).
As part of the Storage Optimization feature, DB2 allows row compression of regular SQL data types, XML data, index
compression, temporary table compression, and new in DB2 10, adaptive compression further enhances the benefits of classic
row compression.
2 Suggested Reading
Best Practices: Deep Compression
http://www.ibm.com/developerworks/data/bestpractices/deepcompression/
DB2 10 for Linux, UNIX and Windows Course Workbook Page 126 of 335.
IBM Software
Information Management Technology Ecosystem
2. Right-click on the Desktop area and choose the “Open in Terminal” option:
3. As the user db2inst1, enter the following command in the terminal window to start the database instance. Make sure
the instance is started successfully.
db2start
The Database Manager is now started and we can begin the exercises by inspecting the available tables and data.
There are three pre-created tables for this exercise as described below:
• CUSTOMERS – This is a large table that contains customer data such as names, address, phone number, date of
purchase, an XML document, which describes the purchase in detail, and a text document (stored in the table as a
CLOB value), for each customer. The number of rows in the table is 100,000.
• CUSTOMERS_ARCH – This is a small table that contains similar data as in the table described above but is very
small in cardinality – only 30 rows.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 127 of 335.
IBM Software
Information Management Technology Ecosystem
• CODES – A relatively large table with about 50,000 rows, which contains coding information for about 50,000
customers. The data included is numerical.
To see the description of these tables and understand the data types of the included columns, connect to CUSTINFO
and view the tables.
5. Type the following commands in the terminal to view the columns’ data type and size within the pre-created tables.
db2 describe table customers
db2 describe table customers_arch
db2 describe table codes
6. To improve search on the large table CUSTOMERS, let’s create some indexes on the table. Create an index on the
CUSTOMER_NAME column and another index on the XML data, which would point to the
/PurchaseOrder/itemDetails/item/partid node.
A script has been provided for your convenience. Change to the directory where the scripts and data files for this lab are
located. Type the following command in the terminal:
cd /home/db2inst1/Documents/LabScripts/Compression
You should see the commands that create the relational and XML indexes. Press the “space bar” to scroll down and
press “q” to exit the view.
We have now prepared our data and in the next section we will examine the properties of each table.
We will now use the various techniques available to view compression statistics for the tables as well as table properties. After
we have examined the statistics, we will make a decision on how to optimize the storage for the CUSTINFO database.
The administrative table function SYSPROC.ADMIN_GET_TAB_INFO provides a method to retrieve table size and state
information that is not currently available in the catalog views. It can be used to retrieve significant information about tables.
Some of the information pieces include the actual physical storage that relational, XML, and large object data occupy on disk.
We can use this information to calculate the overall table size. Based on this we can determine if there is a need to use
compression to reduce the overall storage and possibly improve performance of queries.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 128 of 335.
IBM Software
Information Management Technology Ecosystem
There are no indexes on the CODES table. The indexes on the CUSTOMERS_ARCH table are system-required and
the ones on the CUSTOMERS table are a mix of system-required and the two indexes we created in the previous
section. Notice the different sizes for the relational, XML, and LOB data.
For your reference, the next table has the table sizes from above. You’ll use these to compare the compression results
later in this lab.
Note: The values below might differ, slightly, from your results depending on your current environment.
2. Now, check if these tables are enabled for compression, by executing the following statement in your terminal window.
db2 "select tabname, compression from syscat.tables where tabschema = 'DB2INST1'"
DB2 10 for Linux, UNIX and Windows Course Workbook Page 129 of 335.
IBM Software
Information Management Technology Ecosystem
The result shows that compression is not activated on any of the three tables since the value for the COMPRESSION
column is N.
LOBs can be quite large in size (from some kilobytes up to 2GB in DB2), and are generally stored separately from the base table,
in a location managed by the database manager. When retrieving such objects, DB2 fetches the page with the relational data,
and uses the LOB descriptor stored within the table row to locate the LOB on the system. This process results in an additional
I/O operation to access the LOB itself.
To simplify the manipulation of smaller LOBs, DB2 9.7 introduced the ability to have LOB data that falls below a size threshold to
be inlined within the base table rows. This reduced the overhead of retrieving LOBs separately as these LOBs could then be
fetched with the base table data and cached in the bufferpool.
In addition, inlining LOBs within a base table row, qualifies them for row compression. DB2 cannot compress LOB values that
are stored in the separate LOB storage area. By inlining the LOB values, we get better performance and improved compression
of data within the system.
We will demonstrate how to inline LOB values using the CUSTOMERS table. The first step is to know what inline length to
specify. DB2 provides a table function called ADMIN_EST_INLINE_LENGTH, which can estimate the inline length required to
inline data stored in an XML or LOB column. We will use it to determine the approximate inline length for the CLOB column in
the CUSTOMERS table.
1. Execute the provided script as shown below, to receive an estimate of the maximum record inline length.
db2 -tvf get_max_estimate_reclength.sql
You should see an output similar to the one shown below. The returned value (5408) is the largest length that can be
specified to inline the column DATA.
2. Alter the table CUSTOMERS to add an inline length for the DATA column.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 130 of 335.
IBM Software
Information Management Technology Ecosystem
db2 "alter table customers alter data set inline length 5408"
With the above ALTER TABLE command, LOB inlining has been enabled, although the existing LOB data has not been
moved as part of the command. To move LOB data, a table reorg is to be performed on the LOB data. With the inline
length set to 5408, all LOBs that are smaller than 5408 bytes will be inlined within the base table rows.
3. Execute both commands below in your terminal. The commands might take a few seconds to complete because of the
size of the table.
db2 reorg table customers longlobdata
db2 runstats on table db2inst1.customers for indexes all
4. Check the table properties again using the get_table_properties.sql script to see how data storage has changed.
db2 –tvf get_table_properties.sql
Notice the LOB_SIZE column value for the CUSTOMERS table. It is only 1MB compared to the initial value of 313MB
(section 4.3). Most of the LOB values were inlined, increasing the REL_DATA_SIZE in physical size. The reorganization
of the LOB data has resulted in some of the system-required indexes to be reduced in size, reducing the TOTAL_SIZE
of the data as well.
Record the column values for the three tables here and compare them to your previous recordings.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 131 of 335.
IBM Software
Information Management Technology Ecosystem
For our reference, we will run a sample workload and use the Visual Explain utility to check the estimated execution cost. This
estimated execution cost will be used in later sections to compare the effect that various compression techniques have on query
performance.
1. Open up Data Studio by clicking on the “IBM Data Studio” icon on the desktop.
3. By default, the Database Administration perspective is opened. Your workspace should look like the following :
DB2 10 for Linux, UNIX and Windows Course Workbook Page 132 of 335.
IBM Software
Information Management Technology Ecosystem
4. In the Administration Explorer, expand the folder All Databases and subfolders until you see CUSTINFO. Right click
on CUSTINFO and select Connect.
5. In the pop-up dialog, fill in the connection details as follows, and click Ok.
• Database : CUSTINFO
• Host : localhost
• Port number : 50001
• User name : db2inst1
• Password : password
DB2 10 for Linux, UNIX and Windows Course Workbook Page 133 of 335.
IBM Software
Information Management Technology Ecosystem
6. Now that you’re connected to the CUSTINFO database, open a new SQL Editor. In the Administration Explorer, first
make sure the CUSTINFO database is selected by clicking on it. Then click on New > New SQL Script.
7. In the SQL editor, time in the following SQL statement and press F5 (or click on the top-right of the SQL Editor) to
execute the query.
SELECT * FROM CUSTOMERS;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 134 of 335.
IBM Software
Information Management Technology Ecosystem
9. In the SQL Editor, click the Open Visual Explain button . The button is next to the ‘ button.
10. Keep the default settings in the configuration window that pops up, click Finish and wait until the utility completes
execution.
11. Double click on the Access Plan Diagram tab to expand the bottom pane of Data Studio to see the results:
12. Click on the RETURN operator. Note the estimated cost to execute the query and record it. It should be something
close to 33722.1 timerons.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 135 of 335.
IBM Software
Information Management Technology Ecosystem
13. Restore the Access Plan Diagram window to its original size by clicking the button and minimize Data Studio so we
can continue to work with our data. We will come back to it in a little while.
Now we are going to estimate the compression savings if we were to enable row compression on our various tables. For that,
we’ll make use of the table function SYSPROC.ADMIN_GET_TAB_COMPRESS_INFO. This function provides a variety of
estimates, some of which include an estimated percentage of how many pages could be saved, an estimated percentage of how
many bytes would be saved, as well as overall estimated record length after compression. This information is reported for both
relational and XML data. The estimate provides an analysis of both static and adaptive compression.
With regards to indexes, another table function is made available to users to estimate index compression savings –
SYSPROC.ADMIN_GET_INDEX_COMPRESS_INFO.
1. Execute the following script in your terminal window to estimate relational and XML data savings.
db2 -tvf estimate_compression_results.sql > estimateResults.txt
2. To view the output of the compression estimate, open the estimateResults.txt file using gedit.
gedit estimateResults.txt &
Figure 18 - Estimate compression results for both relational and XML data
The result of the compression estimate run against the DB2INST1 schema shows that most of the savings are related
to the CUSTOMERS table.
• 77% relational data page savings and 79% XML data page savings using static compression
• 81% relational data page savings and 79% XML data page savings using adaptive compression.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 136 of 335.
IBM Software
Information Management Technology Ecosystem
The CUSTOMERS_ARCH table has similar estimation results as it has similar data to the CUSTOMERS table but it
only has ~ 10,000 records in cardinality compared to the 100,000 records in the CUSTOMERS table. The CODES table
only reports 1% relational data page savings. This was expected since the table contains random numerical data and
with very few repeating patterns for row compression to take much affect.
Based on these results, we will enable compression on the CUSTOMERS and CUSTOMERS_ARCH table, in the next
section. CUSTOMERS_ARCH table will not have any significant benefit due to its small size, however we will still
enable compression to see how its total physical size changes compared to the previously recorded results. The
CODES table is left as is.
3. Next, you will estimate Index compression savings. Close your gedit editor and execute the following script in your
terminal window.
db2 -tvf idx_compression_results.sql
When running the table function to estimate index compression savings, we notice very little or no savings on the
ITEMID index. One of the system-required indexes and NAMEINDEX both see relatively good improvement with 12%
and 32% data page savings respectively. Since Index Compression is automatically enabled with Row Compression,
any new indexes will automatically be compressed. However, existing indexes need to be altered to enable
compression. We will demonstrate this on the user created index; NAMEINDEX.
The ITEMID index may not be compressed well as it contains information that is not contained within the base data, and
may not be added to the relational dictionary.
We will now enable classic row compression as we decided in the previous section for the CUSTOMERS and
CUSTOMERS_ARCH tables. Since these tables are pre-populated with data, we can’t make use of the Automatic Dictionary
Creation; therefore we will need to perform an offline reorg after we enable compression.
1. Enable row compression by executing the following commands in your terminal window:
db2 alter table customers compress yes static
db2 alter table customers_arch compress yes static
db2 alter index DB2INST1.NAMEINDEX compress yes
DB2 10 for Linux, UNIX and Windows Course Workbook Page 137 of 335.
IBM Software
Information Management Technology Ecosystem
All commands should report a successful status. At this point compression is enabled on our tables and the index
‘NAMEINDEX’. If we continue to load data, ADC will kick in at a certain point and create a dictionary based on the data
that already exists but the sampling data will not be compressed. Since we have a lot of data in our CUSTOMERS table,
we want to perform an offline reorg on the tables to create a dictionary and compress all rows.
2. Execute the following commands in your terminal window to reorganize relational table data.
db2 reorg table customers
db2 reorg table customers_arch
The relational dictionaries for both CUSTOMERS and CUSTOMERS_ARCH tables should now be created and
relational data and indexes should be compressed.
3. Execute the following commands in your terminal window to reorganize XML table data and indexes related to both
CUSTOMERS and CUSTOMERS_ARCH tables.
db2 reorg table customers longlobdata
db2 reorg table customers_arch longlobdata
db2 reorg indexes all for table customers
db2 runstats on table db2inst1.customers and indexes all
DB2 10 for Linux, UNIX and Windows Course Workbook Page 138 of 335.
IBM Software
Information Management Technology Ecosystem
In this section we will use the available tools to see how our data compressed.
1. Let’s see what the actual savings were for data and indexes as reported by the two compression table functions.
Execute the following script in your terminal. To view results, launch the result file in gedit. The attributes with ‘_Current’
reflect the actual compression results
db2 -tvf estimate_compression_results.sql > estimateResultsAfterComp.txt
gedit estimateResultsAfterComp.txt &
You will find that executing the script after enabling compression has changed its output. You will notice that
ROWCOMPMODE now has an S against the tables with compression enabled. The character ‘S’ represents static
compression. To see the total data page savings achieved by using static compressions, refer to the column
“PCTPAGESSAVED_CURRENT”. It can be observed that 77% of XML data pages and 76% of relational data pages
from the CUSTOMERS table were saved and 77 % and 63% data pages, respectively, from the CUSTOMERS_ARCH
table.
2. Let’s see how the physical sizes of the different database objects changed. Execute the get_table_properties.sql script
again as follows:
db2 -tvf get_table_properties.sql
Now take a look at the physical sizes of the various database objects, including the total size of the CUSTOMERS table,
the table has shrunk to nearly half the original size. The inlined LOB objects being included in the compression process
further reduced the overall table size.
3. Record the column values for the three tables and compare them to your previous recordings.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 139 of 335.
IBM Software
Information Management Technology Ecosystem
The actual index compression results are better than expected; the NAMEINDEX has been compressed with 42%
pages saved. You can see that it has been compressed by the INDEX_COMPRESSED attribute having changed to ‘Y’.
All other indexes remain uncompressed with estimates provided for one of the system indexes.
5. Now execute the RUNSTATS command to update the statistics in order to be able to leverage the performance
increase in Visual Explain in the following steps.
db2 runstats on table db2inst1.customers
We can now run the same workload as in section 3.5 and see how the query performs after data has been compressed.
1. Bring back the Data Studio that we minimized to the taskbar, and run the same script again by, navigating to the SQL
editor and either pressing f5 or clicking on the “Run SQL” button.
At the bottom of the Data Studio screen, in the SQL Results pane, you should see a Successful result message.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 140 of 335.
IBM Software
Information Management Technology Ecosystem
2. Next, in the SQL Editor, click the Open Visual Explain button .
3. Click Finish in the configuration window, which pops up and wait until the utility has finished executing. Finally, expand
the bottom pane of Data Studio to see the results.
Note that the time it takes DB2 to perform the workload is now close to 6768.34 timerons compared to the 33722.1
timerons as reported by Visual Explain before classic row compression was enabled on the table. Not only did we save
on storage by enabling compression, but we also improved the performance of the query.
BEFORE AFTER
Figure 29 Comparative cumulative total cost BEFORE and AFTER classic compression
DB2 10 for Linux, UNIX and Windows Course Workbook Page 141 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 has further improved on classic row compression by introducing a new hybrid compression technique, referred to as
adaptive compression. Adaptive compression offers the most dramatic possibilities for storage savings by leveraging classic row
compression and also working on a page-by-page basis to further compress data.
Up until now, you have been using static compression in order to examine performance and storage benefits. You will now
apply adaptive compression. This section of the lab will have you upgrade a specific table from using static compression to
adaptive compression, and analyze the results afterwards.
1. Switch back to the command line terminal and inspect the current CUSTOMER table properties by executing the script
below.
db2 -tvf get_table_properties.sql
2. Next, execute the script below to inspect the estimated savings on the current CUSTOMERS table. The current savings
showcase the savings achieved using static compression. Expand the screen to properly view the output.
db2 -tvf compare_static_adaptive.sql
You will notice that the estimated PCTPAGESSAVED_ADAPTIVE is higher than the current static compression values.
This is due to the fact that adaptive compression uses two compression approaches. It first uses the same table-level
compression dictionary used in classic row compression to compress data based on repetition within a sampling of data
from the table as a whole. The second approach uses a page-level dictionary-based compression algorithm to
compress data based on data repetition within each page of data.
3. Take note of the columns PCTPAGESSAVED_CURRENT, and AVGROWSIZE_CURRENT.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 142 of 335.
IBM Software
Information Management Technology Ecosystem
PCTPAGESSAVED_CURRENT
AVGROWSIZE_CURRENT
4. Next, apply adaptive compression on the CUSTOMERS table by executing the script below.
db2 alter table customers compress yes adaptive
db2 reorg table customers
db2 runstats on table customers and indexes all
Notice that the relational data size has been further compressed, thereby promoting a reduction in the total size of the
table.
6. Finally, inspect the savings after applying adaptive compression.
db2 –tvf compare_static_adaptive.sql
7. Now, inspect the performance benefits of adaptive compression. Bring back the Data Studio that we minimized to the
taskbar. Navigate to the SQL editor and run the same SQL script by pressing F5 or clicking on .
At the bottom of the Data Studio screen, in the SQL Results pane, you should see a Successful result message.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 143 of 335.
IBM Software
Information Management Technology Ecosystem
8. Next, In the SQL Editor, click the Open Visual Explain button .
9. Click Finish in the configuration window, which pops up and wait until the utility has finished executing. Finally, expand
the bottom pane of Data Studio to see the results.
Note that the time it takes DB2 to perform the workload is now close to 6433 timerons compared to the 33722.1 and
6768.34 timerons as reported by Visual Explain with no compression techniques, and classic row compression
respectively. Not only did we save on storage but we also improved the speed of the query by enabling adaptive
compression, thereby illustrating the performance advantages of this new technology.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 144 of 335.
IBM Software
Information Management Technology Ecosystem
Figure 37 – Comparative cost between no compression, classic row compression and adaptive compression Deleted: 40
5 Summary
This concludes the DB2 Storage Optimization lab. In this lab, we performed several operations including demonstrating various
compression techniques that DB2 employs, as well as compressing different data types including relational, xml, large object,
and index data. We examined the compression and performance savings by using various tools and built-in DB2 functions that
perform detailed reporting such as the Visual Explain tool and the ADMIN_GET_TAB_COMPRESS_INFO function. Finally, we
also had a look at the new adaptive compression feature in DB2 10 and the additional storage and performance savings that can
be achieved through its use.
With industry-leading compression technologies, DB2 not only allows greater savings in storage cost, it also improves
performance as queries on a compressed table needs fewer I/O operations to access the same amount of data.
6 Cleanup
1. Right-click on CUSTINFO database from within the Data Source Explorer and select Disconnect.
1. Close Data Studio and switch back to the terminal window that you have been using so far, and then execute the
command below to clean your environment after completing the exercise. Use ‘password’ as password.
su –
cd /home/db2inst1/Documents/LabScripts/Compression/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 145 of 335.
IBM Software
Information Management Technology Ecosystem
cd /home/db2inst1/Documents/LabScripts/Compression/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 146 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 147 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 148 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 About this Lab......................................................................................................................................................................................3
3 Suggested Reading .............................................................................................................................................................................3
4 DB2 Range Partitioning Basics and Setup .......................................................................................................................................3
4.1 Environment Setup Requirements ...................................................................................................................................................................... 3
4.2 Initial Steps ......................................................................................................................................................................................................... 3
4.3 Inspecting the Database, Table Spaces and Range Partitioned Table .............................................................................................................. 4
4.4 Examine Table Spaces ....................................................................................................................................................................................... 9
4.5 Create Materialized Query Table ........................................................................................................................................................................ 9
4.6 Load Table Data into the Range Partitioned Table........................................................................................................................................... 12
6 Detaching a Partition.........................................................................................................................................................................23
6.1 Detach Partition ................................................................................................................................................................................................ 23
7 Summary ............................................................................................................................................................................................25
8 Clean up..............................................................................................................................................................................................25
DB2 10 for Linux, UNIX and Windows Course Workbook Page 149 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
DB2 10 contains innovative features for delivering information on demand and scaling databases to new levels. The Table
(Range) Partitioning capabilities in DB2 provide a number of advantages including better query performance, improved large
table manageability, and increased availability for data roll-in/roll-out. The Large Row Identifier (RID) allows for creation of tables
and indexes that are terabytes in size. Together these features enable the ability to create and easily manage databases with
virtually unlimited capacity, and are especially beneficial in large data-warehousing environments.
3 Suggested Reading
Table partitioning in DB2 (developerWorks article)
http://www.ibm.com/developerworks/data/library/techarticle/dm-0605ahuja2/index.html
DB2 10 for Linux, UNIX and Windows Course Workbook Page 150 of 335.
IBM Software
Information Management Technology Ecosystem
• Username: db2inst1
• Password: password
3. Open a terminal window by right-clicking on the Desktop and choosing the Open in Terminal item.
4. Ensure that the DB2 Database Manager has been started by issuing the following command at the prompt:
db2start
Attention: This command will only work if you logged in as the user db2inst1. If you accidentally logged in as
another user, then type su - db2inst1 at the command prompt. Password: password.
4.3 Inspecting the Database, Table Spaces and Range Partitioned Table
We have provided you with a script, which creates a database, the needed table spaces and the range-partitioned table that you
will work with in this portion of the lab. The script has already been executed so there is no need to run it again. However, it
would be beneficial to understand the partitioned environment you are working with.
1. Change your working directory to the folder containing the script mentioned.
cd /home/db2inst1/Documents/LabScripts/RangePartition
2. Take a look at the script using your favorite editor. Note how the database, table spaces, and table are being created:
less createDBandTable.sql
Note: You can use the arrow keys to scroll up and down in the script and type the letter “q” to exit anytime.
The script creates a database named RANGE, 16 table spaces and all tables:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 151 of 335.
IBM Software
Information Management Technology Ecosystem
The name of the main partitioned table that we will be working with is CAR_ORDER. The table is partitioned on each year and
each partition is stored in a separate table space. Indexes defined on the table are also partitioned, each partitioned in a
designated table space. As we will see this becomes very beneficial when the DB2 optimizer creates an access plan for queries
that only involve data in specific partitions, only those data and index partitions are accessed when returning the result set.
Another table, YR_2006, is created in this script which will be used for attaching partitions to the CAR_ORDER table as a new
partition later in the lab.
A non-partitioned table, CAR_ORDER_NOPART identical to the CAR_ORDER table is also created. This table will be used in
an exercise that will emphasize the benefits of partition elimination.
The following is a logical representation of the CAR_ORDER table that we have just created. All of the data starting from the
beginning of 2000 to the end of 2005 will be split-up into ranges by year and will be put in the 6 table spaces TBLSPC0 -
TBLSPC5. Each partition (data and index) is stored in a specific table space. Since table spaces can be assigned to dedicated
storage, each partition can effectively have its own dedicated storage components.
Y2000 Data Y2001 Data Y2002 Data Y2003 Data Y2004 Data Y2005 Data
3. Let’s have a look at the definition of the table and the data partitions. Click on ‘”IBM Data Studio” icon on desktop.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 152 of 335.
IBM Software
Information Management Technology Ecosystem
4. In the Select a workspace dialog, accept the default path and click OK.
5. Data Studio will now start in the Database Administration perspective as shown below.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 153 of 335.
IBM Software
Information Management Technology Ecosystem
6. Expand the Database Connections in the Administration Explorer menu on the left-hand side, right-click on the RANGE
database and click Connect.
• User: db2inst1
• Password: password
DB2 10 for Linux, UNIX and Windows Course Workbook Page 154 of 335.
IBM Software
Information Management Technology Ecosystem
7. Once successfully connected, in the Administration Explorer view, expand the following nodes by clicking the icons
beside them: RANGE > Tables.
8. Select the CAR_ORDER table to view its details in the Properties tab on the bottom.
9. Click on Data Partitions in the menu to view the data partitions for this table.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 155 of 335.
IBM Software
Information Management Technology Ecosystem
You should see 6 data partitions named “Y2000” to “Y2005” similar to the picture above. You will continue to use Data Studio
throughout the rest of the exercises, therefore you should leave it open after you have finished analyzing the Data Partitions.
1. Open a new terminal window by right clicking on the Desktop and selecting Open in Terminal.
cd /home/db2inst1/Documents/LabScripts/RangePartition
./showTablespaces.sh > tb_examine1
3. Open the file tb_examine1 using your favorite text editor and take a look at this output.
less tb_examine1
One thing to note is the low page usage for MQTTS. This is because the MQTTS table space has no data objects defined
or built yet. Press ‘q’ on keyboard to exit the less utility.
1. Open the history.sql file containing the necessary commands to create this table by clicking on File at the top menu and
selecting Open File…
DB2 10 for Linux, UNIX and Windows Course Workbook Page 156 of 335.
IBM Software
Information Management Technology Ecosystem
3. After selecting the file “history.sql”, press OK. You will see the following create table statement in the command panel:
Figure 11 – history.sql
5. You will be prompted to confirm the database that you would like to run these commands against. Select the RANGE
database from the list and click Finish.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 157 of 335.
IBM Software
Information Management Technology Ecosystem
6. Check that the command you have entered above is executed without errors by looking at the bottom result panel:
7. Close the history.sql command window after examining the output by clicking on the “X” button at the top right-hand corner
of the window.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 158 of 335.
IBM Software
Information Management Technology Ecosystem
8. Navigate to RANGE > MQTs in the Administration Explorer view. Right-click MQT and select “Refresh”. You should now
see the HISTORY table.
Figure 16 – MQTs
1. In Data Studio, within the Administration Explorer view, navigate to RANGE > Tables. You will see the CAR_ORDER
table within this list. If you cannot navigate here, make sure you reconnect to the database.
2. Right-click on the CAR_ORDER table and select the Load option, then select With Load Utility.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 159 of 335.
IBM Software
Information Management Technology Ecosystem
3. You can either navigate using Browse or directly input the following input files path:
/home/db2inst1/Documents/LabScripts/RangePartition/ORDER.TBL
4. After entering this path click “Run” to start the load of data from the flat file ORDER.TBL into the table “CAR_ORDER”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 160 of 335.
IBM Software
Information Management Technology Ecosystem
5. Make sure that all records have been successfully loaded into the database.
6. Close the Load Table CAR_ORDER tab in the main view by clicking its icon.
Now that we have loaded the data, the CAR_ORDER table is in Set Integrity Pending No Access state. We must use the Set
Integrity command check its constraints and bring the table back online.
7. Navigate to the “CAR_ORDER” table as done previously, right-click on the table and select “Manage > Set Integrity…”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 161 of 335.
IBM Software
Information Management Technology Ecosystem
8. Leave all options as defaults and press “Run” to start the set integrity.
9. Ensure that the operation finished without errors in the “SQL Results” window and in the Messages tab.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 162 of 335.
IBM Software
Information Management Technology Ecosystem
10. Close the Set Integrity CAR_ORDER tab in the main view by clicking its icon.
Now the CAR_ORDER table is back online and we can inspect its contents. Right-click on the CAR_ORDER table to bring
up the menu and select Browse Data within the menu. Note that only the first 500 rows of the table are fetched.
11. We will now perform the same action to set the integrity of the HISTORY table. Open one of your previously used command
terminals and issue the following command:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 163 of 335.
IBM Software
Information Management Technology Ecosystem
12. Take this opportunity to query the status of the table spaces. Execute the following:
cd /home/db2inst1/Documents/LabScripts/RangePartition
./showTablespaces.sh > tb_examine2
13. Open the file tb_examine2 using your favorite text editor and take a look at this output.
less tb_examine2
Notice how the data from the table has been spread across the table spaces (TBLSPC0 – TBLSPC5). Since we have not
yet defined indexes no pages are required by the index table spaces.
Now that we have run Set Integrity against the HISTORY MQT, we can inspect its contents in a similar manner to that of the
CAR_ORDER table. Remember, this table is located under RANGE > MQTs.
14. Right click the HISTORY table and select “Browse Data”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 164 of 335.
IBM Software
Information Management Technology Ecosystem
5 Attaching a Partition
After completion of this section of the exercise the student will be able to:
• Load data into a staging table with a table space for partitioned index defined.
• Alter the CAR_ORDER table to attach data from the staging table as a new partition.
• Explore data availability enhancements.
The following is a logical representation of loading data into the staging table YR_2006 and attaching the table as a new data
partition in the range-partitioned table CAR_ORDER and then setting the integrity:
YR_2006 TABLE
DB2 10 for Linux, UNIX and Windows Course Workbook Page 165 of 335.
IBM Software
Information Management Technology Ecosystem
1. Before loading data into the staging table YR_2006, take a look again at the createDBandTable.sql script, and note how
the staging table is defined. Note how the “index in” clause is specified in the CREATE TABLE statement for the staging
table YR_2006. When the staging table is attached any partitioned indexes defined on the CAR_ORDER table will be
created for the new partition. Indexes are actually built when the SET INTEGRITY command is run.
cd /home/db2inst1/Documents/LabScripts/RangePartition
less createDBandTable.sql
Note: If the “index in” clause was not specified, the index partition for the new data partition would be created in the same table
space as the corresponding data partition.
1. Load some data into the “YR_2006” table. Using Data Studio, right click the YR_2006 table and select “Load” > “With Load
Utility”.
2. Enter the input file path for the load operation by either navigate using Browse or directly input the following input files path:
/home/db2inst1/Documents/LabScripts/RangePartition/yr_06.TBL
DB2 10 for Linux, UNIX and Windows Course Workbook Page 166 of 335.
IBM Software
Information Management Technology Ecosystem
3. Click “Run” to start the load of data from the “YR_06.TBL” flat file into table “YR_2006”.
A message on the result view should indicate that these commands have executed successfully.
4. Inspect the data that was loaded by right clicking on YR_2006 and select Browse Data.
Note that you did not need to run SET INTEGRITY as there are no constraints nor indexes defined on the YR_2006 table,
hence the table is brought online right after the LOAD command is executed.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 167 of 335.
IBM Software
Information Management Technology Ecosystem
5. Take this opportunity to go back to the terminal window to display the table space page usage and execute the following:
cd /home/db2inst1/Documents/LabScripts/RangePartition
./showTablespaces.sh > tb_examine3
6. Open the file tb_examine3 using your favorite text editor and take a look at this output.
less tb_examine3
Note the number of used pages for TBSPC6 has increased as data was loaded.
With DB2 10, when attaching a new data partition using ALTER TABLE … ATTACH/ADD PARTITION, the target partitioned
table remains online. Dynamic queries running under the Read Stability (RS), Cursor Stability (CS), or Uncommitted Read (UR)
continue to have access to the table.
The newly attached data with date range 01/01/2006 to 12/31/2006 is not yet visible in the CAR_ORDER table because it is
not yet validated by the SET INTEGRITY statement. The SET INTEGRITY statement is necessary to verify that the newly
attached data is within the defined range.
2. If you wish to test it, before running SET INTEGRITY, run the following query. It will not return any rows at this point, as the
new data partition is still under Set Integrity Pending state.
In previous DB2 releases, the table would be taken out of set integrity pending state by performing a SET INTEGRITY…
IMMEDIATE CHECKED command, thus performing the required integrity processing on the table.
What if data integrity checking, including range validation and other constraints checking, can be done through application
logic that is independent of the data server before an attach operation? Starting in DB2 10, the newly attached data can be
made available for use much sooner.
You can optimize the data roll-in process by using the SET INTEGRITY…ALL IMMEDIATE UNCHECKED statement to skip
range and constraints violation checking. In this case, the data partition is brought out of Set Integrity Pending state, and the
new data is available for applications to use immediately, as long as there are no non-partitioned indexes on the target table.
Otherwise, the SET INTEGRITY… ALL IMMEDIATE UNCHECKED is automatically converted to a SET INTEGRITY…
IMMEDIATE CHECKED, consequently triggering the required integrity processing on the table.
3. Considering the data in the attached partition was previously checked, use the following commands to set the integrity on
both CAR_ORDER and HISTORY tables without performing integrity processing:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 168 of 335.
IBM Software
Information Management Technology Ecosystem
Note: Although the MDC is taken out of the check pending state, it is not refreshed, that is, the 2006 data is not showing in
HISTORY. To refresh the MDC, the following procedure must be followed:
db2 SET INTEGRITY FOR CAR_ORDER OFF CASCADE IMMEDIATE
db2 SET INTEGRITY FOR CAR_ORDER IMMEDIATE CHECKED
db2 SET INTEGRITY FOR HISTORY IMMEDIATE CHECKED
Switch back to Data Studio to perform the rest of the commands below.
4. Right-click the Tables folder, select Refresh. Navigate to the “CAR_ORDER” table, click the “PROPERTIES” tab, and
select the Data Partitions menu option.
5. Close the CAR_ORDER tab in the main view by clicking its icon. Notice that the staging table “YR_2006” no longer exists!
It has become a data partition in the range-partitioned table CAR_ORDER.
6. Take this opportunity to go back to the terminal window to display the table space page usage and execute the following:
cd /home/db2inst1/Documents/LabScripts/RangePartition
./showTablespaces.sh > tb_examine4
Note how the number of “Used Pages for TBSPC6I has increased significantly this time, due to the building of the
partitioned index.
7. Since we are in the terminal, we can also execute the SELECT statement used before which will now allow us to view the
newly attached data with date range 01/01/2006 to 12/31/2006 in the CAR_ORDER table.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 169 of 335.
IBM Software
Information Management Technology Ecosystem
6 Detaching a Partition
The following is a logical representation of the detach operation. The detached partition becomes a separate table and the data
is no longer available in the partitioned table.
CAR_ORDER TABLE
DETACH
TBLSPC0
YR_2000
1. Execute the following command in the same terminal used for the previous statements:
2. You must perform set integrity on the HISTORY table since the YR_2000 is a dependent table which needs to be
incrementally maintained with respect to the detached data partition so it is initially inaccessible. Use the following
command to set the table integrity:
3. In Data Studio, right-click the Tables folder, select Refresh. Navigate to the CAR_ORDER table, click on the table and
select the Data Partitions option from the menu within the Properties view. In the window, notice that the “Y2000” partition
is no longer listed because it has been detached into a standalone table “YR_2000”.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 170 of 335.
IBM Software
Information Management Technology Ecosystem
4. Now inspect the YR_2000 table that has been detached from the CAR_ORDER table. Right-click on the “YR_2000” table
and select “Browse data”. Notice that the data with year 2000 dates are located in the YR_2000 table.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 171 of 335.
IBM Software
Information Management Technology Ecosystem
7 Summary
By now, you will have experienced the benefits of implementing DB2’s range partitioning features. You now understand how
range partitioning can allow for quick roll-in and roll-out of data and near limitless scalability. Furthermore, you have touched
upon features and functionalities such as materialized query tables, SET INTEGRITY statements and index creation.
8 Clean up
1. To clean your environment after completing the exercise, if you would like to redo this lab in the future, please right-click
on RANGE database, select Disconnect, close Data Studio and execute the commands below in a terminal. Use
“password” as password.
su -
cd /home/db2inst1/Documents/LabScripts/RangePartition/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/RangePartition/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 172 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 173 of 335.
IBM Software
Information Management
DB2 10 for Linux, UNIX and Windows Course Workbook Page 174 of 335.
IBM Software
Information Management
Table of Contents
1 Introduction............................................................................................................................................................................................3
2 About This Lab.......................................................................................................................................................................................3
3 Environment Setup Requirements.......................................................................................................................................................3
3.1 Login to the Virtual Machine ............................................................................................................................................................................. 3
OPTIONAL EXERCISES................................................................................................................................................................................21
8 Constraints on the History Table for System-Period Temporal Tables (optional)..........................................................................21
9 Altering a Table with Versioning Enabled (optional) .........................................................................................................................21
9.1 Adding a Column .............................................................................................................................................................................................. 21
9.2 Dropping Objects .............................................................................................................................................................................................. 22
9.2.1 Dropping a Table .................................................................................................................................................................................. 22
9.2.2 Dropping a Column............................................................................................................................................................................... 23
10 Summary ................................................................................................................................................................................................24
11 Cleanup...................................................................................................................................................................................................24
DB2 10 for Linux, UNIX and Windows Course Workbook Page 175 of 335.
IBM Software
Information Management
1 Introduction
Pericles, a great Athenian statesman, once said “Time is the wisest counsellor of all”. This saying is as true today as was in
Ancient Greece. Time plays a key role in allowing organizations to better understand themselves. Organizations use the history
of stored data along with the study of how that data transforms over time, to better understand the impact they make. More so, in
recent years, stricter governance and compliance policies have created a greater need for organizations to learn from “time” and
to be aware of the accuracy of their data. Along with compliance, tracking time also allows organizations to manage the aspect
of time for their business information, such as ‘effective dates for an insurance policy’. In an effort to allow organizations to
accomplish this, IBM has developed greater awareness of time within DB2.
Prior to this release, database professionals had to create complex triggers or use application logic to manage time-dependent
conditions. With DB2’s new temporal data management technology, database professionals can, now, track and query these
conditions using newly introduced table design options, query syntax and semantics. Thus alleviating the need to go through
complex application logic and giving developers the control to ensure consistent handling of time-related events across the
system.
DB2’s temporal data management introduces several new concepts. System time involves tracking when changes are made to
the state of a table; such tables are referred to as system-period temporal tables (STT). Business time involves tracking the
effective dates of certain business conditions, also referred to as “application time”. Tables tracking application/business time
are referred to as application-period temporal tables (ATT). Some organizations might require tracking both types of temporal
data in a single table; such tables would be considered bitemporal (BTT). This hands-on lab introduces you to these key
concepts and capabilities of DB2’s temporal data management.
Throughout the lab, each of the different types of temporal tables will be created and scenarios highlighting the ways to track
and query data based on time will be demonstrated. After all of the fundamentals are covered, some specific advanced
scenarios will be discussed– such as finding the previous version of a row.
1. Start the VMware image. Use the login credentials below when prompted:
• User: db2inst1
• Password: password
DB2 10 for Linux, UNIX and Windows Course Workbook Page 176 of 335.
IBM Software
Information Management
2. Open a terminal window by right-clicking on the Desktop and choosing the “Open Terminal” item:
In the upcoming example, a LOANS table is created as a STT. The current status of each resource is stored in the LOANS table
and the past historical data is stored in the LOANS_HISTORY table.
1. Open IBM Data Studio by double clicking the icon on the desktop. Use the default workspace.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 177 of 335.
IBM Software
Information Management
2. Open the create script for the system-period temporal base table. In Data Studio, click menu File > Open File on the
top left hand corner.
3. Navigate to the /home/db2inst1/Documents/LabScripts/Temporal directory and open the file named
System_Base.sql. When prompted, press Ok to use the default statement terminator (semi-colon).
CREATE TABLE LOANS (
id INT primary key not null,
Title VARCHAR(255),
BorrowerID INT,
Duration INT,
Renewable CHAR(1),
sys_start TIMESTAMP(12) GENERATED ALWAYS
AS ROW BEGIN NOT NULL,
sys_end TIMESTAMP(12) GENERATED ALWAYS
AS ROW END NOT NULL,
trans_start TIMESTAMP(12) GENERATED ALWAYS
AS TRANSACTION START ID IMPLICITLY HIDDEN,
PERIOD SYSTEM_TIME (sys_start, sys_end)
);
Attention: All commands are denoted by the text within the grey shaded area.
4. Ensure you will execute the query against the TEMPORAL database by right-clicking inside the SQL Editor of Data
Studio.
5. Select ‘Use Database Connection’, click TEMPORAL and then click Finish.
6. Enter the credentials in the Properties pane – user name: db2inst1 password: password, save this information for
future connections.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 178 of 335.
IBM Software
Information Management
7. Execute the script by pressing the run button in Data Studio, or press F5 while the cursor is inside the SQL Editor.
Attention: All subsequent instructions consider that you have a clear SQL editor. Please clear the previous command before
moving on to the next one.
2. We’ll create the history table using a CREATE TABLE statement with the LIKE clause. This ensures that the history
table has the same columns as the base table.
Type in the following statements. The first one creates the history table whereas the second statement selects from the
newly created table. Execute the commands by pressing F5. When prompted, select the connection to the TEMPORAL
database.
Note: When executing multiples commands at a time like the instructions above, to see individual results, expand the result entry
by clicking the triangle (see the SQL Results tab in Figure 5) and then click the command you wish to see the results for. The results are
shown in the Result tab on the bottom-right corner (by default it is on the status tab).
DB2 10 for Linux, UNIX and Windows Course Workbook Page 179 of 335.
IBM Software
Information Management
Figure 5: Create the history table and verify that it has been created properly
4.3 Enable Versioning for the Pair of Base and History Table
After the tables are created, we need to add versioning to establish the link between the system-period temporal table and the
history table. Once versioning is enabled, DB2 automatically copies old versions of the data to the corresponding history table.
1. Enable versioning by altering the base table. Type the following into the SQL Editor and execute it.
ALTER TABLE LOANS ADD VERSIONING USE HISTORY TABLE LOANS_HISTORY;
Inserting new data into a STT is no different than inserting into a regular table.
1. In Data Studio, select File -> File Open, and navigate to open the insert_resources_sys.sql script. It resides in the
following directory: /home/db2inst1/Documents/LabScripts/Temporal/. Notice these are regular SQL INSERT
statements.
2. Execute the script (press F5) against the TEMPORAL database (refer to Section 4.1 on selecting connections to the
database).
DB2 10 for Linux, UNIX and Windows Course Workbook Page 180 of 335.
IBM Software
Information Management
3. Inspecting the LOANS table will reveal that the SYS_START and SYS_END columns have been automatically set with
timestamp values. Execute the SELECT statement below in a clear SQL Editor.
SELECT * FROM LOANS;
The values in the SYS_START column indicate when the row became current. In this case, that’s the timestamp when
the rows were inserted.
The values in the SYS_END column indicate when the row was no longer current. In this scenario, all rows have the
value Dec. 30, 9999 00:00:00.0. This indicates that the data is current, i.e. it has not been updated or deleted.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 181 of 335.
IBM Software
Information Management
When rows are updated in a STT, DB2 transparently copies the old versions of the modified rows to the appropriate history table.
Updating a STT is no different than a regular table; hence it does not require use of special syntax from applications or users.
In this example, a book “DB2 WORKS FOR YOU” has now become renewable and the change in status has to be updated in
the LOANS table. Upon executing the update statement, DB2 automatically creates a history of the state change in the history
table, LOANS_HISTORY. Comparing the history table before the transaction and after the transaction will reveal that DB2
transparently recorded the change in the state of the data.
1. Query the LOANS_HISTORY table to see its current contents by executing the statement below in the previously
created SQL Editor. There should be nothing in the table as no data has been modified in the STT.
SELECT * FROM LOANS_HISTORY;
2. Execute the update command below to set the “DB2 Works For You” to renewable, i.e. set the value of column
RENEWABLE to ‘Y’. Please type the query exactly as indicated below to make sure the proper row is modified.
UPDATE LOANS
SET renewable ='Y '
WHERE title = 'DB2 Works For You ';
3. Inspect the current table (LOANS) to see the changed values. Note that the SYS_START value of the row has been
updated and modified accordingly. It is now set to the time when the update was performed.
SELECT * FROM LOANS;
Query the LOANS_HISTORY table again to confirm that DB2 has copied the old version of the updated row.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 182 of 335.
IBM Software
Information Management
Note: Querying historical data, makes it possible to retrieve all previous states of the data and reconstruct the various changes.
Similar to the update operation, DB2 automatically removes data from the current table while transparently recording the change
of state in the corresponding history table.
1. Suppose all entries for the person with BORROWERID = 25 are to be deleted. The library will purge this information
from their currently borrowed resources within the LOANS table. Execute the DELETE statement below:
DELETE FROM LOANS
WHERE borrowerid = 25;
2. The changes in state are again logged in the history table. Inspecting the LOANS_HISTORY table reveals that all
deleted rows can now be found in it.
SELECT * FROM LOANS_HISTORY;
Querying a temporal table does not require any complex programming. The syntax and semantics of basic SELECT statements
can still be used. In this case, you will be querying only the current data in the base table.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 183 of 335.
IBM Software
Information Management
Additionally, you can return results for a specific point or period of time. Those results can include current and historical values.
For that purpose, there are three supported period specifications when querying system-period temporal tables.
Suppose the library wants to take account of all loan transactions as of a specific point in time. Using temporal data
management, you view the state of the table at the specified time. In our case, as all transactions pertain to the current date, we
will have to specify a particular timestamps as the point in time.
1. To specify a valid timestamp as the point in time, record the SYSTEM_START time specified for the ‘DB2 WORKS FOR
YOU’ row within the LOANS_HISTORY table. Right-click on the record within the Results tab and select copy row.
SELECT * FROM LOANS_HISTORY;
Figure 11: Copy the row, to retrieve a valid timestamp for the AS OF query
2. Paste the row values into the SQL Editor (press Ctrl + V) to extract the SYSTEM_START timestamp.
3. To search, simply include FOR SYSTEM_TIME AS OF « TIME1 » in the criteria area of a query after the table selection.
Using the criterion, instructs the database to fetch everything adhering to this criterion, including the entries in the
history table.
SELECT * FROM LOANS
FOR SYSTEM_TIME AS OF '2012-01-09 11:08:31 565697'
DB2 10 for Linux, UNIX and Windows Course Workbook Page 184 of 335.
IBM Software
Information Management
Figure 12: Copy the row to retrieve a valid timestamp for the AS OF query
It can be seen from the above result set that at the specified point in time:
• The book ‘DB2 Works for You’ had yet to be marked renewable.
• The transactions for BORROWERID=25 were yet to be deleted.
This shows that you can query the state of your data at any point in time, to easily track how the data transforms over time.
This proves to be invaluable during audits.
Note: The time specified within the AS OF clause is exclusive i.e. the query pulls records with SYSTEM_TIME up to the specified
time.
1. Execute the query below to select from the base table, fetching everything for the current date. Remember that DB2
uses an inclusive-exclusive time period for this clause, hence why we’re specifying “CURRENT DATE + 1 DAY”.
SELECT *
FROM LOANS
FOR SYSTEM_TIME FROM
CURRENT DATE TO CURRENT DATE +1 DAY
WHERE BORROWERID = 25
ORDER BY sys_end;
Figure 13: Query returns all (deleted) loans for BORROWERID= 25.
The result set shows that the records are fetched using both the base and history table. The history table entries end_time has
changed to denote the changes made in the base table. Using the FROM…TO… period specification in the above example,
you can query older versions of data along with the current table data.
The time period specification; BETWEEN…AND… works similarly to the above mentioned FROM… TO…, however, its range of
start/end times uses an inclusive, inclusive approach (i.e. both times are used within the search criteria).
DB2 10 for Linux, UNIX and Windows Course Workbook Page 185 of 335.
IBM Software
Information Management
system-generated history of data changes (system time). ATTs allow companies to manage the past, present and future
effective dates for their business data within a single table, using BUSINESS_TIME.
Note: Unlike SYSTEM_TIME, BUSINESS_TIME periods are provided by the user/ application. DB2 does not generate the start/end
times. It only assists in transparently managing the temporal data.
In our library system scenario, when an item is on loan from the library and not overdue – there is no fee for borrowing the item;
however, upon exceeding the due date, a service fee may be incurred. Using an ATT to manage the valid business-time periods
for the loans makes management of the loan system, considerably easy.
1. To create the LOAN_POLICY table, open (File > Open File…) and execute the script Business_Table.sql in the
/home/db2inst1/Documents/LabScripts/Temporal/ directory.
The first section of the script will create a regular table, and the second portion will alter the table to add the business
time columns, transforming the LOAN_POLICY table into an ATT.
ATTs can also be created in one step with all the required fields specified in the CREATE statement. Here we decided
to break it in two steps to highlight the use of the ALTER statement to convert a regular table into an ATT.
Note: THE DEFAULT is not required within the CREATE TABLE statement for an ATT. When using the ALTER statement
during the conversion of an existing table to an ATT, a DEFAULT value has to be specified for the bus_start and bus_end dates.
They are new NON- NULLABLE columns being added to an existing table.
The LOAN_POLICY table will be used to store information about a library’s loan policy with its effective business start and end
dates. The PERIOD BUSINESS_TIME clause instructs DB2 to use the specified columns to track the start/end times for each
row.
The BUSINESS_TIME WITHOUT OVERLAPS option in the primary key definition signifies that rows with the same key do not
have overlapping periods of business time i.e. there can not be two versions of the same loan policy in effect at any given point
in time.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 186 of 335.
IBM Software
Information Management
1. Populate the table by opening and executing the insert_resources_bus.sql script found in the following directory:
/home/db2inst1/Documents/LabScripts/Temporal.
2. Query the contents of the LOAN_POLICY by executing:
SELECT * FROM LOAN_POLICY;
Figure 14: Successful execution – newly inserted records with their business times.
If you inspect the INSERT statements, you will notice that, unlike STTs, we provided values for the period business time
columns.
The business time values must be explicitly set as it is a business condition that is justified by the end user. Pay closest attention
to the Video Game entries. Notice that although they share the same primary key, they appear several times in the table. This is
allowed because the business times create different pockets of time in which each one of these entries is valid.
When updating an entry, it is important to take into consideration the rows that are affected alongside the time span for which
the update should alter.
1. The library needs to update their policies due to rising operational costs. The fees for all medium need to be increased
by an amount of 5. Execute the following statement:
UPDATE LOAN_POLICY SET fee = fee + 5;
2. Then run the following query to see the changes. You’ll notice that ALL entries were updated.
SELECT * FROM LOAN_POLICY;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 187 of 335.
IBM Software
Information Management
3. The new FOR PORTION OF BUSINESS_TIME clause provides additional control over updating data linked to a
business time. Using it, the library can restrict an update to rows in a specific business time period.
In the following scenario, the library has noticed, in the 3rd quarter of the year, video game rentals are in higher demand,
especially during between July-August. To adjust for this, they increase the penalty of late returns by another 15 dollars
during this period. Update the LOAN_POLICY table to reflect this change.
Attention: In the code below, please substitute YYYY with the current year (eg: 2012).
UPDATE LOAN_POLICY
FOR PORTION OF BUSINESS_TIME FROM
DATE ('YYYY-6-01')
TO
DATE ('YYYY-6-01') + 3 Months
SET fee = fee + 15
WHERE id = 4;
UPDATED row
NEW row
Figure 16: Changed only portion that fits the period specified.
The above results show that not only did DB2 update the fee for the media record corresponding to the months which fall
between July and August; it also splits the row into two rows to signify the change in policy once the portion of time has past.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 188 of 335.
IBM Software
Information Management
The fee for the video game falls back to 55 after the month of August (BUS_END date is exclusive). DB2 adjusts the effects of
the update statement automatically, ensuring that the time dimension of the data remains consistent. This way the user need
only worry about specifying the portion in time where data is to be valid, DB2 manages the changes in state required to update
and maintain the validity periods of the data across its lifespan.
5.2.3 Deleting data for portions of Business Time in an Application-Period Temporal Table
Similar to insertion, data can be deleted for portions of business time. DB2 ensures that the appropriate information from the row
is preserved for the time periods not being affected by the delete statement.
1. Assume CDs are no longer being offered as part of the library program, they were too costly to maintain and were not
efficient. Instead of simply flagging the policy inactive, the library wants to purge the policy from the table. Execute the
following statement to remove the record corresponding to media “CD”.
DELETE FROM LOAN_POLICY
WHERE media = 'CD';
2. Confirm that the loan policy for CDs has been removed.
SELECT * FROM LOAN_POLICY;
Since the DELETE statement didn’t have any restrictions on the business period, the statement would have deleted ALL entries
related to CDs if there were many of them. Just like updates, we can restrict the effect of a DELETE to specific periods of
business time.
3. Consider the library will no longer lend out Video Games during the period between May and September to promote
outdoor activity. This period intersects multiple rows and like updates, DB2 will automatically adjust the business time
values of the records to fit the changes.
a. First review the values.
SELECT *
FROM LOAN_POLICY
WHERE ID = 4
ORDER BY bus_start;
b. Delete the Video Game loan policies for the period of May - August. Remember to replace YYYY with the
appropriate year.
DELETE FROM LOAN_POLICY
FOR PORTION OF BUSINESS_TIME FROM
DATE ('YYYY-05-01')
TO
DB2 10 for Linux, UNIX and Windows Course Workbook Page 189 of 335.
IBM Software
Information Management
DATE ('YYYY-09-01')
WHERE ID = 4;
MONTHS
MAY to
AUGUST
DELETED
Figure 18: Final results after the DELETE statement was executed.
The Video Game loan policy for the BUS_START/ BUS_END time of April/June was automatically changed to reflect the delete
statement. The BUS_END time was changed from 2012-06-01 to 2012-05-01. The records for the business period falling
between May and August were deleted entirely.
6 Bitemporal Tables
Bitemporal tables allow you to track and manage both dimensions of time – System as well as Business. Using bitemporal
tables, companies can become a virtual time machine, having access to their data and its state at any point in time – past,
present or future. This gives companies the ability to not only track the history of changes made by transactions against a
business condition such as an insurance policy but also be able to manage the validity periods of those policies.
5. Business is good for our library, so we need to add new policies and make changes to the existing ones.
Open and execute the script Insert_Bitemporal.sql in Data Studio under the directory
/home/db2inst1/Documents/LabScripts/Temporal/. This script will :
• Add a new loan policy for Blu Ray discs
• Make updates to the Blu Ray policy for the summer months
6. Check the current status of the Blu Ray loan policy by executing:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 190 of 335.
IBM Software
Information Management
added due to change in bus_start month added due to change in fee policy
The above example illustrates the effects of an update statement on a bitemporal table. The time period changes to the
LOAN_POLICY table are reflected using business time, while system time tracks the changing versions of data in the history
table.
The librarian wants to report on all changes made to the ‘Blu-Ray’ loan policies that are effective during the first half of the year.
For that, we need to make use of both system and business time querying capabilities.
1. Execute the following query below. Remember to replace YYYY with the current year.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 191 of 335.
IBM Software
Information Management
Notice that records were retrieved from both the base table and the history table, but only for those records that were valid
in the first half of the year.
Notice that even though the last record was valid until before 01-Sep, it was valid since 01-Jun, which satisfies the query
criteria of being valid during the business time period specified.
As mentioned in earlier sections, DB2’s temporal data management capabilities allow you to ‘travel in time’. Along with querying
data at any given point in time, there is a new registry setting that allows existing applications to run queries against data from a
certain point in time, without using the ATT and STT SQL syntax we demonstrated before. This creates the illusion of having
transported to a given point in time, either in the past or future.
The CURRENT TEMPORAL BUSINESS_TIME special register is used to set business time to a specific value, which will affect
queries and DML operations such as update, delete etc. Setting this register causes DB2 to re-write all queries and operations
using the clause FOR BUSINESS_TIME AS OF <value>.
1. Verify the total number of records within the LOAN_POLICY table for comparison by executing:
SELECT COUNT(*) FROM LOAN_POLICY;
2. Run both statements below at the same time (use your mouse to select both of them and press F5) and see that the
number of rows has changed.
SET CURRENT TEMPORAL BUSINESS_TIME = CURRENT DATE + 3 MONTHS;
SELECT * FROM LOAN_POLICY ORDER BY BUS_START;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 192 of 335.
IBM Software
Information Management
We will not be going through an example for the special register for system time. The register is set the same way as the
business time register. However, when the CURRENT TEMPORAL SYSTEM_TIME register is set to any value except NULL, no
DML operations can be performed against the table.
Note: You can either set a system time (business time) special register or use the SYSTEM_TIME (BUSINESS_TIME) specific time
periods in SQL statements, but NOT both at the same time
DB2 10 for Linux, UNIX and Windows Course Workbook Page 193 of 335.
IBM Software
Information Management
CURRENT VERSION
PREVIOUS VERSION
Figure 24: Previous version of the data, using the base table.
Note: When using Application-Period Temporal Tables, ensure that the record is unique by also specifying business time date (or
the most granular value within the periods defined.)
OPTIONAL EXERCISES
1. To define the same unique indexes on a history as on its base table, one or both of the columns of SYSTEM_TIME
period are to be included in the declaration. In the example below, it would be SYS_START or SYS_END, in the
definition of the UNIQUE index. Create an index to check insertion of overlapping system times into the history table
CREATE UNIQUE INDEX IDX_RESOURCE
ON LOANS_HISTORY (id, sys_end);
In the example, the library committee now wishes to know who authorized the lending of each resource, but the current definition
does not capture that information.
In order to achieve this, both the base and the history table definitions must be altered.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 194 of 335.
IBM Software
Information Management
2. The resulting tables, both LOANS and LOANS_HISTORY now each have an additional column APPROVER. Verify this
by issuing a select query to each table individually from fresh SQL Editors.
Figure 26: The history table with the same additional column.
The database administrator wishes to prevent and accidental dropping of the LOANS table.
Note: The RESTRICT ON DROP attribute may also be added to existing tables using an alter table statement.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 195 of 335.
IBM Software
Information Management
Note: If instead, one removed the attribute RESTRICT ON DROP, when dropping the base table, both the base AND the history
will BOTH be dropped.
It is not possible to drop a column from a table with versioning enabled. This is due to the fact that RESTRICT ON DROP cannot
be applied to columns and to protect accidental dropping of columns with adverse effects, DB2 requires the versioning to be
disabled prior to dropping a column.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 196 of 335.
IBM Software
Information Management
1. The library no longer allows renewals so the column is not needed anymore. To save some space, the column is asked
to be removed to ensure it’s not used (contrary to what the DBA says, the committee is adamant about altering the
schema).
ALTER TABLE LOAN_POLICY DROP COLUMN renewable;
The column cannot be dropped because the tables act as a single unit and there is no way of explicitly protecting the
column from accidental deletion.
Note: Ensure auto-commit is not on if done in a real environment (for the purpose of this lab, it is fine to have it on). This is due to
the fact that the lock will not be held if auto-commit is on.
10 Summary
With the introduction of System-period Temporal Tables, Application-period Temporal Tables and Bitemporal tables, DB2 offers
a powerful and easy way to take control of time-based data. Using simple yet refined SQL enhancements, DB2 allows database
professionals to efficiently manage temporal data, saving time and effort spent on hard-coding time awareness into applications.
Chasing down data changes is now only a simple query away and management of time dependant business conditions takes a
few keystrokes to implement. Gained from this lab was the knowledge to utilize these features and administer them. All of these
features aim to not only manage time, but to help save time and costs for the end user.
11 Cleanup
1. To clean your environment after completing the exercise, right-click on TEMPORAL database in the Administration
Explorer view and select Disconnect.
2. Next, close Data Studio and switch back to the terminal window that you have been using so far.
3. Finally, execute the commands below. Use “password” as password.
su -
cd /home/db2inst1/Documents/LabScripts/Temporal/setup
./cleanup.sh
4. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/Temporal/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 197 of 335.
IBM Software
Information Management
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 198 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 199 of 335.
IBM Software
Information Management
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 About this Lab......................................................................................................................................................................................3
2.1 Environment Setup Requirements...................................................................................................................................3
2.2 Preparing for the lab ........................................................................................................................................................3
2.3 Outline .............................................................................................................................................................................4
3 Creating the DB2 Database ................................................................................................................................................................4
4 Moving Database Objects...................................................................................................................................................................6
4.1 The IBM Data Movement Tool.........................................................................................................................................6
4.1.1 Operation Overview ............................................................................................................................................................................ 6
4.2 Tool Configuration ...........................................................................................................................................................7
4.3 Testing connection to DB2 database...............................................................................................................................9
4.4 Extracting DDL and Data ...............................................................................................................................................10
4.5 Executing the DDL and Importing the Data ...................................................................................................................12
4.5.1 Checking for Errors – DDL Execution............................................................................................................................................... 14
4.5.2 Checking for Errors – Data Loading ................................................................................................................................................. 14
DB2 10 for Linux, UNIX and Windows Course Workbook Page 200 of 335.
IBM Software
Information Management
1 Introduction
Applications within an organization tend to be aligned with a particular database vendor. As business requirements change and
organizations try to switch vendors to take advantage of lower prices, better technology, or a better partnership, they find
themselves held hostage by their earlier choice. Legacy code that requires extensive rewrite, non-standard practices or
proprietary features make the switch difficult. In addition, the skill set for one product cannot be transferred to another very easily.
DB2 database dramatically lowers the barriers for applications written for Oracle® when enabling them to DB2. This provides
customers the ability to choose a DBMS based on its merits rather than application history. With DB2, you do not need to port an
application anymore. You merely enable the application with minimal changes.
2. Open a terminal window by right-clicking on the Desktop area and choose the “Open in Terminal” item.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 201 of 335.
IBM Software
Information Management
2.3 Outline
This lab is divided into the following four sections, as described below:
Section 3 – Create the DB2 database
1. Enable compatibility features for Oracle
2. Issue the CREATE DATABASE command
Section 4 – Move DB schema and data from Oracle database to DB2
1. Extract DDL for the database objects and base data from the Oracle source database
2. Create the database schema (tables, constraints, etc) onto the DB2 database
3. Load data into DB2 database
Section 5 – Create PL/SQL routines in the DB2 database
1. Use PL/SQL support in DB2 to create the Stored Procedures, UDFs and triggers.
2. Explore some typical modifications to accommodate unsupported PL/SQL features.
Section 6 – Enable your application code
1. Modify your application to connect to DB2.
2. Test the application and make necessary adjustments.
You will be using IBM Data Movement Tool and DB2 Command Line Processor (CLP) to carry out the migration tasks.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 202 of 335.
IBM Software
Information Management
Prior to creating the database, the compatibility features for DB2 need to be enabled. These features reduce the complexity and
time required to enable applications from different vendors. To make the DB2 environment more compatible with the Oracle
database management system environment, you can set the DB2_COMPATIBILITY_VECTOR registry variable within DB2.
To enable all of the supported Oracle compatibility features, set the registry variable to the value ORA; using a terminal window,
enter the following commands:
1. Enable all compatibility features
db2set DB2_COMPATIBILITY_VECTOR=ORA
2. Verify the new setting; db2set displays the current compatibility vector setting for the instance.
db2set
3. Stop the DB2 server
db2stop force
4. Start the DB2 server
db2start
5. Create the database with all compatibility features for Oracle enabled. Issue the command below.
db2 create database OB2 on /db2fs pagesize 32 K
Attention: The PAGESIZE option sets the default page size to 32 KB that will be used when creating buffer
pools and table spaces. This is done to accommodate Oracle’s larger row length.
6. After database creation, check that the compatibility features are enabled for your database by listing the database
configuration parameters.
db2 get db cfg for OB2 | grep compat
7. Next step is to set the auto-revalidation parameter for your database to DEFERRED_FORCE. This way you can deploy
objects out of the dependency order. It is also recommended to adjust the rounding behavior to match that of Oracle.
db2 connect to ob2
db2 update db cfg using AUTO_REVAL DEFERRED_FORCE
db2 update db cfg using DECFLT_ROUNDING ROUND_HALF_UP
db2 terminate
DB2 10 for Linux, UNIX and Windows Course Workbook Page 203 of 335.
IBM Software
Information Management
In this section, you will use the IBM Data Movement tool (IDMT) to move your database objects and data from an Oracle
database to a target DB2 database. IDMT is a very simple and powerful tool designed to easily move data and database objects
from various sources. In this section we focus on the non procedural objects like tables, sequences and views.
Attention: Extracted DDL statements for PL/SQL objects are not converted to DB2’s SQL PL syntax.
3. Database Objects Creation and Data Loading – There are three different ways of deploying the extracted objects in
DB2.
Deciding which option to choose to deploy data is based upon the data and objects movement requirements. You can
utilize the scripts generated by IDMT during the extraction process or use interactive deploy.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 204 of 335.
IBM Software
Information Management
• Executing scripts generated by IDMT is useful for all non-procedural objects (tables, constraints, indexes, sequences,
etc.) and loading data.
o Database Objects Creation and Data Loading – The “db2gen” script executes the DDL statements against
the target DB2 database. This script can also be used for data load or a separate script can be generated for
that purpose. More detail will follow in the following sub-sections.
o PL/SQL Objects Creation – “db2plsql_functions.db2”, “db2plsql_procedures.db2”, “db2plsql_triggers.db2” and
others create the procedural objects for the database.
• Using Interactive Deployment option is a good choice when you are also deploying PL/SQL objects such as triggers,
functions, procedures, and PL/SQL packages.
• Deploy DDL/DATA using command line script db2gen.
For the purpose of this lab, we will demonstrate two of the above mentioned options. The db2gen script will be used to
deploy non-procedural objects and data with the Deploy DDL/Data button and the interactive deploy feature will be utilized
for procedural objects such as functions, procedures and triggers.
Attention: Steps marked with a ■ in the following sections cannot be executed. These steps require an Oracle
server that is not provided as part of this environment. Instead, the instructions will show you the results of these
steps.
1. Prior to initializing IDMT, the database user responsible for deploying database objects and data requires the right
permissions. DBADMIN access is required for the user bravousr, to successfully migrate objects to DB2, using IDMT.
To do so, execute the command below in the command line terminal.
db2 connect to ob2
db2 "GRANT DBADM on DATABASE to USER BRAVOUSR"
2. Go to the directory where IBM Data Movement Tool is installed. If you list the files in this directory you will notice the
tool files (IBMDataMovementTool.*) and Oracle’s JDBC driver files are present. This is all you need to get started.
cd /home/db2inst1/Documents/LabScripts/EnablingOpenBravo/IBMDataMovementTool
DB2 10 for Linux, UNIX and Windows Course Workbook Page 205 of 335.
IBM Software
Information Management
3. Run the tool. The GUI screen will open where you can specify the details about the source and destination databases.
./IBMDataMovementTool.sh &
4. You will have to provide the following values:
Source – Oracle Database Target DB2 Database
Vendor Oracle DB2 LUW with Compatibility Mode
Any value other than an Oracle server is
Server Name localhost
not provided with the lab
Port Number 1521 50001
DB2 10 for Linux, UNIX and Windows Course Workbook Page 206 of 335.
IBM Software
Information Management
After setting up the tool for extraction, your settings should resemble the screenshot shown below.
2. ■ To retrieve the list of source schemas, you should click the “Connect to ORACLE” button. You will not see details as
shown in Figure 5, since there is no Oracle server available. Figure 7 is an example of what you would see when a
connection is successfully established.
a. A list with all available schemas is displayed
DB2 10 for Linux, UNIX and Windows Course Workbook Page 207 of 335.
IBM Software
Information Management
Note: The steps marked with a ■ cannot be executed. The steps and corresponding images are outlined for
learning purposes
3. ■ An option is provided to select the schemas intended for migration to DB2. All schemas can be moved or as in the
case for this lab, a particular schema can be chosen for migration to DB2.
2. ■ After clicking the “Extract DDL and Data” button, the GUI screen switches to the “View File” tab, where you can see
the progress of the extraction.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 208 of 335.
IBM Software
Information Management
4. You can click on the View Script/Output button to check the contents of generated scripts, DDL, data or the output log
file. Then, go back to Extract / Deploy tab after checking the output.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 209 of 335.
IBM Software
Information Management
As mentioned in the earlier section, there are three different ways of executing the DDL and deploying the extracted objects in
DB2. All three will be utilized in the following sections.
• Click the Deploy DDL/DATA button from the GUI screen
• Go to the Interactive Deploy tab and deploy objects in step-by-step process
• Deploy DDL/DATA using command line script db2gen
1. Using the first option to deploy non-procedural objects using the script generated by IDMT, we will run the db2gen
script by clicking on the Deploy DDL/Data button to deploy objects.
2. The following pop-up will appear, asking to run the db2gen.sh script. This script executes the previously extracted DDL
statements from the *.sql files to create sequences, tables and associated objects, such as indexes, primary keys,
constraints, etc. It will also import the extracted data using DB2’s LOAD. Click on this YES to create the database
objects.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 210 of 335.
IBM Software
Information Management
3. The deployment results can be viewed in the View file tab. The operation is complete after the foreign keys are
deployed. Please note that this operation might take some minutes. Note that the progress animation ( ) will
disappear as soon as the operation is complete.
The script creates the database schema by running the DDL statements listed below. It also imports the data from the
text files into the DB2 database using the LOAD utility.
• db2tsbp.sql – to create the buffer pools and table spaces
• db2tables.sql – to create the TABLES
• db2default.sql – to create the DEFAULT constraints
• db2sequences.sql – to create the SEQUENCES.
• db2load.sql – to load the DATA extracted from the Oracle database.
• db2check.sql – to create the CHECK constraints
• db2cons.sql – to create PRIMARY KEYS, INDEXES and UNIQUE INDEXES
DB2 10 for Linux, UNIX and Windows Course Workbook Page 211 of 335.
IBM Software
Information Management
To help with this task, we have included a small program to parse the output and give you a list of errors and warnings
generated. You can run this using the checkerrors.sh script located at “opt/IBMDataMovementTool”
cd ../IBMDataMovementTool/
./checkerrors.sh DDL
/home/db2inst1/Documents/LabScripts/EnablingOpenBravo/migr/db2gen.log
DB2 10 for Linux, UNIX and Windows Course Workbook Page 212 of 335.
IBM Software
Information Management
DB2 10 for Linux, UNIX and Windows Course Workbook Page 213 of 335.
IBM Software
Information Management
5. Remove (or comment out) the UNIQUE constraint for DB2. Open the “db2uniq.sql” file within the migr directory, and
look for the UK1_AD_USER constraint (around line 129). Comment out the ALTER TABLE statement. Save your
changes.
gedit db2uniq.sql &
9. On checking the output of the data loading operation. This time there should be no errors.
./checkerrors.sh db2load
/home/db2inst1/Documents/LabScripts/EnablingOpenBravo/migr/db2gen_LOAD.log
DB2 10 for Linux, UNIX and Windows Course Workbook Page 214 of 335.
IBM Software
Information Management
It is important to note that even though DB2 has support for PL/SQL code, you can not mix PL/SQL and DB2’s SQL PL syntax in
the same statement. Separate statements using both SQL PL and PL/SQL syntax can be used inside the same session.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 215 of 335.
IBM Software
Information Management
In this section we start creating Openbravo’s procedural objects in DB2. We will utilize the interactive deploy feature of the IBM
Data Movement tool, for deploying these procedural objects. Interactive deploy is used to deploy all objects within the database.
In our case, we deployed all non-procedural objects using the dbgen.sh script,
2. Click on the folder icon to choose the source directory where all the objects are to be retrieved from. In our case, it is
/home/db2inst1/Documents/LabScripts/EnablingOpenBravo/migr
Folder Icon
The database object tree will become populated with all the database objects.
3. As the deployment process begins, you will notice that the objects deployed successfully are marked with a check
mark sign and failed objects are marked with a red stop icon.
4. Now, we will deploy all objects in the tree to the database. This includes functions, views, triggers and procedures.
The objects already present in the database remain unchanged.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 216 of 335.
IBM Software
Information Management
a. To do this, go to File > Execute all statements or click on the corresponding button from the GUI, as
shown below.
O meu
5. At the end of the deployment, you can see the summary of the deployed objects in a table on the bottom right of the
panel.
Attention: Prior to investigating the errors, re-deploy all objects to ensure that some objects which might
have failed due to dependency issues get correctly deployed.
a. To view error details of the failing objects, you can right-click object in the results pane and select ‘Get
detailed error message’.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 217 of 335.
IBM Software
Information Management
5.2 Functions
In this section, we discuss how to resolve issues with functions outlined by the deployment summary in the previous section
Attention: When re-deploying an object. The above deployment summary is lost. To view the error message again for the object,
right-click on the object, choose “deploy selected objects to db2”. The above window pane would refresh with the selected object’s
deployment status.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 218 of 335.
IBM Software
Information Management
-- v_ProductPrice:=BOM_PriceStd(CUR_BOM.M_ProductBOM_ID,
PriceList_Version_ID);
3. Just before the commented code, add the following to execute your dynamic statement. Notice that the result of the
statement is stored INTO v_ProductPrice and 2 parameters are provided, just like in the original recursive function call.
4. After making the changes above, re-deploy the function by right-clicking on BOM_PRICESTD and selecting Deploy
selected objects to DB2. You will see the following result.
5.3 Views
In our case there are no failing views. If views do fail, it might due to the fact that they are dependant on the functions that have
not been successfully created. These views are marked invalid as the functions they depend on were not created at the time of
deployment. On re-deploying the views DB2 tries to revalidate the functions.
• Select all failing views by pressing down on CTRL while you select them.
• Then right-click on one of the selected view and click on Deploy selected Objects to DB2 or alternatively click on the
button at the top menu bar
Deploy Selected
Objects to DB2
DB2 10 for Linux, UNIX and Windows Course Workbook Page 219 of 335.
IBM Software
Information Management
DB2 10 for Linux, UNIX and Windows Course Workbook Page 220 of 335.
IBM Software
Information Management
Solution:
Instead of adding columns to the GROUP BY clause, add any one of the columns from the GROUP BY list. This will still work
because the SELECT statement is part of a EXISTS clause, i.e. the result values are not used anyway.
1. Click on T_REPLENISH_CREATE. In the source code window pane, replace “*” for ANY of the columns from the
GROUP BY list. Line 57-65
AND EXISTS
(SELECT pp.M_Product_ID Replace * with a column from GROUP BY
FROM M_Product_PO pp
WHERE M_Product_PO.M_Product_ID=pp.M_Product_ID
AND M_Product_PO.C_BPartner_ID=pp.C_BPartner_ID
GROUP BY pp.M_Product_ID,
pp.C_BPartner_ID
HAVING COUNT(*)=1
)
DB2 10 for Linux, UNIX and Windows Course Workbook Page 221 of 335.
IBM Software
Information Management
2. Re-deploy the object by right-clicking on the stored procedure in the object tree and selecting “Deploy selected
objects in DB2”
5.5 Triggers
Now we move on to enable the PL/SQL triggers to DB2 database. You will find that all triggers get deployed. Since DB2 9.7 fp5,
DB2 supports combined action triggers.
At this point you have all your database objects created in DB2 and ready to be used by your application. The next step is to
change your application in order to access the DB2 server.
This part of the process varies depending on the programming language and which database interface you use to communicate
with the database server.
Openbravo ERP is a web-based Java application that uses JDBC as the API to access the database server. Since JDBC is a
standardized, database-agnostic1 API, modifying the application to access the DB2 server is very straightforward. The only
necessary changes are:
• Modify the code so that the application loads the DB2 JDBC driver instead of the Oracle JDBC driver;
• Change the connection string to the format accepted by the DB2 driver;
• Include the DB2 JDBC driver to the set of libraries the application uses.
The rest of the application’s Java code should remain unchanged and you can proceed to testing your system with DB2.
You will be using the Apache Tomcat server (installed at “/opt/tomcat”) to host Openbravo ERP. The Openbravo ERP
application has already been conveniently deployed to Tomcat at “/opt/tomcat/webapps/openbravo”.
6.1.1 Changing JDBC Driver and Connection String
Fortunately, Openbravo ERP uses properties files to store the JDBC driver name and the connection string used to connect to
the database, you only need to replace these values in the appropriate files, avoiding recompiling the application. You will need
to edit 2 files: “dbCon5.xml” and “XmlPool.xml”.
The name of the DB2 JDBC driver type 4 is “com.ibm.db2.jcc.DB2Driver”
The JDBC connection string for the DB2 JDBC driver has the following format “jdbc:db2://<server>:<port>/<databaseAlias>”
1. Switch to directory “/opt/tomcat/webapps/openbravo/WEB-INF”.
cd /opt/tomcat/webapps/openbravo/WEB-INF
2. Open file “XmlPool.xml” for editing.
gedit XmlPool.xml &
3. Edit the parameters below with the following values:
1
Database-agnostic means that the same code written using the API classes and methods can be used to access difference database vendors.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 222 of 335.
IBM Software
Information Management
• dbDriver : com.ibm.db2.jcc.DB2Driver
• dbServer: jdbc:db2://localhost:50001/ob2
DB2 10 for Linux, UNIX and Windows Course Workbook Page 223 of 335.
IBM Software
Information Management
cp /opt/ibm/db2/V10.1/java/db2jcc_license_cu.jar
/opt/tomcat/webapps/openbravo/WEB-INF/lib
You have everything in place now, so let’s start testing the application.
1. Open a new terminal window by right-clicking on your desktop and selecting “Open Terminal”.
2. Switch to root user. The password is “password”.
su -
3. Start the tomcat server.
/opt/tomcat/bin/startup.sh
5. Click the Openbravo link located at the toolbar at the top; or alternatively, enter this URL at the address
bar on Firefox: http://localhost:8080/openbravo/ The login screen will be loaded. It might take a while to load the page at first
because the application is still initializing.
6. Login using the following credential (both username and password are case-sensitive):
• Username: Openbravo
• Password: openbravo
DB2 10 for Linux, UNIX and Windows Course Workbook Page 224 of 335.
IBM Software
Information Management
8 Cleanup
If you want to run this lab again, execute the commands below to drop all newly created database objects and reset the lab
environment:
1. To clean your environment after completing the exercise, execute the command below. Use “password” as password.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 225 of 335.
IBM Software
Information Management
su –
cd /home/db2inst1/Documents/LabScripts/EnablingOpenBravo/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/ EnablingOpenBravo/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 226 of 335.
IBM Software
Information Management
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 227 of 335.
IBM Software
Information Management Technology Ecosystem
DB2® Security
Hands-On Lab
DB2 10 for Linux, UNIX and Windows Course Workbook Page 228 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 Suggested Reading .............................................................................................................................................................................3
3 About this Lab......................................................................................................................................................................................3
4 Environment Setup Requirements.....................................................................................................................................................3
5 Basic Setup ..........................................................................................................................................................................................3
6 Authentication......................................................................................................................................................................................4
6.1 Where Does Authentication Take Place? ........................................................................................................................................................... 4
6.2 Specifying Authentication Type........................................................................................................................................................................... 5
7 Authorization......................................................................................................................................................................................11
7.1 Authorities ......................................................................................................................................................................................................... 11
7.1.1 System-level Authorities ................................................................................................................................................................... 11
7.1.2 Database-level Authorities................................................................................................................................................................ 12
7.2 Privileges .......................................................................................................................................................................................................... 12
7.3 Granting and Revoking Authorities and Privileges ........................................................................................................................................... 13
7.3.1 Adding a new user ............................................................................................................................................................................ 13
7.3.2 Granting Database Privileges ........................................................................................................................................................... 14
7.3.3 Object-level Authorities..................................................................................................................................................................... 16
7.4 Using Views to Define Granular Privileges ....................................................................................................................................................... 19
7.4.1 Creating a View ................................................................................................................................................................................ 20
8 Roles ...................................................................................................................................................................................................23
8.1 Creating Roles .................................................................................................................................................................................................. 23
9 Summary ............................................................................................................................................................................................27
10 Cleanup...............................................................................................................................................................................................28
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 2 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 229 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
Database security is of utmost importance today. Your database may allow customers to purchase products over the Internet, or
it may contain historical data used to predict business trends. In order to protect your data, it is crucial to control who has access
to your database and limit the operations that the user can perform on the data.
2 Suggested Reading
DB2 Security and Compliance Solutions for Linux, UNIX, and Windows (Redbook)
http://www.redbooks.ibm.com/abstracts/sg247555.html
5 Basic Setup
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 3 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 230 of 335.
IBM Software
Information Management Technology Ecosystem
• Username: db2inst1
• Password: password
3. Open a terminal window by right-clicking on the Desktop and choosing the Open in Terminal item:
db2start
6 Authentication
When you first attempt to access an instance or database, the authentication system will try to determine if you are who you say
you are. By default, DB2 works closely with the authentication mechanism of the underlying operating system to verify your user
IDs and passwords. DB2 can also use third-party authentication facilities such as Kerberos or LDAP server to authenticate users.
By using an external authentication system, DB2 does not need to store a redundant set of passwords and sensitive credentials.
This minimizes security vulnerabilities and hacker attacks.
Authentication type defines where and how authentication will take place. This is specified by the AUTHENTICATION
parameter in the database manager configuration file on the server. Thus all the databases in an instance will have the same
authentication type. On the client, the authentication type is specified when a remote database is cataloged.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 4 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 231 of 335.
IBM Software
Information Management Technology Ecosystem
KERBEROS Authentication occurs at the server and is handled by a Kerberos server. The KERBEROS
authentication type is available if both the DB2 server and client operating systems support
Kerberos. The Kerberos security protocol uses conventional cryptography to create a shared
secret key which becomes the credentials used to verify the identity of the user. This eliminates
the need to pass a user ID and password across the network.
KRB_SERVER_ENCRYPT This authentication type is the same as KEBREOS, except it will use SERVER_ENCRYPT if the
client does not support Kerberos security system. If none of these options are available, the
client will receive a connection error and will not be able to connect.
DATA_ENCRYPT Authentication occurs at the server and its behavior is similar to SERVER_ENCRYPT. In this
type of authentication, not only is the password encrypted, but also all user data is encrypted
during transmission between the client and the server.
DATA_ENCRYPT_CMP This type of authentication is identical to DATA_ENCRYPT. However, this setting provides
compatibility to those clients who do not support DATA_ENCRYPT authentication and will
instead connect using SERVER_ENCRYPT so user data will not be encrypted.
GSSPLUGIN Authentication occurs at the server using an external GSS-API plug-in. If the client’s
authentication type is not specified, the server will send a list of server-supported plug-ins to the
client. These plug-ins are listed in the SRVCON_GSSPLUGIN_LIST database manager
configuration parameter. The client then selects the first plug-in found in the client plug-in
directory from the list. If the client does not support any plug-in in the list, the client is
authenticated using the KERBEROS authentication method.
GSS_SERVER_ENCRYPT Authentication occurs at the server using either the GSSPLUGIN or the SERVER_ENCRYPT
authentication method. Authentication uses a GSS-API plug-in and if the client does not support
any of the plug-ins found in the server-supported plug-ins list, the client is authenticated using
KERBEROS. If the client does not support the Kerberos security protocol, the client is
authenticated using the SERVER_ENCRYPT authentication method.
In this section, you will experience how authentication is easily managed through Data Studio.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 5 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 232 of 335.
IBM Software
Information Management Technology Ecosystem
3. Locate the Administration Explorer view. Expand All Databases > localhost > db2inst1. Right-click on the SECLAB
database and select Connect.
5. After a connection has been established, right click on DB2INST1 and select Configure.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 6 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 233 of 335.
IBM Software
Information Management Technology Ecosystem
6. If the connection profile popup appears, select SECLAB and click OK.
7. Select the Instance Tab. Locate the AUTHENTICATION parameter. Double-click the correspondent Pending Value
field and select the SERVER_ENCRYPT authentication. Notice you can not use the Immediate option, therefore you
must restart the instance after changing the authentication type of the db2inst1 instance.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 7 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 234 of 335.
IBM Software
Information Management Technology Ecosystem
8. Click on button to apply the changes. Check the execution status on the SQL Results tab.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 8 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 235 of 335.
IBM Software
Information Management Technology Ecosystem
11. Use the following credentials when prompted and click Finish.
• User: db2inst1
• Password: password
12. Select the first option (force the applications off), then click to execute the db2stop command. Check the SQL
Results tab for errors. If a warning dialog appears to inform you that databases, such as SECLAB, have been
disconnected, click OK to close the dialog.
13. Now start the db2inst1 instance again. Right-click on db2inst1 and select Start.
14. Use the same credentials to execute the command and click Finish.
• User: db2inst1
• Password: password
15. Click to execute the db2start command. Check the SQL Results tab to confirm the command was executed
with success.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 9 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 236 of 335.
IBM Software
Information Management Technology Ecosystem
16. Now you must reconnect to the SECLAB database. Just like before, right-click the SECLAB database and select
Connect. Login with the following credentials:
• User: db2inst1
• Password: password
17. After a connection has been established, right click on DB2INST1 and select Configure.
18. If the connection profile popup appears, select SECLAB and click OK.
19. Select the Instance Tab. Locate the AUTHENTICATION parameter. Notice the authentication value has changed to
SERVER_ENCRYPT.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 10 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 237 of 335.
IBM Software
Information Management Technology Ecosystem
7 Authorization
After a user has been authenticated, authorization serves as the second security mechanism which determines what operations
a user can perform, and which data objects that user can access within a database or instance. The set of permissions granted
to users can come from privileges, authorities, roles, user groups and label-based access control (LBAC) credentials.
Administrative authorities group together certain privileges so that users can perform functions at the database or instance level.
A privilege is a permission to perform an action or task. For instance, privileges define the objects that a user can create or drop
and commands that a user can use to access objects like tables, views, indexes, and packages.
Roles can group together a set of privileges, so these can be assigned to users through the role instead of individually.
LBAC uses security labels to control who has read/write access to individual rows and columns of a table.
7.1 Authorities
Authorities are needed for managing databases and instances and can be divided into two groups:
• System-level authorities
• Database-level authorities
System level authorities enable you to perform instance-wide functions, such as creating and upgrading databases, managing
table spaces, and monitoring activity and performance on your instance. These authorities are granted through the database
manager configuration and can only be assigned to user groups defined at the operating system level.
Below you will find the list of system-level authorities and their descriptions.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 11 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 238 of 335.
IBM Software
Information Management Technology Ecosystem
By default, on Linux systems, the SYSADM group is set to the primary group of the instance owner: DB2GRP1. On Windows,
members of the local Administrators group are all granted SYSADM authority.
1. Using the Configure Parameters tab previously open, type SYSADM_GROUP in the filter text field. As soon as you
start typing, you will notice that Data Studio dynamically filters the parameters. You should see the SYSADM_GROUP
parameter below the filter text box.
`
Figure 16 - Close the "Configure Parameters" tab
Database level authorities enable you to perform functions within a specific database, such as granting and revoking privileges,
inserting, selecting, deleting and updating data, and managing workloads.
7.2 Privileges
A privilege is a permission to perform an action or task. They can be obtained in three different ways:
• Explicitly: These are the privileges explicitly granted to a user/group/role using the GRANT command.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 12 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 239 of 335.
IBM Software
Information Management Technology Ecosystem
• Implicitly: When a user creates a database object, that user will implicitly receive all privileges for that object. For
example, when a user creates a database, that user implicitly receives DBADM authority for that database.
• Indirectly: An indirect privilege is usually associated with a package. When a user is given permission to execute a
package, the user may not have privileges to access the objects used by the package. Therefore, the user will be
indirectly given these privileges temporary in order to execute the package.
So far, you have been issuing all database commands as the instance administrator (db2inst1) which has privileges to access all
the utilities, data, and database objects within DB2. It is important that users be only given privileges that are necessary to
complete their tasks.
In the following scenario, a new member (USERDEV) has joined your team. We will look at how to assign specific authorities
and privileges to him to safeguard the security of the database.
USERDEV is an application developer and as such he requires SELECT, INSERT, UPDATE, and DELETE privileges to access
the various tables in the database. USERDEV should also be able to add new packages to the database and execute them;
therefore, he needs BINDADD authority.
1. Open a terminal window by right-clicking on the Desktop and choosing the Open in Terminal item:
As mentioned before, DB2 relies on the OS authentication mechanism, so we need to create the new users and groups
at the operating system level.
2. In the terminal window, execute the commands below to: 1) Log in as root user (use ‘password’ as password); 2) Add
a new user called USERDEV; 3) Change USERDEV’s password to ‘password’.
su -
useradd userdev
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 13 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 240 of 335.
IBM Software
Information Management Technology Ecosystem
passwd userdev
exit
When the new user is added, this user he has no authorities or privileges other than those defined in the PUBLIC group.
1. Switch to Data Studio. In the Administration Explorer, expand SECLAB > Users and Groups. Right-click on Users and
select Create User.
2. In the Properties view, input userdev into the Name field and select the Privileges option.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 14 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 241 of 335.
IBM Software
Information Management Technology Ecosystem
3. The Connect should be already selected. Check the BINDADD and CREATETAB privileges as well.
4. Next, locate the toolbar on the top right corner ( ) and click on the Review and Deploy Changes
The Review and Deploy screen will display the commands that were generated to apply the desired changes.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 15 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 242 of 335.
IBM Software
Information Management Technology Ecosystem
6. Check the SQL Results tab to ensure the commands were executed without errors.
USERDEV has now privileges to connect, bind a package and create tables in the SECLAB database.
7. Switch to the previously open terminal window. Try again to connect to SECLAB database as USERDEV.
db2 CONNECT TO SECLAB USER userdev USING password
1. In the same terminal window, try to query the EMPLOYEE table. You will notice that USERDEV does not have
privileges on EMPLOYEE table.
db2 "SELECT * FROM DB2INST1.EMPLOYEE"
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 16 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 243 of 335.
IBM Software
Information Management Technology Ecosystem
2. Go back to Data Studio and locate the SECLAB database on the Administration Explorer view. Select the Tables
folder. Data Studio will list all SECLAB tables into the main view. Locate the Filter text box and type EMPLOYEE into
the text box. Notice that Data Studio’s dynamic filter will display the EMPLOYEE table.
3. Right click on EMPLOYEE table and select the Manage Privileges option.
4. Locate the USERDEV and tick the SELECT, INSERT, UPDATE and DELETE privileges.
Note: a single tick means the user has that privilege. Double-tick means the user has that privilege with the ability to
GRANT the privilege to other users (WITH GRANT OPTION).
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 17 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 244 of 335.
IBM Software
Information Management Technology Ecosystem
5. Next, locate the toolbar on the top right corner ( ) and click on the Review and Deploy Changes
The Review and Deploy screen will display the commands that were generated to apply the desired changes.
7. Finally, check the SQL Results tab to ensure the commands were executed without errors.
USERDEV now has the privilege to interact with the SECLAB database.
8. Switch back to the terminal and try the same query again user USERDEV. You will notice that the operation will now
succeed.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 18 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 245 of 335.
IBM Software
Information Management Technology Ecosystem
There are three ways in which access to specific portions of data in a table can be restricted: views, Label-Based Access
Control (LBAC) and the new Row and Column Access Control (RCAC). The use of LBAC and RCAC is beyond the scope of this
lab, so we will instead focus on the implementation of views.
Views are virtual tables (computed dynamically and not stored explicitly) that are derived from one or more tables or views.
They can be used to provide a customized subset of data to the users, allowing them to see different presentations of the same
set of data or hide data to which a user should not have access. Views can perform delete, insert, update operations or be read-
only.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 19 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 246 of 335.
IBM Software
Information Management Technology Ecosystem
• Create a view that returns the portion of the data that the user should have access to.
• Grant the user access to the view.
• Revoke from the user access to the base table.
We would like to create a view that returns a directory of only those who are in department E11. This view will contain only first
name, last name, phone number, and job role.
1. Switch back to Data Studio, right-click the Views folder and select Create View option.
The new view will be added at the bottom of the list and you will be able to define its definitions using the Properties
view. Select the General tab, enter E11INFO, and then select the SQL tab.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 20 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 247 of 335.
IBM Software
Information Management Technology Ecosystem
3. Type the following SELECT statement to the Expression field, and then press the Update button to check the syntax.
4. Next, locate the toolbar on the top right corner ( ) and click on the Review and Deploy Changes
button ( ). The Review and Deploy screen will display the commands generated to reflect the desired changes.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 21 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 248 of 335.
IBM Software
Information Management Technology Ecosystem
6. Finally, check the SQL Results tab to ensure the commands were executed without errors.
The view E11INFO is now created and a user issuing a select statement against the view will see only the four columns
defined.
7. In Administration Explorer, on the menu bar, click and select New SQL Script:
8. In the editor, enter the SQL statement below and press to execute the statement.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 22 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 249 of 335.
IBM Software
Information Management Technology Ecosystem
From now on, any new users that require access only to employees of department E11 can be granted access to the
E11INFO view instead of the EMPLOYEE base table.
8 Roles
A role is a database object that may group together one or more privileges and can be assigned to users, groups, PUBLIC or to
other roles via a GRANT statement. Roles simplify the administration and management of privileges.
Consider now that your team is expanding and more application developers have joined in. Since all developers share the same
access requirements, roles can be used instead of managing privileges individually to each user.
The security administrator holds the authority to create, drop, grant, revoke, and comment on a role.
1. Switch back to Data Studio and expand the Users and Groups folder on Administration Explorer View. Then, right
click on Roles and select Create Role.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 23 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 250 of 335.
IBM Software
Information Management Technology Ecosystem
4. Find the Table tab on the top. You might need to use the show list button ( ) if tab is not be visible.
5. Click on the grant new privilege button ( ), expand the db2inst1 schema, select the EMPLOYEE table and click OK.
Notice that the EMPLOYEE table is now visible in the Privileges tab.
7. Next, locate the toolbar on the top right corner ( ) and click on the Review and Deploy Changes
button ( ). The Review and Deploy screen will display the commands that were generated to apply the desired
changes.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 24 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 251 of 335.
IBM Software
Information Management Technology Ecosystem
9. Finally, check the SQL Results tab to ensure the commands were executed without errors.
The role Developer is now created. Now you must grant the role Developer to user USERDEV.
10. Click on the Users folder, right click on USERDEV and select Alter.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 25 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 252 of 335.
IBM Software
Information Management Technology Ecosystem
11. Click on the Roles tab, then click on the grant new privilege button ( ).
13. Next, locate the toolbar on the top right corner ( ) and click on the Review and Deploy Changes
button ( ).
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 26 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 253 of 335.
IBM Software
Information Management Technology Ecosystem
The Review and Deploy screen will display the commands that were generated to apply the desired changes.
14. Select the Run option and click the Finish button.
15. Finally, check the SQL Results tab to ensure the commands were executed without errors.
The role Developer is now granted to user USERDEV. As more developers join the team, the Developer role can be
assigned to them in the same manner we did for USERDEV. In the future, if developers require any changes to their
security access, the security administrator can simply modify the privileges of the Developer role. All users associated
to this role will automatically inherit the new privileges.
9 Summary
This lab presented an overview of the basic security mechanisms in DB2 for LUW.
Before connecting to a DB2 database, users must be authenticated, i.e. their user ID and password are validated. DB2 does not
store user/password information in the database. Instead, it relies on an external authentication mechanism, by default the OS
authentication mechanism.
After a user is authenticated, DB2 performs an authorization check, where the database manager verifies if the user is
authorized to perform the requested operations on specific data objects. Users can be granted specific privileges, authorities or
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 27 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 254 of 335.
IBM Software
Information Management Technology Ecosystem
roles. DB2 also allows you to create your own database roles to bundle several privileges together so you can grant them at
once to a user, group or role.
10 Cleanup
1. To clean your environment after completing the exercise, right-click on SECLAB database, select Disconnect. Close
Data Studio and execute the commands below. Use “password” as password.
su –
cd /home/db2inst1/Documents/LabScripts/Security/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/Security/setup
./config.sh
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 28 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 255 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 Security
© Copyright IBM Corp. 2012. All rights reserved Page 29 of 29
DB2 10 for Linux, UNIX and Windows Course Workbook Page 256 of 335.
IBM Software
Information Management
DB2 10 for Linux, UNIX and Windows Course Workbook Page 257 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction..........................................................................................................................................................................................3
2 Objective...............................................................................................................................................................................................3
3 Environment Setup Requirements.....................................................................................................................................................3
4 Laboratory Setup .................................................................................................................................................................................3
5 Implementing Row and Column Access Control on IBMCARD Database.....................................................................................4
5.1 Initial Steps ......................................................................................................................................................................................................... 6
5.2 Database Authorities........................................................................................................................................................................................... 7
5.3 Implementing Row Permissions.......................................................................................................................................................................... 7
5.4 Implementing Column Masks............................................................................................................................................................................ 12
5.5 Verifying the Restrictions .................................................................................................................................................................................. 14
5.6 Adding a New Customer ................................................................................................................................................................................... 15
5.7 Updating Customers’ Options ........................................................................................................................................................................... 16
5.8 Other Operations .............................................................................................................................................................................................. 17
6 Summary ............................................................................................................................................................................................18
7 Cleanup...............................................................................................................................................................................................18
DB2 10 for Linux, UNIX and Windows Course Workbook Page 258 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
In order to comply with various government regulations, companies need to implement rules to ensure that information is
adequately secured. As a result, individuals should only be allowed to access the subset of data needed to perform their tasks.
DB2 10 introduces a new feature called Row and Column Access Control (RCAC). RCAC acts as an additional layer of data
security by controlling access to a table at the row level, column level, or both, complementing the table privileges model.
RCAC has several advantages over existing mechanisms. For example, no database user is exempted from the RCAC rules.
This means that even higher-level authorities, such as users with DATAACCESS authority, are not exempt from these rules.
RCAC also ensures that table data is protected regardless of how that table is accessed. Regardless of whether the data is
accessed via direct SQL, application logic, ad-hoc query tools, report generation tools, or even browsing through IBM Data
Studio, all users are subject to RCAC rules. A further advantage of the RCAC solution is that no application changes are
required to take full advantage of this additional layer of data security. Consequently, the feature is transparent to existing
applications.
The RCAC feature represents an important shift in paradigm; it is no longer about what data is being asked, but rather who is
asking for the data. Depending on who requested data, result sets for the same query can differ immensely.
2 Objective
The exercises in this laboratory are designed to help you understand how Row and Column Access Control works, and what
steps are required to implement data access restrictions with it.
4 Laboratory Setup
The lab’s users have already been set up for you, along with the database and tables required for the lab. This lab uses five
users: ADAM, JILL, PAMELA, ERIC and ANTHONY. Each of their passwords has been set to “password”. The database that
has been created is named IBMCARD. There are two tables in this database: CUSTOMER and MARKETINGCHOICE.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 259 of 335.
IBM Software
Information Management Technology Ecosystem
This scenario considers the following columns within the CUSTOMER table:
Column Description
SSN The customer’s social security number.
NAME The customer’s name.
ADDRESS The customer’s address.
CARD_NUMBER The customer’s credit card number.
PHONE The customer’s phone number.
BALANCE The customer’s balance.
MGR_ID The customer’s primary care account manager database ID.
The MARKETINGCHOICE table stores individual customer information, which decides whether a customer wants to participate
in IBMCARD’s marketing programs.
This scenario considers the following columns within the MARKETINGCHOICE table:
Column Description
SSN The customer’s social security number is used to match customers with their
choices.
VALUE The decision made by the customers about the marketing program.
For example, the row (123-45-6789, yes) says that customer with SSN 123-45-
6789 agrees to receive marketing information.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 260 of 335.
IBM Software
Information Management Technology Ecosystem
It is important for the data in IBMCARD’s database systems to be secure and only be accessible by privileged users. The first
column outlines the policies and the challenges faced by the IBMCARD organization, while the second column outlines the
feature of RCAC that best addresses the challenge.
Table 3 – How DB2’s Row and Column Access Control feature secures information
In this scenario, a number of different people create, secure and use IBMCARD data. Each person has different user rights and
database authorities. Internal and external access to data is based on the separation of duties to users who access the data and
their data access privileges. IBMCARD created the following database roles to separate these duties:
Name Duties
ADAM IBMCARD Chief Security Administrator. He holds the SECADM authority
JILL IBMCARD Marketing Department. He belongs to the MARKETING role.
ERIC IBMCARD Accounting Department. He belongs to the ACCOUNTING role.
PAMELA IBMCARD Accounting Department. She belongs to the ACCOUNTING role.
ANTHONY IBMCARD Membership Manager. He belongs to the MEMBERSHIP role.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 261 of 335.
IBM Software
Information Management Technology Ecosystem
--Grant authority
GRANT SELECT, UPDATE ON TABLE IBMCARD.CUSTOMER TO ROLE ACCOUNTANT;
GRANT SELECT, INSERT ON TABLE IBMCARD.CUSTOMER TO ROLE MEMBERSHIP;
GRANT UPDATE (SSN, NAME, ADDRESS, PHONE, CARD_NUMBER) ON TABLE
IBMCARD.CUSTOMER TO ROLE MEMBERSHIP;
GRANT SELECT ON TABLE IBMCARD.CUSTOMER TO ROLE MARKETING;
GRANT INSERT, SELECT, DELETE, UPDATE ON TABLE IBMCARD.MARKETINGCHOICE TO ROLE
MEMBERSHIP;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 262 of 335.
IBM Software
Information Management Technology Ecosystem
10. After looking at the code, press “q” to exit the script. Next, connect to the database and run this script by executing the
following commands:
db2 connect to IBMCARD
db2 -tvf 01_PREPARE_IBMCARD_ENVIRONMENT
The roles are now created and the proper permissions are assigned to each database user.
Security administration and the security administrator (SECADM) role play important parts in securing both the CUSTOMER
table and company data at IBMCARD.
At IBMCARD, the management team decided to create a role for administering access to their data. The team also decided that
even users with DATAACCESS authority are not allowed to view protected personal data by default.
The management team selects ADAM to be the sole security administrator for IBMCARD. From now on, ADAM controls all data
access authority. With this authority, ADAM defines security rules such as row permissions, column masks, and whether
functions and triggers are secure or not. These rules are used to control user access to data on the system.
ADAM now becomes the sole authority to grant data access to others within and outside IBMCARD.
Row permissions are one of the functions provided by DB2’s row and column access control feature. These are used to restrict
or filter rows based on the user who has logged on to the database. At IBMCARD, the row permissions create a record
restriction on the CUSTOMER table.
ADAM, IBMCARD’s Security Administrator, must connect to the database, so that he can create row permissions and activate
row access control in the CUSTOMER table.
In order to create row permissions and column masks, ADAM must be able to alter the CUSTOMER table definition.
ADAM will implement the following permissions to restrict who can see any given row on a need-to-know basis:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 263 of 335.
IBM Software
Information Management Technology Ecosystem
• Each user belonging to the ACCOUNTANT role is permitted to view the data of the customers under his
responsibility, but not the data of customers who are under other accountants.
• Users belonging to the MEMBERSHIP role can access all CUSTOMER information.
• Users belonging to the MARKETING role are allowed to access the information of only those customers who
opted for receiving marketing calls.
Before creating the permissions, let’s make sure all users have access to customers’ information.
4. Select the tab labeled PAMELA (accountant) and execute the following command in order to connect to IBMCARD
database as PAMELA:
db2 CONNECT TO IBMCARD USER PAMELA USING password;
5. Then, execute the following SQL statement to retrieve the customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
4 Record(s) selected
PAMELA is able to retrieve all customers’ information. However, according the new rules, PAMELA must only see her
own customers.
6. Select the tab labeled ANTHONY (membership manager) and execute the following command in order to connect to
IBMCARD database as ANTHONY:
db2 CONNECT TO IBMCARD USER ANTHONY USING password;
7. Then, execute the following SQL statement to retrieve the customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
4 Record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 264 of 335.
IBM Software
Information Management Technology Ecosystem
4 Record(s) selected.
So far, only Sam and Max opted to receive marketing calls, therefore JILL, IBMCARD’s marketing operator, shouldn’t
be able to retrieve Mike and Dug’s information.
9. Select the tab labeled JILL (marketing operator) and execute the following command in order to connect to IBMCARD
database as JILL:
db2 CONNECT TO IBMCARD USER JILL USING password;
10. Then, execute the following SQL statement to retrieve the customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
4 Record(s) selected.
11. ADAM starts implementing permissions for the ACCOUNTANT role by creating the
02_CREATE_PERMISSION_ACCOUNTANT script.
Go back to the tab labeled ADAM (belonging to ADAM the security administrator) and execute the command below to
review the script created:
less 02_CREATE_PERMISSION_ACCOUNTANT
DB2 10 for Linux, UNIX and Windows Course Workbook Page 265 of 335.
IBM Software
Information Management Technology Ecosystem
IBMCARD.CUSTOMER.MGR_ID = SESSION_USER)
ENFORCED FOR ALL ACCESS
ENABLE;
12. Now press “q” to exit the script. Run the script by executing the following command:
db2 -tvf 02_CREATE_PERMISSION_ACCOUNTANT
The permission is created in the database but the row permission feature is not activated. Therefore you won’t see the
correct results yet. We will activate row access control after creating all permissions.
13. Then, ADAM implements the permission for MEMBERSHIP role by creating the
03_CREATE_PERMISSION_MEMBERSHIP script. In order to view the script, execute the command below:
less 03_CREATE_PERMISSION_MEMBERSHIP
14. Now press “q” to exit the script. Run the script by executing the following command:
db2 -tvf 03_CREATE_PERMISSION_MEMBERSHIP
15. Next, ADAM implements the permission for MARKETING role by creating the
04_CREATE_PERMISSION_MARKETING script. In order to view the script, execute the command below:
less 04_CREATE_PERMISSION_MARKETING
CREATE PERMISSION ROW_ACCESS_MARKETING ON IBMCARD.CUSTOMER
-------------------------------------------------------
-- MARKETING are allowed to access the rows where the customers
-- opted for receiving marketing calls.
------------------------------------------------------------
FOR ROWS WHERE
(VERIFY_ROLE_FOR_USER(SESSION_USER, 'MARKETING') = 1 AND
EXISTS (
SELECT 1 FROM IBMCARD.MARKETINGCHOICE C
WHERE IBMCARD.CUSTOMER.SSN = C.SSN AND C.VALUE = 'yes'))
ENFORCED FOR ALL ACCESS
ENABLE;
DB2 10 for Linux, UNIX and Windows Course Workbook Page 266 of 335.
IBM Software
Information Management Technology Ecosystem
16. Now press “q” to exit the script. Run the script by executing the following command:
db2 -tvf 04_CREATE_PERMISSION_MARKETING
Finally, Adam activates the row access control feature in the IBMCARD database.
17. Execute the following alter statement to activate row access control on table Customer:
db2 ALTER TABLE IBMCARD.CUSTOMER ACTIVATE ROW ACCESS CONTROL;
All permissions are now activated in the customer table. All data in IBMCARD.CUSTOMER table is now protected.
18. Switch to the tab labeled PAMELA (accountant) and execute the following SQL statement to retrieve the customers’
information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
2 Record(s) selected.
You may recall that the last time PAMELA ran this command; she could see all data in the table. This time, PAMELA is
now allowed to retrieve only her costumers’ records. Row access control has been successful!
19. Then, switch to the tab labeled ANTHONY (membership manager) and execute the following SQL statement to retrieve
the customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
4 Record(s) selected.
As can be seen, ANTHONY is still able to retrieve all customers’ information. According to our earlier model, this is as
expected.
20. Finally, switch to the tab labeled JILL (marketing operator) and execute the following SQL statement to retrieve the
customers’ information:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 267 of 335.
IBM Software
Information Management Technology Ecosystem
2 Record(s) selected.
As we would expect from our earlier model, JILL is now allowed to retrieve only customers who opted for receiving
marketing calls.
Later, ADAM is asked by management to protect the customers’ balance information. The BALANCE column must only be
accessible to the users in the ACCOUNTANT role.
ADAM restricts data access on the IBMCARD database by using column masks, a part of DB2’s row and column access control
feature. Column masks hide data returned to users unless they are permitted to view the data.
ADAM creates the 05_CREATE_COLUMN_MASK_BALANCE script, which implements column masks such that a user in each
role is restricted to view a result set that they are privileged to view.
1. As ADAM must create the column mask, switch back to the tab labeled ADAM (security administrator) and view the
script by executing the command below:
less 05_CREATE_COLUMN_MASK_BALANCE
2. Now press “q” to exit the script. Run the script by executing the following command:
db2 -tvf 05_CREATE_COLUMN_MASK_BALANCE
A column mask is not applied until it is activated on the table for which it was defined. Therefore, the security
administrator (ADAM) must now activate the column access control.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 268 of 335.
IBM Software
Information Management Technology Ecosystem
Let’s check if the changes were correctly applied in the database. As ANTHONY has the MEMBERSHIP role, he
shouldn’t be able to see the clients’ balance information.
4. Switch to the tab labeled ANTHONY (membership manager) and execute the following SQL statement to retrieve the
customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
4 Record(s) selected.
As expected by our model above, ANTHONY is not able to retrieve customers’ balance information. He simply sees a
balance of 0.00.
In order to implement the above restrictions, ADAM created the following scripts:
• 06_CREATE_COLUMN_MASK_SSN
• 07_CREATE_COLUMN_MASK_CARD_NUMBER
5. In order to view each script, switch to the tab labeled ADAM (security administrator) and execute the commands below.
Press “q” to exit the script before moving to the next one.
less 06_CREATE_COLUMN_MASK_SSN
DB2 10 for Linux, UNIX and Windows Course Workbook Page 269 of 335.
IBM Software
Information Management Technology Ecosystem
less 07_CREATE_COLUMN_MASK_CARD_NUMBER
6. Go to the tab labeled ADAM (security administrator) and execute the following command:
db2 -tvf 06_CREATE_COLUMN_MASK_SSN
7. Execute the corresponding command:
db2 -tvf 07_CREATE_COLUMN_MASK_CARD_NUMBER
ADAM observes that after creating these column masks, the data is only viewable to intended users. The CUSTOMER table has
already had column access control activated.
1. In order to verify the restrictions are applied, go to the tab labeled ANTHONY (membership manager) and execute the
following SQL statement to retrieve the customers’ information:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 270 of 335.
IBM Software
Information Management Technology Ecosystem
4 Record(s) selected.
As expected, ANTHONY is still able to retrieve the customers’ SSN and CARD_NUMBER information.
2. Switch to tab labeled JILL (marketing operator) and execute the following SQL statement to retrieve the customers’
information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
2 Record(s) selected.
The SSN, CARD_NUMBER and BALANCE columns are now masked and JILL, who is a marketing operator, can’t see
this information anymore.
3. Finally, switch to the tab labeled PAMELA (accountant) and execute the following SQL statement to retrieve the
customers’ information:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
2 Record(s) selected.
Melanie is a new customer, and her records must be added to the IBMCARD database. A user with the required security
authority must create the new record for Melanie. ANTHONY, with the MEMBERSHIP role, enrolls Melanie as a new member
and assigns PAMELA as her account manager. ANTHONY created the ADD_CUSTOMER_MELANIE script to add Melanie to
the IBMCARD database.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 271 of 335.
IBM Software
Information Management Technology Ecosystem
3. Now press “q” to exit the script. Run the script by executing the following command:
db2 -tvf 08_ADD_CUSTOMER_MELANIE
4. ANTHONY confirmed that Melanie was added to the database by querying the same from the CUSTOMER table in the
IBMCARD database:
db2 "SELECT * FROM IBMCARD.CUSTOMER WHERE NAME ='Melanie'";
1 Record(s) selected.
As the row and column access control feature is still activated and Melanie opted to not receive any marketing calls,
therefore JILL (the marketing operator) shouldn’t be able to see Melanie’s information.
5. In order to verify this, select the tab labeled JILL (marketing operator) and execute the following command:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
2 Record(s) selected.
Let us say that Melanie called ANTHONY from IBMCARD Membership department, to change her marketing choice so that she
can now receive marketing offers.
1. Switch back to the tab labeled ANTHONY (membership officer) and execute the following command in order to update
Melanie’s information:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 272 of 335.
IBM Software
Information Management Technology Ecosystem
2. Then, select the tab labeled JILL (marketing operator) and execute the following command:
db2 "SELECT * FROM IBMCARD.CUSTOMER";
3 Record(s) selected.
After the update, JILL is now able to see Melanie’s information Therefore she can now call Melanie to offer marketing
programs.
As RCAC only allows users to access the subset of data that is needed to perform their tasks.
Let’s see what happens when PAMELA, an IBMCARD accountant, tries to update the balance of all customers in IBMCARD
database.
1. To do so, select the tab labeled PAMELA (accountant) and execute the following command:
db2 "UPDATE IBMCARD.CUSTOMER SET BALANCE = 100.00";
This operation should change all clients’ balance to 100.00, but as PAMELA only has access to his customers, the other
customers who are managed by other accountants (for example, ERIC) won’t be affected by PAMELA’s operation.
3 Record(s) selected.
3. Now, let’s verify the balance of Eric’s customers. Select the tab labeled ERIC (accountant) and execute the following
command in order to connect to IBMCARD database as ERIC:
db2 CONNECT TO IBMCARD USER ERIC USING password;
4. Then, execute the following SQL statement to retrieve ERIC’s customers’ information:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 273 of 335.
IBM Software
Information Management Technology Ecosystem
2 Record(s) selected.
You can try the same operation using the “DELETE” command instead of “UPDATE”. The behavior will be the same.
Only the data visible to each user will be affected by their actions.
6 Summary
You can see by now that DB2 Row and Column Access Control is a powerful feature that can help your business secure its
critical information. By acting as an additional layer of data security, it can allow access-control at the row level, column level or
both without having the need to modify your already existing application. Over the course of this laboratory, you have seen how
efficient and easy it is to create and implement row permissions and column masks. You have also seen that even higher-level
authorities, such as users with DATAACCESS authority, are not exempt from these rules, regardless of how that table is
accessed. The best part may be that this is just a subset of all that the DB2 Row and Column Access Control feature can do.
7 Cleanup
1. To clean up your environment for the next lab, go to the first tab (belonging to DB2INST1, the database administrator)
and run the commands below. Use “password” as password.
db2 force application all;
su -
cd /home/db2inst1/Documents/LabScripts/RCAC/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/RCAC/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 274 of 335.
IBM Software
Information Management
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 275 of 335.
IBM Software
Information Management Technology Ecosystem
DB2 10 for Linux, UNIX and Windows Course Workbook Page 276 of 335.
IBM Software
Information Management Technology Ecosystem
Table of Contents
1 Introduction 3
2 About this Lab 3
3 Environment Setup Requirements 3
4 Initial Steps 3
5 Utility Configuration 4
6 Ingesting Data 5
7 Monitoring Ingest Operations 6
8 Handling Data Errors 8
9 Recovering Failed INGEST Command 11
10 Data Manipulation with Ingest Utility 13
11 Sample CDI Project 16
12 Summary 17
13 Cleanup 17
DB2 10 for Linux, UNIX and Windows Course Workbook Page 277 of 335.
IBM Software
Information Management Technology Ecosystem
1 Introduction
The ingest utility, or CDI (Continuous Data Ingest), is a high-speed client-side DB2® utility capable of streaming data from files
and pipes into DB2 target tables. Unlike the import and load utilities, the ingest utility does not lock target tables when moving
large amounts of real-time data, so you don’t have to choose data availability at the expense of data concurrency. Data is
ingested at speeds high enough to populate even large tables in partitioned database environments without affecting data
availability. Therefore business decisions can be taken based on the most recent data, leading to effective business intelligence.
4 Initial Steps
DB2 10 for Linux, UNIX and Windows Course Workbook Page 278 of 335.
IBM Software
Information Management Technology Ecosystem
4. Ensure that the DB2 Database Manager has been started by issuing the following
db2start
A message like the following should be displayed to indicate that the DB2 Database Manager has been started:
db2inst1@db2beta3:~/Desktop> db2start
01/05/2012 14:36:11 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
5 Utility Configuration
By default, failed INGEST commands are restartable from the last commit point. A restart table needs to be created before
running the ingest utility to capture information needed to resume a failed ingest operation, otherwise you will receive an error
message notifying you that the command issued is not restartable. The ingest utility uses this table to store information needed
to resume an incomplete INGEST command.
1. A connection needs to be established to the INGEST database first. Issue the following command in the terminal.
db2 CONNECT TO INGEST
After the command is successfully executed, a status of 0 is returned and table SYSTOOLS.INGESTRESTART is created.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 279 of 335.
IBM Software
Information Management Technology Ecosystem
There are a few configuration parameters specifically designed for the INGEST utility. These parameters can be set with the
INGEST SET command. The setting affects only later INGEST commands in the same CLP session that share the same
CLP backend process. It does not affect INGEST commands in other sessions or later sessions that use a different CLP
backend process. In this section, you will set commit_count and commit_period configuration parameters for the
environment of this lab.
Note: commit_count specifies the number of rows each flusher writes in a single transaction before issuing a commit. It
should be a number multiple of 1000. The default value is 0.
4. Set commit_period to 0.
db2 INGEST SET commit_period 0
Note: commit_period specifies the number of seconds between committed transactions. The default value is 1. If
commit_period is set to 0, the commit_count configuration parameter is used.
Note: in this lab, we choose to commit transactions based on the number of rows only.
6 Ingesting Data
1. Change directory to /home/db2inst1/Documents/LabScripts/CDI, where the scripts needed for this lab are stored.
cd /home/db2inst1/Documents/LabScripts/CDI/
2. Now that you have set the configuration parameters, you are ready to ingest data. Ingest all 100,000 records from the
pre-populated file ingest_data_100k to table SALES.
db2 "INGEST FROM FILE './ingest_data_100k' FORMAT DELIMITED INSERT INTO
sales"
When the INGEST command starts executing, you will see a message like the following, where the timestamp indicates
the exact time at which the ingest utility is started and the job ID is a unique number that identifies the INGEST
command being run. In the case of an execution failure, you can use this ID to restart the failed operation from the last
commit point.
db2inst1@db2v10:~/Documents/LabScripts/CDI> db2 "INGEST FROM FILE './ingest_data_100k'
FORMAT DELIMITED INSERT INTO sales"
SQL2979I The ingest utility is starting at "01/06/2012 09:26:00.324748".
SQL2914I The ingest utility has started the following ingest job:
"DB21001:20120106.092600.324748:00002:00016".
DB2 10 for Linux, UNIX and Windows Course Workbook Page 280 of 335.
IBM Software
Information Management Technology Ecosystem
When the ingest operation finishes executing, a summary is displayed in the terminal like the one below:
db2inst1@db2v10:~/Documents/LabScripts/CDI> db2 "INGEST FROM FILE './ingest_data_100k'
FORMAT DELIMITED INSERT INTO sales"
SQL2979I The ingest utility is starting at "01/06/2012 09:26:00.324748".
SQL2914I The ingest utility has started the following ingest job:
"DB21001:20120106.092600.324748:00002:00016".
In this lab, you will experiment the INGEST LIST and INGEST GET STATS commands.
1. You will re-use table SALES to ingest more data. So truncate the SALES table first by issuing the following command:
db2 TRUNCATE TABLE SALES REUSE STORAGE IMMEDIATE
2. To monitor INGEST operations, you need a separate session but it must be run on the same machine that the INGEST
command is running on. In the terminal that you have been using so far, click File on the top menu bar, then select
Open Terminal.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 281 of 335.
IBM Software
Information Management Technology Ecosystem
3. The second terminal should open in the same /home/db2inst1/Documents/LabScripts/CDI directory as the first terminal.
You need to connect to the INGEST database in the second terminal as well.
db2 CONNECT TO INGEST
4. In the next few steps, you will need to use both terminal windows at the same time, one for running INGEST commands,
and the other one for monitoring the commands. So timing could play a factor in the monitoring result you get.
Switch back to the first terminal, where you ran your first INGEST command. Issue the following command to ingest
200,000 records from file ingest_data_200k into SALES. This time, instead of letting the ingest utility generate a job ID
for you, you will add a RESTART NEW option in the command to attach a customized ID so that it’s more convenient to
remember and use.
db2 "INGEST FROM FILE './ingest_data_200k' FORMAT DELIMITED RESTART NEW
'job1' INSERT INTO sales"
Note: The job ID you specify can be up to 128 bytes. Because the string uniquely identifies an INGEST command, it must
be unique across all concurrently running INGEST commands that specified the RESTART NEW option in the current database.
5. When the INGEST command starts executing, i.e. after the ingest utility starting timestamp is displayed, switch to the
second terminal, then issue command INGEST LIST.
db2 INGEST LIST
A summary like the following should be displayed with the basic information about the running INGEST command, such
as the number of records that have been processed thus far.
6. To get more detailed information about the running INGEST commands, INGEST GET STATS should be used. Switch
to the first terminal and ingest data again by issuing the following command:
db2 "INGEST FROM FILE './ingest_data_200k' FORMAT DELIMITED RESTART NEW
'job2' INSERT INTO sales"
DB2 10 for Linux, UNIX and Windows Course Workbook Page 282 of 335.
IBM Software
Information Management Technology Ecosystem
7. Like before, when the INGEST command starts executing, switch to the second terminal, then issue command below:
db2 "INGEST GET STATS FOR 'job2' EVERY 2 SECONDS"
You will see that the statistics about the INGEST operation gets updated every 2 seconds on the screen until the
operation finishes executing. Depending on the performance of the machine you run this lab on, the output should look
similar to the following:
The ingest utility supports copying rejected records to a file or table, or discarding them. A record can be rejected due to
errors such as out-of-range numeric values, incompatible data types, or invalid date format. You can specify what the
INGEST command does with rows rejected by the ingest utility (using the DUMPFILE option) or by DB2 (using the
EXCEPTION TABLE option). Note that rejected records do not cause the INGEST command to fail, the command would
continue with the remaining records from the source input. In this section, you will explore how the ingest utility handles
rejected records using the DUMPFILE option.
1. In your first terminal, truncate the table SALES by issuing the following command:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 283 of 335.
IBM Software
Information Management Technology Ecosystem
2. Ingest data from file ingest_data_excp into table SALES with command:
db2 "INGEST FROM FILE './ingest_data_excp' FORMAT DELIMITED DUMPFILE
'./rejected.txt' MESSAGES './messages.txt' RESTART NEW 'job3' INSERT INTO
sales"
Note: Parameter DUMPFILE specifies that rows rejected by the formatters are to be written to the specified file. If this
parameter is not specified, the ingest utility issues an error message for each rejected row, but does not save the row anywhere.
If you specify this parameter and any rejected input records contain sensitive data, these records appear in the dump file. You
must protect the file from unauthorized access as needed.
Note: Parameter MESSAGES specifies the file to receive informational, warning, and error messages.
After the INGEST command is finished, you should see messages like the ones below. Note that the warning messages
are saved in file messages.txt.
As the result shows, 2 of the records were rejected. The exact errors are saved in
/home/db2inst1/Documents/LabScripts/CDI/messages.txt.
3. Issue the following command to view the generated messages in messages.txt.
gedit messages.txt &
4. It is stated that record at line 10 and at line 990 from the source file were invalid.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 284 of 335.
IBM Software
Information Management Technology Ecosystem
5. The exact rejected records were saved in the dump file rejected.txt. So instead of looking through the source file for the
incorrect records, you can look into the dump file. Issue the following command to open file rejected.txt to view the
rejected records.
gedit rejected.txt &
DB2 10 for Linux, UNIX and Windows Course Workbook Page 285 of 335.
IBM Software
Information Management Technology Ecosystem
The dump file confirms the error messages in messages.txt. ‘2011-13-07’ is an invalid date and ‘one’ is not an integer.
The command is executed successfully and a message similar to the following is displayed:
DB2 10 for Linux, UNIX and Windows Course Workbook Page 286 of 335.
IBM Software
Information Management Technology Ecosystem
1. First, issue the truncate command below to clean up the table SALES.
db2 TRUNCATE TABLE SALES REUSE STORAGE IMMEDIATE
2. In the first terminal, issue the following command to start ingesting data.
db2 "INGEST FROM FILE './ingest_data_200k' FORMAT DELIMITED RESTART NEW
'job5' INSERT INTO sales"
3. When the INGEST command starts executing, i.e. after the ingest utility starting timestamp is displayed, in the same
terminal, press ctrl+c on your keyboard until the command is interrupted.
A message notifying you that the command was interrupted and has exited is displayed.
4. As you have created the restart table in section Utility Configuration in this lab, information about the failed INGEST
command is saved in that table and will be used by the ingest utility to restart the command. You can verify this by
doing a SELECT on table SYSTOOLS.INGESTRESTART. In our case, a record for job with ID ‘job5’ should be stored.
db2 'SELECT * FROM SYSTOOLS.INGESTRESTART'
DB2 10 for Linux, UNIX and Windows Course Workbook Page 287 of 335.
IBM Software
Information Management Technology Ecosystem
5. Still in the first terminal, where you just run the failed INGEST command, reissue the command with the RESTART
CONTINUE option.
db2 "INGEST FROM FILE './ingest_data_200k' FORMAT DELIMITED RESTART CONTINUE
'job5' INSERT INTO sales"
This time, the ingest utility should complete and return a successful message. Note that the total number of inserted
records from both INGEST commands equals the total number of records in the source file ingest_data_200k, which is
200,000 records.
Let’s go through a simple example to demonstrate this capability by increasing the products inventory in the INGEST database.
1. Take a look at the data currently in table INVENTORY by issuing the SQL query below in your first terminal:
db2 'SELECT * FROM INVENTORY'
4 record(s) selected.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 288 of 335.
IBM Software
Information Management Technology Ecosystem
What you want to update is the inventory of products with ID 100-101-01, 100-103-01 and 100-201-01. Their inventory
count should be changed by 10, 8, and -15 respectively
A script containing the command is already created to update the inventory count. Note that only the Quantity column of
the matching products should be updated.
3. You can verify the command by issuing the command below:
gedit ingest_ETL.sh &
DB2 10 for Linux, UNIX and Windows Course Workbook Page 289 of 335.
IBM Software
Information Management Technology Ecosystem
After the INGEST command finishes executing, a message like the following should be displayed.
Note that, as expected, record at line 4 in file ingest_inventory was rejected, which is the product with ID 999-987-10.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 290 of 335.
IBM Software
Information Management Technology Ecosystem
5. Run the following query against table INVENTORY again, you’ll notice that the quantity for products 100-101-01, 100-
103-01, and 100-201-01 have been updated.
db2 'SELECT * FROM INVENTORY'
4 record(s) selected.
A script has already been prepared for this exercise. It performs the following tasks in order:
• Check the input directory for new source files
• Prepare an INGEST command for each source file found
• Execute the INGEST command
• Process the return code, move processed source files to a success directory or a failed directory
When the script is running, it does not generate any output to the standard output. However, all actions will be recorded in log
files stored at /home/db2inst1/Documents/LabScripts/CDI/sample_project/logs/, and in messages.txt stored at
/home/db2inst1/Documents/LabScripts/CDI/sample_project/.
Six sample data input files have been generated to use with the script. They contain data for the SALES table. These files are
located at /home/db2inst1/Documents/LabScripts/CDI/sample_project/input/, which is the directory where the script
ingest_files.sh will fetch new source files from.
As the script executes, successfully processed source files will be moved to /sample_project/input/success, and failed source
files will be moved to /sample_project/input/failed/.
As long as there exists a source file in /sample_project/input/, the script will automatically process it. When there is no more
source file to process, the script will exit. In this particular exercise, we demonstrate with only 6 input files, but it could be a
continuous stream of incoming data. Thus, with the INGEST utility, real-time data warehousing can be achieved at a high rate
while queries are running against your data warehouse.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 291 of 335.
IBM Software
Information Management Technology Ecosystem
2. In your first terminal, where you ran the previous INGEST commands, change directory to
/home/db2inst1/Documents/LabScripts/CDI/sample_project.
cd /home/db2inst1/Documents/LabScripts/CDI/sample_project/
3. Execute ingest_files.sh. Two arguments are required to run the script, database username and password. In our case,
they are db2inst1 and password.
./ingest_files.sh db2inst1 password
4. When the script finishes executing, all successfully processed source files are moved to
/home/db2inst1/Documents/LabScripts/CDI/sample_project/input/success. Open the logs or messages.txt for detailed
information regarding the INGEST operations.
In a production environment, you can use a similar setup to continuously process incoming input data and ingest it into your
target database while keeping your database available to concurrent user workloads. The ingest utility allows movement of real-
time data with minimal impact on data availability.
12 Summary
The ingest utility ingests data directly or from files and pipes into DB2 tables. It can run continually and thus it can process a
continuous data stream through pipes. An INGEST command updates the target table with low latency in a single step. The
ingest utility uses row locking, so it has minimal interference with other user activities on the same table.
In this lab, you were presented with an overview of ingest-related tasks, ingesting data, monitoring ingest operations, handling
data errors, restarting a failed ingest operation, manipulating data during ingest, and a sample on how to automate continuous
data ingest from source files to a target table.
13 Cleanup
1. To clean your environment after completing the exercise, execute the commands below. Use “password” as password.
su –
cd /home/db2inst1/Documents/LabScripts/CDI/setup
./cleanup.sh
2. If you would like to redo this lab in the future, please execute the following commands in a terminal window as root:
cd /home/db2inst1/Documents/LabScripts/CDI/setup
./config.sh
DB2 10 for Linux, UNIX and Windows Course Workbook Page 292 of 335.
IBM Software
Information Management Technology Ecosystem
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
DB2 10 for Linux, UNIX and Windows Course Workbook Page 293 of 335.
IBM Software
Information Management
Workload Management
Hands-On Lab
DB2 10 for Linux, UNIX and Windows Course Workbook Page 294 of 335.
IBM Software
Information Management
Table of Contents
1 Introduction..........................................................................................................................................................................................4
2 Suggested Reading .............................................................................................................................................................................5
3 About this Lab......................................................................................................................................................................................5
4 Environment Setup Requirements.....................................................................................................................................................5
5 Getting Started with Basic Monitoring ..............................................................................................................................................6
5.1 Running Activities ............................................................................................................................................................................................... 6
5.2 View and Send In-Memory Statistics .................................................................................................................................................................. 7
5.3 View Service Class Statistics Collected by the Event Monitor.......................................................................................................................... 10
5.4 View Activity Information................................................................................................................................................................................... 10
5.5 Reset the Database for the Next Section.......................................................................................................................................................... 11
8 Using Thresholds to Control Rogue Activities and Using the Threshold Violation Monitor.....................................................16
8.1 Create Workload and Threshold Violation Event Monitor ................................................................................................................................. 16
8.2 Run Some Activities.......................................................................................................................................................................................... 17
8.3 View the Threshold Violation Event Monitor ..................................................................................................................................................... 17
8.4 View Information of the Activity that Violated the Threshold............................................................................................................................. 18
8.5 Reset for the Next Section ................................................................................................................................................................................ 18
10 WLM Dispatcher.................................................................................................................................................................................20
10.1 Creating the CPU shares and CPU limits ......................................................................................................................................................... 21
10.2 Execute the TE Workloads to Stress the CPU.................................................................................................................................................. 22
10.3 Set the Instance Configuration to Enable WLM Dispatcher and CPU Limit ..................................................................................................... 25
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 2 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 295 of 335.
IBM Software
Information Management
11 Summary ............................................................................................................................................................................................29
12 Cleanup...............................................................................................................................................................................................30
13 Appendix ............................................................................................................................................................................................30
13.1 Workloads ......................................................................................................................................................................................................... 30
13.2 Service Classes ................................................................................................................................................................................................ 31
13.2.1 Prioritization and resource control .................................................................................................................................................................... 31
13.2.2 Subclasses........................................................................................................................................................................................................ 32
13.3 Thresholds ........................................................................................................................................................................................................ 33
13.3.1 Threshold actions.............................................................................................................................................................................................. 34
13.4 Work classes & Work class sets ....................................................................................................................................................................... 35
13.5 Work actions & Work action sets ...................................................................................................................................................................... 36
13.6 Monitoring ......................................................................................................................................................................................................... 37
13.6.1 Real-time monitoring......................................................................................................................................................................................... 37
13.6.2 Historical monitoring ......................................................................................................................................................................................... 39
13.6.3 Historical Analysis ............................................................................................................................................................................................. 41
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 3 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 296 of 335.
IBM Software
Information Management
1 Introduction
In a typical database environment, there is a wide range of database activities that can flood the data server. There are short
transactional updates to a warehouse, (potentially) long reports, batch loading of data, applications calling stored procedures, to
name a few. These activities can come from numerous sources such as different users, business units, applications, multi-tier
servers, and even DB2 itself.
At times, all the database activity can cause the data server to perform unexpectedly. In order to keep control of the data server
workload, a comprehensive approach to workload management (WLM) is critical. An effective workload management
environment can be broken down into four basic stages: understanding business goals, identification of work that the goals are
established around, managing the workload to meet the goals, and monitoring the workload.
A good understanding of the business goals you are looking to meet for this system is important. For example, there are updates
to the database from cash registers around the country and it is critical to have a fast response time in order to prevent
customers from having to wait; you have a set of daily reports that have to be completed by 9 AM every morning for a review
meeting.
Once the goals are established, the next stage of WLM is the identification of activities that you will be trying to manage. The
wider the range of options for identification, the more likely you will be able to isolate the work you will be managing. Examples
of methods of identification include by source (e.g. an application or user ID that submitted a query) or by type (e.g. load
commands or queries that are read-only).
The third stage of WLM is the management of activities in order to meet the business goals. This would include any mechanism
that can affect how an activity is executed. For example, CPU or I/O resources could be made available to a set of queries,
activity concurrency can be limited, or resource intensive activities could be simply stopped.
The final stage of WLM is the ability to monitor the state of activities on the data server. If you have taken the time to establish
business goals for the environment, then it is important to have the mechanisms in place to determine whether those goals are
met (e.g. if you have response time goals, then you need to be able to determine what the average response time is). It is also
important to have monitoring functionality available to identify problem areas and understanding what activities are running on
the system.
Monitoring information is also very useful in determining if there are modifications required to the identification or management
stages. It is a good idea to use some monitoring information before the initial configuration to validate the approach used in
identification. For example, the activity information available from the monitoring should contain enough details about its source
or type to help with the identification process.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 4 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 297 of 335.
IBM Software
Information Management
In DB2, there are a number of new feature to help with managing the data server workload. The exercises in this lab are
designed to help you get more familiar with these features so that you can get an idea of the tools you have at your disposal to
build and effective workload management system.
The main design goals for DB2 WLM were to allow our customers to identify incoming work and isolate the work in an execution
environment so that resource consumption can be controlled in order to meet the business goals. Resources (such as CPU)
could be controlled explicitly or implicitly by controlling the work requests themselves.
There are a number of other WLM concepts discussed during the presentation which are covered in further detail in the
Appendix of this document.
2 Suggested Reading
Best Practices for DB2 for Linux®, UNIX®, and Windows®
Contains series of white papers, including one about WLM for Linux, Unix, and Windows
https://www.ibm.com/developerworks/db2/bestpractices/
Other sources of material that would be useful in understanding WLM can be found in the Suggested Reading section.
A more in-depth introduction about WLM concepts can be found in the Appendix of this document.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 5 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 298 of 335.
IBM Software
Information Management
In DB2, a default user workload (SYSDEFAULTUSERWORKLOAD) and a default user service class
(SYSDEFAULTUSERCLASS) are always created for each database.
These default objects can be used to take advantage of the workload management monitoring features without having to create
any user defined workloads or service classes. If no user defined workloads and service classes are created, all user activities
will be associated with these default objects.
Two event monitors, DB2ACTIVITIES and DB2STATISTICS were created already for use to collect activity information for
default workload and service class on activities and statistics respectively.
2. There are two scripts, located in the /home/db2inst1/Documents/LabScripts/WLM directory, which will be used to
generate workloads. These workloads are work1.db2 and work2.db2. These scripts must be executed in a terminal.
Open a terminal by right-clicking anywhere on the screen and select “Open Terminal”. Type the following commands
to execute the workloads. Each script will take a few moments to run so please be patient.
cd /home/db2inst1/Documents/LabScripts/WLM
db2 connect to WLM
db2 -o -tvf work1.db2
db2 -o -tvf work2.db2
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 6 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 299 of 335.
IBM Software
Information Management
Attention: Please ensure the user is db2inst1 when executing these commands unless otherwise noted.
1. Open Data Studio by double clicking the icon on the desktop screen. Use the default workspace when prompted.
3. Connect to the WLM database by right-clicking anywhere inside the SQL Editor and choose “Use Database
Connection”. Select WLM and click Finish. Enter the credentials – user: db2inst1 password: password. Click
“Save Password” for future reference. After the connection is successfully established, run the script by pressing F5
while the cursor is inside the SQL Editor or press the “Run SQL” button.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 7 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 300 of 335.
IBM Software
Information Management
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 8 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 301 of 335.
IBM Software
Information Management
CALL SYSPROC.WLM_COLLECT_STATS();
6. Re-run the first script, view_inmem_stats.sql against WLM, to view the statistics. The stored procedure run in step 5
will reset all in-memory statistics.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 9 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 302 of 335.
IBM Software
Information Management
Next we will view the service class aggregate activity information that was collected after the work scripts were performed.
1. Run the view_activity_info.sql under the /home/db2inst1/Documents/LabScripts/WLM directory against the WLM
database.
A script executed in the terminal is what is denoted as “Command Line Processor” (CLP) command. The
CLIENT_APP_NAME column has “CLP work1.db2” since the workload was run via the CLP.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 10 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 303 of 335.
IBM Software
Information Management
As we saw in the previous sections, all activities in DB2 are assigned to a workload and run in a service class. If no user defined
service classes are created, activities are run in the default subclass, SYSDEFAULTSUBCLASS, under the default user service
class, SYSDEFAULTUSERCLASS, and if no user defined workloads are created, activities are run under the default user
workload, SYSDEFAULTUSERWORKLOAD.
Two service super classes were created: WORK1_SC and WORK2_SC. In addition, two workloads, WORK1_WL and
WORK2_WL are assigned respectively to each one of the super classes associated to the client app names “CLP work1.db2”
and “CLP work2.db2”.
1. Enable the event monitor for activities and reset the in-memory statistics by utilizing the WLM_COLLECT_STATS
stored procedure. Run this script in a new SQL Editor against the WLM database in Data Studio.
SET EVENT MONITOR DB2ACTIVITIES STATE 1;
CALL SYSPROC.WLM_COLLECT_STATS();
1. Minimize Data Studio and return to the terminal. Execute the two workloads work1.db2 and work2.db2 in the
terminal. Please refer to section 5.1 for detailed instructions.
db2 connect to WLM
db2 -o -tvf work1.db2
db2 -o -tvf work2.db2
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 11 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 304 of 335.
IBM Software
Information Management
1. Return to Data Studio. Use the WLM_GET_WORKLOAD_STATS table function to view workload statistics to
determine which workload the applications are being associated with. Run the script in a fresh SQL Editor against
the WLM database.
SELECT SUBSTR (WORKLOAD_NAME,1,22) AS WL_DEF_NAME,
WLO_COMPLETED_TOTAL, CONCURRENT_WLO_ACT_TOP
FROM TABLE (WLM_GET_WORKLOAD_STATS (CAST (NULL AS VARCHAR (128)), -2))
AS WLSTATS;
2. Clear the SQL editor and run the following script to use the WLM_GET_SERVICE_SUBCLASS_STATS table
function which shows the service class the activities are being run under.
Notice the activities that completed under the WORK1_SC due to the WORK1_WL workload mapping and activities
completed under the WORK2_SC due to the WORK2_WL mapping.
SELECT VARCHAR (SERVICE_SUPERCLASS_NAME, 30) SUPERCLASS,
VARCHAR (SERVICE_SUBCLASS_NAME, 23) SUBCLASS,
COORD_ACT_COMPLETED_TOTAL COORDACTCOMP
FROM TABLE (WLM_GET_SERVICE_SUBCLASS_STATS('', '',-1)) AS T;
Next we will query the activity table for information on the activities that have been run. We will notice that only activities from
both work1.db2 and work2.db2 have been collected since data collection was specified during the definition of the workloads.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 12 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 305 of 335.
IBM Software
Information Management
1. Clear the editor and run the following statement against the WLM database to view the activity data collected.
SELECT SUBSTR (WORKLOADNAME, 1, 20) WL_DEF_NAME,
SUBSTR (APPL_NAME,1,20) APPL_NAME,
SUBSTR (ACTIVITY_TYPE, 1, 10) ACT_TYPE
FROM SYSIBM.SYSWORKLOADS, ACTIVITY_DB2ACTIVITIES
WHERE WORKLOADID = WORKLOAD_ID;
1. Run the reset_monitor.sql script in the /home/db2inst1/Documents/LabScripts/WLM directory against the WLM
database to reset the statistics for the next section.
After an activity is mapped to a new service class, it continues to run with the new resource constraints applied; therefore, we
now have the ability to remap an activity from one subclass to another within the same service class and allocate a higher/lower
CPU/IO priority as desired.
A service super class named WORK_RE_MAP was created with buffer pool priority set to low. This controls the buffer pool
priority of pages fetched by activities in this service class. Pages fetched with higher buffer pool priority are less likely to be
swapped out than pages fetched with lower priority.
In addition, a service class HIGH_PRI, created under the super class WORK_RE_MAP has an agent priority of -10. This means
that the relative processor priority of queries is much higher than others (in a relative sense, since negative values on
UNIX/LINUX specifies a higher priority). In addition we are also collecting aggregate request data for this subclass in order to be
able to view CPU execution times later.
Lastly, a workload WORKLOAD_REMAP maps all queries coming from the remap_work.db2 workload.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 13 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 306 of 335.
IBM Software
Information Management
Run some work on the database by executing the remap_work.db2 script; however, while the workload is running, try opening
and closing several applications (for example, double click the Firefox icon and close the browser then opening other program
and closing them). By doing so, one can gauge the reaction of the system, whether it be sluggish, or normal.
1. Execute the remap_work.db2 workload against the WLM database from the terminal. Please refer to section 5.1 for
detailed instructions.
db2 -o -tvf remap_work.db2
You should notice that everything seems to open and close alright. This is because everything is being executed in
the SYSDEFAULTSUBCLASS, which does not have the AGENT PRIORITY increased.
2. Return to Data Studio and use the WLM_GET_SERVICE_SUBCLASS_STATS function to ensure that everything
was properly executed.
Execute the view_remaps.sql script in the /home/db2inst1/Documents/LabScripts/WLM directory against the WLM
database.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 14 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 307 of 335.
IBM Software
Information Management
7.2 Create Threshold for Service Class to Remap Work that Takes Longer
We will now create the threshold that will remap any activities that take longer than 1 second of CPU time to remap to the
HIGH_PRI subclass within the WORK_RE_MAP service class.
1. Before continuing, reset the statistics to ensure a fresh start. Execute the reset_monitor.sql script in the
/home/db2inst1/Documents/LabScripts/WLM directory against the WLM database from Data Studio.
2. Minimize Data Studio and move to the terminal. Execute the remap_work.db2 workload against the WLM database
from a terminal. Please refer to section 5.1 for detailed instructions. During this time, open and close several
applications to view the response of the system.
db2 -o -tvf remap_work.db2
3. Use the same view_remaps.sql script open within the SQL editor. If closed, Re-open the script from the following
directory; /home/db2inst1/Documents/LabScripts/WLM and review the results to see the changes that have occurred.
Even though the response of the system was slightly more sluggish, the overall time the query took was reduced. In
addition, there should be queries that were remapped into the HIGH_PRI subclass.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 15 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 308 of 335.
IBM Software
Information Management
We will drop the threshold since we are no longer using it during the lab and clean up the event monitor table, and call
WLM_COLLECT_STATS() to reset the statistics.
1. Execute the script reset_remap.sql in the /home/db2inst1/Documents/LabScripts/WLM directory against the WLM
database to accomplish the reset.
1. STOP EXECTUION – Stop processing the activity that caused the threshold to be violated
2. CONTINUE – Continue processing
3. Collect information about the activity that violated the threshold – this action can be combined in conjunction with the
CONTINUE or STOP EXECTUTION actions.
Regardless of whether an activity that violates a threshold is stopped or allowed to continue running, a record of the violation is
written to an active THRESHOLD VIOLATIONS event monitor (assuming one is defined in advance). As we will see, the record
contains information such as which threshold was violated, the time of the violation, and the threshold action.
In the following section, it will be demonstrated how thresholds can be used to detect or prevent rogue (“runaway”)
queries/activities which use unexpectedly high amount of resources.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 16 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 309 of 335.
IBM Software
Information Management
1. Return to the terminal. Execute the workth.db2 workload against the WLM database in a terminal. Please refer to
section 5.1 for detailed instructions.
db2 -o -tvf workth.db2
Figure 19: Statements that violate the thresholds defined will fail with an error of SQL4712N/SQL STATE 5U026.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 17 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 310 of 335.
IBM Software
Information Management
1. Execute the view_viol_act.sql script found in the /home/db2inst1/Documents/LabScripts/WLM directory against the
WLM database to view the results of the script.
Figure 21: The activities that violated the th_estcost threshold are not shown because the COLLECT ACTIVITY
DATA clause was not specified so no activity data was collected.
1. Execute the cleanup_thres.sql script found in the /home/db2inst1/Documents/LabScripts/WLM directory against the
WLM database to prepare for the next section.
The following section demonstrates how a work action set can be used to:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 18 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 311 of 335.
IBM Software
Information Management
Attention: There are other actions that can be applied, such as collecting statistics for activities of a certain type that are not
covered in this task which can be found in the Appendix.
1. Enable some of the event monitors for activities that were created previously. Execute the command in a fresh SQL
Editor against the WLM database. For detailed instructions, refer to Section 5.2.
SET EVENT MONITOR DB2ACTIVITIES STATE 1;
9.2 Create a Work Class Set and a Database Work Action Set
Create a work class set containing work classes that will represent the specific types of activities we are interested in. This work
class set will be used in conjunction with work action sets to perform actions on the selected types of activities. Below is an
example that creates a work class set containing work classes of all possible types in DB2; however, specific activity types of
work class sets could be created to only contain that one work class.
In order to perform a particular action on activities of a specific type (such as apply a threshold or collect activity information) a
database work action set could be used. Create a work action set at the database level that contains work actions for the
specific work class representing the type of activities that should be isolated. For this example, assume they want to collect
activity data for all DDL, READ and LOAD activities that run on the system. In addition, stop any large read activity from running.
For this task, a large read activity will be defined as any select statement that has an estimated cost (in timerons) of greater than
10000.
1. Generate some activities by running the work1.db2 and work3.db2 scripts in the
/home/db2inst1/Documents/LabScripts/WLM directory. Open a terminal and execute the scripts. Refer to section 5.1
for more detailed instructions.
db2 connect to WLM
db2 -o -tvf work1.db2
db2 -o -tvf work3.db2
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 19 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 312 of 335.
IBM Software
Information Management
2. Use the WLM_GET_WORK_ACTION_SET_STATS table function to access the work action set stats in memory to
get the number of times specific activity types have been run. Open and execute the view_wa_stats.sql script in the
/home/db2inst1/Documents/LabScripts/WLM directory against the WLM database in Data Studio. Notice that running
the query shows only the load_wc, read_wc, and ddl_wc since they are the only ones that have an applicable work
action. The remaining activities are counted under the wildcard ‘*’.
Figure 23: Some statements prevented due to their estimated cost being greater than 10000 timerons.
9.5 Reset for Next Steps and Disable Work Actions and Workloads
1. Execute the script reset_before_histo.sql script in the /home/db2inst1/Documents/LabScripts/WLM directory
against the WLM database to disable the work actions and workloads in Data Studio.
2. Close Data Studio.
10 WLM Dispatcher
The WLM Dispatcher is an instance level CPU scheduling technology inside of DB2. This allows for the control of CPU
resources allocated to various service classes. The resources are controlled by hard CPU shares, soft CPU shares and/or CPU
limits.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 20 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 313 of 335.
IBM Software
Information Management
In this exercise, 3 service classes – SC_WD1 with 6000 hard CPU shares, SC_WD2 with 6000 soft CPU shares, and SC_WD3
with 6000 hard CPU shares are used; furthermore, the aforementioned classes reside under a super class SC_SUPERCLASS
with a CPU limit of 15%. All of these will be created within the WLMD database. Attach the W_WD1, W_WD2, and W_WD3
workloads to the service classes SC_WD1, SC_WD2, and SC_WD3 respectively. This is summarized within the diagram below.
Attention: Please note the databases addressed are now WLMD and WLMD2, and not WLM.
Note the split between the two databases – this is using the assumption that there are only two competing service classes – and
that they are both created using the default values of 1000 shares. If there were no competing shares, SC_WD1, SC_WD2 and
SC_WD3 would be free to take 100% and spilt it amongst themselves for entitlement.
3. Navigate back to the terminal. Connect to the WLMD database and create all of the objects mentioned above by
executing the script create_WMD.sql found in the /home/db2inst1/Documents/LabScripts/WLM directory.
db2 connect to WLMD
db2 -tvf create_WMD.sql
Attention: As this exercise is extremely sensitive to hardware and other processes running, do not open any other program,
unless instructed, to conserve resources.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 21 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 314 of 335.
IBM Software
Information Management
1. Start the Technology Explorer (TE) by double-clicking the TE shortcut icon on the desktop.
2. Once inside, connect to the WLMD databases by double-clicking the row with the corresponding databases. Enter
“password” as the password when prompted; leave the rest of the fields as default.
3. Connect to the WLMD2 in the same fashion; the password is again “password”.
4. Once connected, both of the databases should have a light blue checkmark beside them to signify they have been
authenticated.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 22 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 315 of 335.
IBM Software
Information Management
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 23 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 316 of 335.
IBM Software
Information Management
7. Minimize the browser by going up and clicking the small horizontal bar.
Note: Wait 10 seconds for this query to respond – it takes values over 10 seconds to report. Maximize the terminal window
for better readability.
Hint: Press the up arrow on the cursor pad to show the last executed command.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 24 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 317 of 335.
IBM Software
Information Management
10.3 Set the Instance Configuration to Enable WLM Dispatcher and CPU Limit
1. Return to the terminal and enable WLM dispatcher and set the minimum utilization to 2%. Once updated, restart
DB2 to make the changes take effect and reconnect to the database WLMD.
db2 update dbm cfg using wlm_dispatcher yes
db2 update dbm cfg using wlm_disp_cpu_shares yes
db2 update dbm cfg using wlm_disp_min_util 2
db2 force applications all
db2stop
db2start
db2 connect to WLMD
db2 get dbm cfg | grep 'WLM dispatcher'
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 25 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 318 of 335.
IBM Software
Information Management
Figure 38: The limit is being enforced, and so are the shares.
1. Navigate back to Technology Explorer. Tweak the workload by clicking into the WL2 workload and click the icon
beside CLIENT THINK TIME. Type “60ms” into the box to slow the workload down a bit. This should still keep it
above the 2% utilization but due to many factors it may or may not. If required to tweak the workload to reach
desired results, the smaller the number – the higher the workload (smaller breaks between transactions). The graph
should start to dip to signify the reduction in the workload.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 26 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 319 of 335.
IBM Software
Information Management
Note: Due to the dependency on hardware configuration, it may require a bit of tweaking in order to reach desired values.
2. Return to the terminal and remove the CPU limit by setting the limit to 100% and decreasing the number of hard
shares allocated to WL1 to 500.
db2 "alter service class SC_SUPERCLASS CPU LIMIT 100"
db2 "alter service class SC_WD1 under SC_SUPERCLASS HARD CPU SHARES 500"
3. Execute the cpu_util_all_sc.sql script again. Notice the WL2 is not using the full 7.5% allocated to the service class;
however, due to the fact that WL1 was defined using hard shares, it cannot utilize the free shares that are already
available. This is shown via the fact it cannot allocate more than its ESTIMATED_CPU_ENTITLEMENT. The CPU
entitlement is calculated based on the number of shares the service class has, divided by the number of shares that
are in contention.
Note: Workloads under the minimum utilization are not considered contending workloads and will not contribute to the calculation
Figure 40: Cannot exceed defined limit even with resources available.
1. Increase the workload from WL2 by setting the CLIENT THINK TIME value to 8ms and decrease the workload of
WL1 by setting the CLIENT THINK TIME value to 60ms.
Attention: Ensure that the setting CLIENT THINK TIME value is kept at 60ms for workload WL1
2. Return to the terminal and set the soft shares of SC_WD2 to 500, and the hard shares of SC_WD1 to 6000.
db2 "alter service class SC_WD2 under SC_SUPERCLASS SOFT CPU SHARES 500"
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 27 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 320 of 335.
IBM Software
Information Management
db2 "alter service class SC_WD1 under SC_SUPERCLASS HARD CPU SHARES 6000"
3. Execute the cpu_util_all_sc.sql script again. As SC_WD2 is using soft cpu shares, it is allowed to utilize the free
resources that SC_WD1 freed up and its utilization exceeds past its ESTIMATED_CPU_ENTITLEMENT.
db2 -tf cpu_util_all_sc.sql
As noted, WLM Dispatcher is an instance level control; therefore, it is possible to control service classes defined in other
databases relative to this one as well.
1. Execute the script create_WMD2.sql found in the /home/db2inst1/Documents/LabScripts/WLM directory against the
WLMD2 database. This script will create a service class “SC_SUPERCLASS_LIM” associated to the WL4 workload
with 800 hard CPU shares.
db2 connect to WLMD2
db2 -tvf create_WMD2.sql
Figure 42: CPU entitlement assuming all shares are fully utilized.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 28 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 321 of 335.
IBM Software
Information Management
Note: Due to high CPU usage, the system may now appear sluggish.
4. You should have only WL1 and WL4 running at this point.
5. Execute the cpu_util_all_sc.sql script again and take a look at the values. The service class
SC_SUPERCLASS_ LIM is not able to utilize more than 44% of the CPU resources, which is the maximum CPU
entitlement for this service class.
db2 -tf cpu_util_all_sc.sql
Figure 44: May now fully utilize available resources due to no more competing service classes.
11 Summary
There were many different concepts introduced and demonstrated in this lab. The use of various event monitors, views, and
table functions allow for the user to be able to see what is going on within the database. Armed with that information, one could
set up various thresholds, work actions and other objects to control and contain different actions within the database to ensure
that the workload intentions are defined by the user, rather than dictated by the workload.
The CPU scheduling technology, introduced in DB2 10 allows for the control of the CPU usage using hard CPU shares, soft
CPU shares and CPU limits. The WLM dispatcher allows the user to reside within the comfort of DB2 while having the capability
to direct workloads and resource consumption.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 29 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 322 of 335.
IBM Software
Information Management
This lab only scratched the surface of workload management, providing an introduction to several features within DB2’s
Workload Management and the newly added WLM Dispatcher. For further insight into this valuable topic refer to the appendix
and suggested readings..
12 Cleanup
1. To cleanup this lab, execute the following script. The password is “password”.
su –
cd /home/db2inst1/Documents/LabScripts/WLM/setup
./cleanup.sh
2. If you would like to redo this lab, please execute the following script in a terminal as root.
cd /home/db2inst1/Documents/LabScripts/WLM/setup
./config.sh
13 Appendix
The following will provide you with an overview of the various concepts within WLM used throughout the lab above and
discussed in the accompanying presentation.
13.1 Workloads
A workload can be defined to identify incoming work based on its source so that it can later be properly managed. Connections
are assigned to a workload based on the following connection attributes:
Attribute Description
application name The name of the application that is connected to the database.
session The authorization ID that is set in the current session of the database
authorization ID connection. Useful when a connection is maintained for a long period of
time but user ID attributes need to change.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 30 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 323 of 335.
IBM Software
Information Management
role of session The DB2 role to which the session authorization ID belongs to.
authorization ID
client user ID The client user ID. Useful in an environment where an application server
or tool uses connection pooling to connect to a database with a single
user ID representing a large number of clients.
client application The client application name. Useful when the application that established
name the database connection did so on behalf of the client application.
client workstation The client workstation name. Useful when you need to isolate work from
name a specific workstation.
client accounting A user-defined accounting string. Useful when you need to isolate work
string based on your own custom criteria.
The workload is evaluated and assigned at the unit of work (UOW) boundary, at which time a new workload occurrence is
started. A workload occurrence consists of one or more units of work within a database connection that are associated to a
workload. It is quite likely that multiple connections have similar attributes that result in multiple workload occurrences based on
the same workload definition. Workload re-evaluation occurs at the beginning of each UOW in the event that the value of a
connection attribute or the workload definition itself changes during the UOW. This re-evaluation might result in the connection
being associated with a new workload, creating a different workload occurrence. It is possible that multiple workload definitions
include a given activity. In this case, the first workload defined that includes the activity is the one that is used. You can
therefore manipulate the order of workload definitions to ensure that the proper workload is chosen.
In DB2, if no custom-defined workload is found during workload evaluation, all database connections are assigned to the default
workload.
When you define a workload, you must indicate the service class where work associated with that workload runs. There is
always a default service class, which ensures that all data server work executes inside a given service class.
When you create a service class, you can define a number of resource controls:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 31 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 324 of 335.
IBM Software
Information Management
Control Description
Agent This control sets a CPU priority level for the agent threads running within a
Priority service class. This priority flows through to the operating system as a relative
(delta) priority to other threads and processes running in the class.
Prefetch For heavy I/O work involving prefetching, this control is useful for mapping work
Priority to one of three prioritized prefetch queues (high, medium, and low).
Buffer pool This control allows sets the buffer pool priority of pages fetched by activities in
Priority this service class. Pages fetched with higher buffer pool priority are less likely to
be swapped out than pages fetched with lower priority.
External This control allows a workload to have some of its resources controlled by an
WLM tag external workload manager such as the AIX Workload Manager (currently, the
only supported external workload manager). The tag flows through the agent to
the external workload manager and maps to a resource group defined with the
manager.
13.2.2 Subclasses
You can divide a service class into multiple service subclasses. Define these in cases where the granularity found in a service
class does not adequately map to the resource goals that you need for that service class. For example, if the Operations and
Finance departments share a data server, you could define two service classes to account for resource allotment for each
department. Also, if the Operations department regularly runs custom reports and batch ETL processing, and the batch jobs
need to be constrained so they do not interfere with the reports, you could define service subclasses to which you assign the
batch and ETL workloads. Each subclass could define appropriate resource controls so that the custom Operations reports have
priority.
The service super class remains the highest tier for work, but service subclasses are the only place where activities run. This
distinction is important to note, especially when you look at some of the information available through the workload management
monitoring features discussed later on. Each service super class has a default service subclass defined to run activities that you
do not assign to an explicitly defined subclass. Additionally, you can define only a single level of subclasses (that is, you cannot
define a subclass under another subclass, only under a service super class).
The following figure is an example of a custom workload management configuration using workloads and service classes:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 32 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 325 of 335.
IBM Software
Information Management
As user requests to enter the data server, they are identified as belonging to a given workload and assigned to a service super
class or subclass.
13.3 Thresholds
Resource controls are one way to try to meet your defined goals. At times, work comes in that might exceed your normal
expectations (for example, a query that is returning hundreds of thousands of rows), consuming valuable resources at the
expense of all of the other work running on the system. Threshold objects could be created to look for these types of exceptions
and trigger actions if thresholds are exceeded.
The following table provides the list of thresholds that can be defined:
Elapsed Time Jobs that are taking abnormally long to Controls the amount of time that any given activity can spend from
complete. submission to completion in the DB2 data server (execution time
and idle time).
CPU Time in Jobs that are taking abnormally long to Controls the maximum processor time across service subclass
Seconds complete.
Idle Time Inefficient use of data server resources and Controls the amount of time that a connection idles, not working on
unexpected wait conditions. behalf of user requests. Use to detect
Estimated Cost Potentially resource-heavy SQL (identifying Controls DML activities that the DB2 optimizer determines to have a
poorly written SQL). large estimated cost.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 33 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 326 of 335.
IBM Software
Information Management
Rows Returned When the amount of data returned exceeds a Controls the number of rows returned when executing SQL.
reasonable volume.
SQL Rows Read In Amount of data read exceeds a reasonable Controls the number of rows read when executing various SQL
Seconds volume. statements.
Temporary Space Disproportionate amount of temporary space Controls the amount of temporary table space a given activity can
usage. consume on a partition.
Aggregate Temporary Disproportionate amount of aggregate Controls aggregate usage of temporary table space for activities
Space temporary space usage. across service classes
Concurrent Workload Large amount of work from a specific source Controls the number of active occurrences of a workload that can
Occurrences overwhelming a data server. run on a coordinator partition at the same time.
Total Database Partition becoming overloaded with Controls the number of database connections to a given partition
Partition Connections connections. that can be established at the same time.
Total Service Class Partition connections linked to a service class. Controls the number of database connections to a given partition for
Partition Connections work executing within a given service class at the same time.
Concurrent Workload Workload from dominating overall data server Controls the number of individual activities that can run within a
Activities activity. workload.
Concurrent Database Limit work within an individual workload Controls the number of individual activities that can run within a
Activities occurrence. workload occurrence.
CPU Cost in Seconds Limit work Controls the maximum amount of combined user and system
processor time that an activity can use on a particular database
partition while the activity is running.
These thresholds can be divided into different categories. The first set of thresholds deal with activity limits, where the controls
pertain to the impact that an activity can have on how the data server is running. Excess time, abnormally high volumes of data
returned, and abnormally high amounts of resources consumed are warning flags that potentially troublesome activities could be
using up resources that they are not supposed to. The next set of thresholds deals with concurrency control and is meant to look
for cases where you need to consider limiting the number of certain activities running at the same time to reduce their impact on
the data server.
The kind of actions that can be taken when a threshold is exceeded depends on the threshold itself.
Collect data
When a threshold is exceeded, there is always some form of data collected. By default, the fact that a threshold was exceeded
is recorded in the event monitor. You might want more detail, though, so on each threshold definition, you can request that more
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 34 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 327 of 335.
IBM Software
Information Management
data be captured, such as information about individual activities, the statement text, the compilation environment, and even the
input data values.
Stop execution
A common action when a threshold breach occurs is to stop the activity from executing. In this case, an error code is returned to
the submitting application indicating that the threshold was exceeded.
Continue execution
In some cases, stopping the execution of the activity is too harsh a measure. It might be better for business to allow the activity
to continue to run and collect the relevant data for a DBA to perform future analysis to determine how to prevent this condition
from happening again. In this case, no error code is returned to the submitting application.
Queue activities
In some cases, there is the option to queue activities if there is too much work running. Rather than stopping activities (or
connections), you can queue them. Queuing can be quite an effective method of resource control and throughput management
with very little impact to the submitting applications (because they wait for the activity to complete and are unaware the activity is
queued). You can specify the maximum size of the queue, and if the queue size is exceeded, the stop execution action takes
effect.
Queuing makes sense only for thresholds that are considered predictive, where an action decision can be made before activity
execution starts. This includes some of the concurrency thresholds: in particular, the total service class partition connections and
the concurrent database activities. If an activity is queued, no error code is returned to the submitting application. The application
waits as if the activity were running until the activity is taken off the queue for execution and eventually completes.
For example, consider the case where you set a threshold to allow a maximum of 500 concurrent database activities plus 100
that can be queued and you currently have exactly 500 activities running. A query is then submitted that represents activity
number 501. That query remains in a queued state until one of the currently running activities completes. The application that
submitted the query is oblivious to the fact the query was put into a queued state. Queries continue to queue until there is a total
of 600 concurrent queries. Any additional queries are stopped, and an error code is returned to the submitting application.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 35 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 328 of 335.
IBM Software
Information Management
- CREATE - GRANT
- ALTER -
REVOKE
- DROP
- COMMENT
- SELECT - INSERT - DECLARE GLOBAL
- XQuery - UPDATE TEMPORARY TABLE
- DELETE - REFRESH TABLE
- MERGE - RENAME
- SET INTEGRITY
Work classes also introduce predictive identification for DML work (or READ and WRITE statements). Predictive identification is
very useful because it provides information about database activities that can be used to take action before they start consuming
resources on the data server.
You can also identify activities by using the schema name of the procedure invoked by a CALL statement. Based on workload
attributes and work class types, you can identify work and prepare it for the next stage, the management of the work.
If you define a work action set for a database, there are several types of actions that you can apply to activities that fall
within a work class:
• Prevent execution
• Count activity
For example, perhaps you want to prevent SQL from returning more than 100 000 rows. You can define a single work class
for a work action set that identifies SQL READ statements and then apply a work action with a threshold that represents a
threshold that would stop execution if the number of rows returned is more than 100 000.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 36 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 329 of 335.
IBM Software
Information Management
Typically, the work action maps an activity to a specific service subclass and that subclass has thresholds defined on the
subclass to help manage the activity.
13.6 Monitoring
Monitoring validates assumptions that you made about data server capacity, it determines what is going on in the system, it
allows you to identify problem areas, and it enables you to drill down into specific areas to find useful data for solving the
problems.
Workload management monitoring allows you to access real-time operational data (such as a list of running workload
occurrences, a list of the activities running within a service class or the average response times for activities executing in a
service class) by using table functions to access in-memory data. New event monitors capture detailed activity information
(per service class, work class, or workload or when a threshold is exceeded) and aggregate activity statistics for historical
analysis.
With the workload definitions and the service classes and thresholds, there is a unique opportunity to collect various sets of
information internal to the DB2 workload manager. This data includes statistics that represent the current activity on the system
that can help determine usage patterns and resource allocation and identify problem areas.
The method for accessing the real-time monitor data is through DB2 table functions. Table functions provide access to a set of
data that exists inside a DB2 database (such as the workload management statistics) as a virtual DB2 table against which you
can execute a SELECT statement. This offers you the ability to write applications to query data and analyze it just as if it were
any physical table on the data server.
General statistical information is available at a number of different levels, including the ones described in the following table:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 37 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 330 of 335.
IBM Software
Information Management
Statistics are only useful if the time period during which they are collected is meaningful. Collecting statistics over a very long
time might be less useful if it becomes difficult to identify changes to trends or problem areas because there is too much old
data. Thus, there is the ability to reset statistics at any time.
In addition to table functions that return statistics, there are table functions that return data sets of information about the work
that is currently executing on a system. This information about work is available at various levels:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 38 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 331 of 335.
IBM Software
Information Management
One important thing to remember is that because there are default workloads and service classes on the data server, monitoring
capabilities exist from the moment that you install the DB2 data server. These can be a great help in kick starting or validating
the identification stage of workload management by helping to isolate sources of activities that you can use to create workloads
and the service classes to which you can assign them.
In addition to the table functions, DB2 Workload Manager uses event monitors to capture information that might be of use in the
future. Three new event monitors have been added for DB2 WLM, each for a different purpose:
Activity event monitor: This monitor captures information about individual activities in a service class, workload, or work
class or activities that violated a threshold. The amount of data that is captured for each activity is configurable and should be
considered when you determine the amount of disk space and the length of time required to keep the monitor data. A common
use for activity data is to use it as input to tools such as db2advis or to obtain access plans (from the explain utility) to help
determine table, column, and index usage for a set of queries.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 39 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 332 of 335.
IBM Software
Information Management
Threshold violations event monitor: This monitor captures information when a threshold is exceeded. It indicates what
threshold was exceeded, the activity that was the source of the exception, and what action was taken when it occurred (i.e. was
the activity stopped or allowed to continue).
Statistics event monitor: This monitor serves as a low-overhead alternative to capturing detailed activity information by
collecting aggregate data (for example, the number of activities completed and average execution time). However, if you use
aggregates, you can lose information about the distribution of activities, which you can mitigate by using histograms. More than
simply a way to determine statistics such as the average response time, histograms help you to understand what kind of
variance in the response time your users are experiencing (an average response time of 2 seconds might seem great, but if the
users don’t know whether response time is sub second or a couple of minutes, that leads to frustration). DB2 workload
management offers a number of histograms to represent the distribution of activities over their entire life cycle or the time that
they were executing.
The figure below shows the different monitoring options available to access workload information, table functions to access real-
time statistics and activity details and historical information captured as efficient aggregates or detailed information for individual
activities:
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 40 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 333 of 335.
IBM Software
Information Management
To help with analyzing resource consumption and the effectiveness of your database design, a historical analysis tool is provided
to look at the activity information collected in the activity event monitor and generate details on different users accessing tables
and columns as well as which tables, columns, and indexes are being used.
The WLM Historical Analysis Tool is composed of 2 Perl scripts: wlmhist.pl (to generate the historical data) and wlmhistrep.pl
(to produce the usage reports). These scripts can be found in the sqllib/samples/perl directory.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 41 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 334 of 335.
IBM Software
Information Management
IBM Canada
8200 Warden Avenue
Markham, ON
L6G 1C7
Canada
IBM, the IBM logo, ibm.com and Tivoli are trademarks or registered
trademarks of International Business Machines Corporation in the
United States, other countries, or both. If these and other
IBM trademarked terms are marked on their first occurrence in this
information with a trademark symbol (® or ™), these symbols indicate
U.S. registered or common law trademarks owned by IBM at the time
this information was published. Such trademarks may also be
registered or common law trademarks in other countries. A current list
of IBM trademarks is available on the Web at “Copyright and
trademark information” at ibm.com/legal/copytrade.shtml
Product data has been reviewed for accuracy as of the date of initial
publication. Product data is subject to change without notice. Any
statements regarding IBM’s future direction and intent are subject to
change or withdrawal without notice, and represent goals and
objectives only.
Workload Management
© Copyright IBM Corp. 2012. All rights reserved Page 42 of 42
DB2 10 for Linux, UNIX and Windows Course Workbook Page 335 of 335.