Detailed description of the custom XML storage and use environment in DB2

Source: Internet
Author: User
Tags ibm db2

BKJIA: If you use IBM DB2 for z/OS, DB2 for Linux, UNIX, and Windows (LUW), there is no problem. Let's review when to use XML storage, and some best practices for customizing XML storage!

For an image description, I will use an XML document with the following content:

 
 
  1. <order OrderID="9001" OrderDate="2009-10-18">> 
  2.  <customerID>26914</customerID> 
  3.  <item id="LK-486"> 
  4.   <name>Magic Potion</name> 
  5.   <size>300ml</size> 
  6.   <price>19.99</price> 
  7.  </item> 
  8.  <item id="VF-145"> 
  9.   <name>Crystal Ball, Deluxe</name> 
  10.   <color>crystal clear</color> 
  11.   <price>295.00</price> 
  12.  </item> 
  13. </order> 


It shows an order XML document that includes the order ID, date, customer ID, and other items. Note that some items have different descriptions, such as size and color. We assume that we need to manage many XML documents similar to this type in DB2.

How to split and reorganize XML

In another article, "15 DB2 pureXML performance best practices", I talked about how to choose document granularity wisely, in fact, it is necessary to match the XML document stored in DB2 with the business logic object of the application and the main access granularity.

In our example, assuming that the order changes frequently, reading, adding, or deleting entries in the order is the most critical operation and requires the best performance. In this case, you can consider splitting the order document and storing each entry as an independent document in each row of the DB2 table. This storage method is compared with the method used to store the original complete order document) the advantage is that it makes operations on the stored data easier and faster:

You can use a single row to read and retrieve an entry. You do not need to extract entries from a complete order document;

You can simply delete an entry from the order by deleting the rows in the table, and you do not need to operate the complete order document;

You can quickly Insert a new entry into the order without the need to complete the order document.

This easy addition and removal of order entries is particularly valuable in DB2 9 for z/OS, because this version does not support adding or deleting elements in existing XML documents.
The following code shows the definition of a table and the INSERT statement for splitting an order document. The related columns store the order ID, customer ID, order date, and entry sequential number respectively.

 
 
  1. CREATE TABLE items(ordID INTEGER, custID INTEGER,  
  2.                                    odate DATE, seqNo INTEGER, item XML);  
  3. INSERT INTO items(ordID, custID, odate, seqno, item)  
  4.  SELECT T.ordID, T.custID, T.odate, T.seqno, XMLDOCUMENT( T.item)  
  5.  FROM 
  6.   XMLTABLE('$d/order/item' PASSING cast(? AS XML) "d" 
  7.    COLUMNS  
  8.     ordID        INTEGER    PATH      '../@OrderID',  
  9.     custID       INTEGER    PATH      '../customerID' 
  10.     odate        DATE       PATH      '../@OrderDate',  
  11.     seqNo        FOR ORDINALITY,  
  12.     item         XML        PATH      '.') AS T; 

The entry information is stored in XML format, because the entry may have different elements and attributes, such:

 
 
  1. ORDID     CUSTID     ODATE     SEQNO     ITEM  
  2. -----     -----     ------     -----     -----  
  3. 9001     26914     10/18/2009     1   <item id="LK-486">  
  4.                                         <name>Magic Potion</name>  
  5.                                         <size>300ml</size>  
  6.                                         <price>19.99</price>  
  7.                                       </item>  
  8. 9001     26914     10/18/2009     2   <item id="VF-145">  
  9.                                         <name>Crystal Ball, Deluxe</name>  
  10.                                         <color>crystal clear</color>  
  11.                                         <price>295.00</price>  
  12.                                       </item>  
  13. 2 record(s) selected. 

The INSERT statement includes an XMLTABLE function. This function extracts the values inserted into the items table from the input XML file. It splits the input XML file and generates a document with independent entries. The XMLTABLE function includes a parameter through which the application can pass an order document and use the XPath expression $ d/order/item. The XMLTABLE function generates a row of data for each entry in the input document, then, the Order ID, customer ID, and order date are extracted. The special column definition for ordinality is numbered FOR each row generated. The XMLDOCUMENT function ensures that each entry segment can be inserted as an independent XML document.

