DB2 Data compression
Disk storage is not cheaper, take place, make noise, so, for large warehouses (or huge volumes database), the cost of the storage become important. IBM DB2 have a solution, with the data compression named “Venom” technology, reducing storage requirements, improving I/O, and providing quicker data access from the disk.
DB2 uses a dictionary based algorithm for compressing data records. DB2 9 will scan tables, and search for duplicate occurrences, then assign a short numeric key to each entries. You will understand better with a picture:

Compress data
Each new occurences of “Sophie”, “New York”, “IBM US”, will be respectively assign to “(01)”, “(19)” and “(09)”. This is very useful on large table. Each time a new row is added to the table, DB2 will automatically compress these new data.
Seems to be good, let’s try on our DB2 now. To enable the compression, we should turn on the “COMPRESS” option on the table.
CREATE TABLE Users COMPRESS YES
or use alter if the table is already created:
ALTER TABLE Users COMPRESS YES
Typically, we can save an average ratio of 45 to 75 percent of our storage space. Next time, we’ll see how to use the inspect tool, to determine the compression ratio.
Customize your DB2 CLP prompt
You have a nice DB2 CLP prompt, looks like “db2>”, but you want more, you want a efficient prompt. It is possible with DB2 to add or modify your CLP ! DB2 have a registry variable named DB2_CLPPROMPT which allows us to define the prompt to be used in the CLP interactive mode.
To define this variable, let’s use the DB2set command:
Db2set DB2_CLPPROMPT="db2isgreat> "
Our new DB2 Prompt will be “db2isgreat> “. It’s much more better, but we can do more. DB2_CLPPROMPT registry variable can contain the tokens %n, %ia, %d, %da and %i:
- %n – New line
- %ia – Authorization ID of the current instance attachment
- %d – Local alias of the currently connected database
- %da – Authorization ID of the current database connection
- %i – Local alias of the currently attached instance
Now, let’s try with these tokens:
Db2set DB2_CLPPROMPT="%ia@%i, %da@%d> "
With an instance attachment to instance “DB2″ with authorization ID “mycado”. Database “sample” with authorization ID “mycadoax”, will return something like:
MYCADO@DB2, MYCADOAX@SAMPLE >
You can now everytime where you are, and one which instance/database you’re working on.
Queries on XML data with XQuery
We know how to store native XML data in our DB2 tables, and now we will see how we can access to these data. We can choose between standard SQL queries and XQuery.. or both ! The first solution, with a SQL query, only query at the column level of your table; this query will return the full XML data. The second solution, with XQuery, allow us to make a “query” inside our XML data.
For the SQL query, nothing more than a SELECT:
SELECT id, info from client
Easy, but not really powerful for XML data, let’s try with XQuery, who give us two functions for DB2. db2-fn:sqlquery and db2-fn:xmlcolumn. The first function retrieves a sequence that is the result of an SQL fullselect and the second retrieves a sequence from a column. One important thing you should keep in mind, SQL is not a case-sensitive language alors que XQuery is a case-sensitive language.
An example which return all the XML data from the “info” column:
XQUERY db2-fnxmlcolumn ('CLIENT.INFO')
Which is same as this SQL query:
SELECT info FROM client
Let’s see something more nice. This query will return all the elements in <name> , inside the “info” column, and with the <city> element which containt “Paris”:
XQUERY declare defaut element namespace "http://posample.org";
for $d in db2-fn:xmlcolumn('CLIENT.INFO')/clientinfo
where $d/addr/city="Paris"
return <out>{$d/name}</out>
db2-fn:xmlcolumn retrieves the data from the “info” column in the “client” table. We add a $d variable, for each element of <clientinfo>, and we use a where to filter the <city> element which should be “Paris. To finish, we use <out> to output the data:
<out xmlns="http://posample.org"> <name>Sophie Bool</name> </out>
To finish, the same example with a SQL query inside the XQuery:
XQUERY declare default element namespace "http://posample.org";
for $d in db2-fn:sqlquery('SELECT info FROM client')/clientinfo
where $d/addr/city="Paris
return <out>{$d/name}</out>
Enough for today, we will see next time how to do more complex and more powerful query with XQuery !
Store native XML in a DB2 table
IBM DB2 is the first RDBMS to provide a native XML facility, in a table. You will be able to insert in a column, some XML data. It will be easier to make direct requests to these data with XQuery.
So, it’s funny, but why it’s interesting to do that ? Because XML data are hierarchic (instead of relational data which are flat) and data self-describing through XML tags. XML also allows a better flexibility for data structures required to change very often. In the other hand, access time performances will be a little slower, we lose the integrity constraints, and OLAP queries will be more difficult. The important question is “Which flexibility-performance ratio do you need ?”.
Let’s start now with the creation of a database, which should be encoded with UTF-8, to store XML. For that, we use the CREATE DATABASE command:
CREATE DATABASE xmldb USING CODESET UTF-8 TERRITORY US
We have our table, now we should create the “client” table, with an “info” column who contain client information, in XML format:
CONNECT TO xmldb CREATE TABLE client (id INT, info XML)
Let’s try to insert a new client with a SQL query:
INSERT INTO client (id, info) VALUES (1, '<clientinfo xmlns="http://posample.org" Cid="1"><name>Sophie Bool</name><addr country="France"><street>5 rue du chateau de stable</street><city>Paris</city></addr><phone type="work">01 72 92 02 88</phone></clientinfo>')
The first thing you will told me is this query is a normal SQL query, and this is right, insert XML isn’t more difficult. The second thing is about the XML, here we have a short XML data, but if we have more, it will be very difficult to use. This why, we will use XQuery to manipulate these data or use a XDS import. I’ll come back soon on these points.
DB2 queries with PHP
You want to use PHP as programmation language to query your DB2 databases. We will see how to do. We only need a HTTP server with the PECL ibm_db2 extension. This extension allow us to use new functions, relative to IBM DB2, but also work with IBM Cloudscape and Apache Derby.
After the installation, we will use these functions as normal PHP functions. This is a connection example to the SAMPLE database. You can click on the function to get more information:
<?php $conn = db2_connect('SAMPLE', 'db2user', 'secretpass'); if($conn) { echo "connection to sample: ok."; } else { echo "connection to sample: failed."; } db2_close($conn); ?>
Nothing very complicated here, we connect to the SAMPLE database with the db2_connect function, and we check if the connection works, then we close the connection with db2_close.
Now, let’s do something more interesting, do some query on our tables !
<?php $query = "SELECT * FROM ADMINISTRATOR.EMPLOYEE"; $stmt = db2_prepare($conn, $query); if($stmt) { $ex = db2_execute($stmt); if($ex) { while($ligne = db2_fetch_array($stmt)) { $lastname = ligne[3]; echo "<br />- $lastname"; } } } ?>
We put your query in the $query variable, and we use the db2_prepare function with the previous connection ($conn). This function will “prepare” (I’m so smart), it will create an optimized path in DB2, to be more fast. We execute this result ($stmt) with the db2_execute function, who do the query on your database. To finish, we use db2_fetch_array to retrieve our data in an array.
A BLOB example.. or Binary Large OBject, an image, a audio or video file,..
$filename = '/home/mycado/itsme.jpg'; $name = 'My cute picture"; $query = 'INSERT INTO photo (id, name, image) VALUES (?, ?, ?)'; $stmt = db2_prepare($conn, $query); if($stmt) { db2_bind_param($stmt, 1, 'id', DB2_PARAM_IN); db2_bind_param($stmt, 2, 'name', DB2_PARAM_IN); db2_bind_param($stmt, 3, 'filename', DB2_PARAM_FILE); $ex = db2_execute($stmt); }
We use the flag db2_bind_param we give us more precision with the data type in our request. The first two variables, an id and a string, use DB2_PARAM_IN, an classical input parameter et we put your picture with DB2_PARAM_FILE. Note that the question marks are not an error !
It’s enough for now, I advice you to read the ebook “DB2 Express-C: The Developer Handbook for XML, PHP, C/C++, Java, and .NET‘. About the ibm_db2 functions, you can find them all on php.net.
Get documented with IBM Redbooks
![]()
It’s always very hard to find good documentation on some products (IBM or else). Internet is really nice, but sometime it’s the jungle, we can find lot of good, but also lot of bad/false.. Shop sells very good books, but come on.. they’re too expensive, I’m student, I can’t buy 10 or 20 books near 50/60€ each..
Hopefully, IBM have a huge ebook library named Redbooks. There are 5 kinds of IBM ebook, first is the classic RedBook, it’s a guide on one or multiple producs, Drat are RedBook in beta version, RedPapers are short technical articles, RedGuides are a kind of praticals business example, and TechNotes are brief technical help on something very precise.
IBM have more than 5.000 RedBooks, in 2008 more than 400 Redbooks have been published. Each month, more than 500.000 ebooks and 100.000 RedPapers are downloaded.
Here are some “must have” Redbooks:
- Up and Running with DB2 on Linux
- VSAM Demystified
- DB2 9 for z/OS Technical Overview
- ABCs of z/OS System Programming Volume 1 (on 11)
- DB2 9 for z/OS Performance Topics
And, the most important, all these ebooks are free !
Start on DB2 with IBM DB2 Express-C
You should have heard something about IBM DB2, but it’s still two letters and a number for you. We will see how to start with DB2, in the good and free way ! (and.. legal).
IBM DB2 is a relational database management system, using SQL language. Created in 1983 by IBM, and the last version is the 9.7 (Cobra). Compatible with the major operating systems (z/OS, AIX, Unix, Linux, Windows, Mac OS,..), DB2 ranks second in terms of market share, behind Oracle. There are several versions of DB2, more or less specialized and offer more or less services, but we will choose the free version, who can be use by students, developers, or small companies. DB2 Express-C is free to develop, deploy and distribute.
DB2 Express-C only have two limitations, which are not problem for us. It supports only 2 core and up to 2GB of RAM, which is enough for our tests. This version is available for Linux (32bit, 64bit and POWER), Windos (32bit and 64bit), Solaris (Intel 64bit), and Mac OS X (Intel 64bit), but unavailable for z/OS.. still not a problem for us ! There is no other limitation, we are free for the database size, number of connected users,.. Whether you develop in Java, .Net, Ruby, Python, Perl or pretty much any other programming language out there
We will proceed to the crucial stage, downloading DB2 Express-C 9.7 and install it on your system. To go further, don’t hesitate to consult the excellent ebook “Getting Started with DB2 Express-C” and watch the videos on channelDB2.