The code above shows the data in the items table after the XML document is inserted using the INSERT statement. The code below shows how to recreate the original order document, the XMLELEMENT and XMLATTRIBUTES functions use the values of relevant columns in the items table to construct the top document. The XMLAGG function combines all entries and finally forms a complete order document. Note that xmlparts includes an optional order by clause in the seqno column. This ensures that the ORDER document after restoration is consistent with the ORDER in the original document.

 
 
  1. SELECT XMLELEMENT(name "order",  
  2.          XMLATTRIBUTES(ordID AS "OrderID", odate as "OrderDate"),  
  3.          XMLELEMENT(name "customerID", custID)  
  4.          XMLAGG(item ORDER BY seqno) )  
  5. FROM items  
  6. WHERE ordID = 9001  
  7. GROUP BY ordID, odate, custID; 

Use generate Columns

The new IBM DB2 pureXML Feature in DB2 9.7 for LUW allows you to use XML columns with Database partition Feature Database Partitioning Feature, DPF), range Partition Table and multi-dimensional cluster MDC) tables, however, the partition or cluster key must be composed of related columns. You have seen how to use INSERT and XMLTABLE to extract values from XML documents to related columns. You can use these associated columns to partition or cluster tables. If you prefer to use simple INSERT statements in a program and do not know how to extract data, you can consider using a generated column.

DB2 9.7 supports XML parameters in user-defined function udfs, allowing you to define generated columns and automatically fill in values in the inserted XML document. The following code shows a UDF that accepts an XML document as input. For example, in the order document in the previous example, this UDF uses the XMLCAST and XMLQUERY functions to extract the OrderDate attribute of the input document:

 
 
  1. CREATE FUNCTION extractDate(doc XML)  
  2.   RETURNS DATE 
  3.   LANGUAGE SQL CONTAINS SQL  
  4.   NO EXTERNAL ACTION DETERMINISTIC  
  5.   RETURN XMLCAST(XMLQUERY('$d/order/@OrderDate' 
  6.          PASSING doc AS "d") AS DATE); 

You can use this UDF in SELECT queries and other SQL statements, but you also need to define a generated column. For the following example, it is assumed that retrieving and inserting a complete order is the most critical operation, in this case, it is best to store order documents completely. The following code defines a table that uses XML columns to store orders, and automatically extracts order dates and fills them in the associated column odate. An INSERT statement can now INSERT an XML document to the order column without the need to extract values to the associated column:

 
 
  1. CREATE TABLE orders(  
  2.   order XML,  
  3.   odate DATE GENERATED ALWAYS AS (extractDate(order))); 

If you store many orders continuously, you may need to archive old orders. Using range partitioning is the best choice, the following code shows that table order2 is partitioned by the value of the odate column, and the odate column is generated from the XML column. Similarly, you can use the generated columns as the distribution key of the partitioned database or the cluster key of the MDC table:

 
 
  1. CREATE TABLE order2(  
  2.   order XML,  
  3.   odate DATE GENERATED ALWAYS AS (extractDate(order)) NOT NULL)  
  4.  PARTITION BY RANGE (odate)  
  5.  (PART q109 STARTING('01-01-2009') ENDING ('03-31-2009') INCLUSIVE,  
  6.  PART q209 ENDING ('06-30-2009') INCLUSIVE,  
  7.  PART q309 ENDING ('09-30-2009') INCLUSIVE,  
  8.  PART q409 ENDING ('12-31-2009') INCLUSIVE); 

Control XML storage

Custom XML storage has many advantages. Splitting large XML documents into multiple small documents makes it easier and more efficient to operate XML data, using UDF definition to generate columns can simplify the extraction of XML values to associated columns. Using the generated columns can also help you manage partition databases, range partition tables, or XML in MDC tables.

Source: http://www.ibm.com/developerworks/data/library/dmmag/DMMag_2009_Issue3/Tips/index.html

Original article: Customizing XML storage in DB2

Author: Matthias Nicla

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.