DB2 9 – XML and the Unprepared DBA

advertisement

DB2 9 – A DBA Guide to Native XML

4/15/2020

Agenda and Purpose

XML it looks easy enough

XML in DB2 9

XML testing 1 2 3

XML and Performance

Summary and Questions

Understand XML and related technology

DB2 9 implementation of native XML

Have an idea how it looks in the real world

Performance and design considerations for XML

Be able to discuss options for DB2 XML applications

Why Pure XML

o 85% of information is unstructured o 50 separate systems and 2 -3 ERP in average company o 30% of people’s time is spent searching for relevant information

40 Exabyte's (4 x 10 to the 19th) of unique information will be generated in 2007

Why XML

Information outlives technology

Information Outlives Technology

85% of information is unstructured

XML can represent just about anything

XML forces syntax-Level interoperability

XML Overview

› XML stands for Extensible Markup Language

› XML is a markup language much like HTML

› XML was designed to describe data

› XML tags are not predefined. You must define your own tags

› XML uses a XML Schema (XSD) to describe the data (DTD older technology)

› XML is a W3C Recommendation

XML Description - TAGS

<?xml version="1.0"?>

<contact-info>

<name> Mike Sniezek </name>

<company> BMC Software </company>

<phone> (713) 918-8800 </phone>

</contact-info>

XML Validation

XML DOCUMENT

<?xml version="1.0" ?>

<contact-info>

<name>Mike Sniezek</name>

XSD XML SCHEMA

<?xml version="1.0" encoding="utf-8"?>

<phone>(713) 918-8800</phone>

</contact-info> xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="contact-info">

<xs:complexType>

<xs:sequence>

<xs:element minOccurs="0" name ="name" type="xs:string" />

<xs:element minOccurs="0" name ="company" type="xs:string" />

<xs:element minOccurs="0" name ="phone" type="xs:string" />

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element msdata:IsDataSet="true" msdata:UseCurrentLocale="true" name="NewDataSet">

<xs:complexType>

<xs:choice minOccurs="0" maxOccurs="unbounded">

<xs:element ref="contact-info" />

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>

XML Transformation

› XSL consists of three parts:

– XSLT - a language for transforming XML documents

– XPath - a language for navigating in XML documents

– XSL-FO - a language for formatting XML documents

XML Document XSL Style sheet Parameters

XSL Transformation

Email XML WML Text SQL XHTML HTML

XML Data - Spread sheet used for examples

title

Empire Burlesque

Hide your heart

Greatest Hits

Still got the blues

Eros

One night only artist

Bob Dylan

Bonnie Tyler

Dolly Parton

Gary Moore

Eros Ramazzotti

Bee Gees

Sylvias Mother

Maggie May

Dr.Hook

Rod Stewart

Romanza Andrea Bocelli

When a man loves a woman Percy Sledge

Black angel

1999 Grammy Nominees

For the good times

Big Willie style

Tupelo Honey

Soulsville

Savage Rose

Many

Kenny Rogers

Will Smith

Van Morrison

Jorn Hoel

The very best of

Stop

Bridge of Spies

Private Dancer

Midt om natten

Pavarotti Gala Concert

The dock of the bay

Picture book

Red

Unchain my heart

Cat Stevens

Sam Brown

T`Pau

Tina Turner

Kim Larsen

Luciano Pavarotti

Otis Redding

Simply Red

The Communards

Joe Cocker

UK

UK

UK

UK

EU

UK

USA

EU

UK

USA country

USA

UK

USA

UK

EU

UK

UK

UK

EU

USA

EU

USA

UK

USA

UK

Norway

Island

A and M

Siren

Capitol

Medley

DECCA

Atlantic

Elektra

London

EMI company

Columbia

CBS Records

RCA

Virgin records

BMG

Polydor

CBS

Pickwick

Polydor

Atlantic

Mega

Grammy

Mucik Master

Columbia

Polydor

WEA price

7.8

9.9

7.9

7.2

7.8

8.9

8.9

7.9

8.9

8.2

8.1

8.5

10.8

8.7

10.9

10.2

8.7

9.9

8.2

7.9

10.9

9.9

9.9

10.2

9.9

10.9

year

1990

1988

1987

1983

1983

1991

1987

1985

1987

1987

1973

1990

1996

1987

1995

1999

1995

1997

1971

1996

1985

1988

1982

1990

1997

1998

XML Data from Spreadsheet

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited by XMLSpy® -->

<catalog>

<cd>

<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<country>USA</country>

<company>Columbia</company>

<price>10.90</price>

<year>1985</year>

</cd>

<cd>

<title>Hide your heart</title>

<artist>Bonnie Tyler</artist>

<country>UK</country>

<company>CBS Records</company>

<price>9.90</price>

<year>1988</year>

</cd>

XML Display with XLS

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited by XMLSpy® -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Title

Empire

Burlesque

Artist

Bob

Dylan

Using XML the Big Picture

<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle= http://www.w3.org/2001/12/soap-encoding >

<soap:Body>

<m:GetPrice xmlns:m="http://www.w3schools.com/prices">

<m:Item>Apples</m:Item>

</m:GetPrice>

</soap:Body>

</soap:Envelope>

<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body>

<m:GetPriceResponse xmlns:m="http://www.w3schools.com/prices">

<m:Price>1.90</m:Price>

</m:GetPriceResponse>

</soap:Body>

</soap:Envelope>

miracle

XML and DB2 v9

XML Data type / storage

CREATE TABLE FAVORITE_CDS

(NAME CHAR(20) NOT NULL,

CDID BIGINT,

CDINFO XML);

Tablespace

XML Tablespace

Partitioned by growth, if the base table space is not partitioned

Partitioned by range, if the base table space is partitioned

Table

XML column DOCID

DOCID

Index

DOCID, NODEID

Index

XML Tablespace

XML Table

DOCID NODEID XMLDATA XML User

Index

SYSXMLRELS

TBOWNER TBNAME

MKTMBS CLIENTS

MKTMBS FAVORITE

MKTMBS PURCHASEORDERS

COLNAME XML TABLE

CONTACTINFO XCLIENTS

CDINFO XFAVORITE_CDS

XMLPO XPURCHASEORDERS

Base

Table

XML column DOCID

SYSIBM.SYSXMLRELS

XML Table

DOCID NODEID XMLDATA

SYSXMLSTRINGS

SELECT <cd>

FROM "SYSIBM".SYSXMLSTRINGS

WHERE STRINGID > 1142

<artist>Bob Dylan</artist>

<country>USA</country>

STRINGID STRING

<company>Columbia</company>

<price>10.90</price>

<year>1985</year>

1143

1144

1145

1146

1147

COMPANY

YEAR

CD

<title>Hide your heart</title>

<artist>Bonnie Tyler</artist>

<country>UK</country>

<company>CBS Records</company>

NUMBER OF ROWS SELECTED 5

<price>9.90</price>

<year>1988</year>

</cd>

1143Hide your heart1143

Just going through the basics

Getting to DB2 9

------------------------------ Commands Entered -----------------------

------connect to DEDK user MVSMXS1 using ********;

-----------------------------------------------------------------------------connect to DEDK user MVSMXS1 using

Database Connection Information

Database server = DB2 OS/390 9.1.5

SQL authorization ID = MVSMXS1

Local database alias = DEDK

A JDBC connection to the target has succeeded.

XML Data Type – Purchase Orders

<?xml version="1.0"?>

<purchaseOrder orderDate="2008-01-20">

<shipTo country="US">

<name>Alice Smith</name>

<street>123 Maple Street</street>

<city>Mill Valley</city>

<state>CA</state>

<zip>90952</zip>

</shipTo>

<billTo country="US">

<name>Robert Smith</name>

<street>8 Oak Avenue</street>

<city>Old Town</city>

<state>PA</state>

<zip>95819</zip>

</billTo>

<comment>Hurry, my lawn is going wild</comment>

<items>

<item partNum="872-AA">

<productName>Lawnmower</productName>

<quantity>1</quantity>

<USPrice>148.95</USPrice>

<comment>Confirm this is electric</comment>

</item>

<item partNum="926-AA">

<productName>Baby Monitor</productName>

<quantity>1</quantity>

<USPrice>39.98</USPrice>

<shipDate>1999-05-21</shipDate>

</item>

</items>

</purchaseOrder>

DDL - Nothing To Worry About

CREATE TABLESPACE relData pagesize 4K managed by automatic storage bufferpool bp4k;

DROP TABLE PURCHASEORDERS;

CREATE TABLE PurchaseOrders (ponumber varchar(10) not null, podate date not null, status char(1),

XMLpo xml, primary key (ponumber));

CREATE TABLE PO LIKE PurchaseOrders;

CREATE VIEW ValidPurchaseOrders as

SELECT ponumber, podate, XMLpo

FROM PurchaseOrders

WHERE status = ‘A’;

ALTER TABLE PurchaseOrders

ADD revisedXMLpo xml;

Manipulation

UPDATE PurchaseOrders SET XMLpo = :XMLpo_revised

WHERE ponumber = ‘12345’;

INSERT INTO PurchaseOrders VALUES (‘200300001’,CURRENT DATE,

‘A’, :xmlPo);

INSERT INTO PurchaseOrders VALUES (‘200300003’, CURRENT DATE,

‘A’, XMLPARSE (DOCUMENT :vchar PRESERVE WHITESPACE ) );

INSERT into PurchaseOrders VALUES( '200300004', CURRENT DATE,

'A',DSN_ XMLValidate (:lobPo, ’SYSXSR.myPOSchema’));

DELETE FROM PurchaseOrders WHERE ponumber = ‘12345’

Retrieval

SELECT XMLpo INTO :xmlPo

FROM PurchaseOrders

WHERE ponumber = ‘200300001’;

SELECT XMLPO

FROM PurchaseOrders

WHERE XMLEXISTS (‘//items/item[productName = “Baby Monitor”]’

PASSING XMLpo);

SELECT XMLQUERY (‘//items/item/quantity’ PASSING XMLpo)

FROM PurchaseOrders WHERE …;

Indexes

CREATE INDEX ON

PurchaseOrders(XMLPO)

Generate Keys Using

XMLPATTERN

‘/purchaseOrder/items/item/productName’ as SQL VARCHAR(100);

Index will be used for this query.

SELECT XMLPO

FROM PurchaseOrders

XMLEXISTS(‘/purchaseOrder/items/item[productName =

“Lawnmower”]’ passing XMLPO)

Validation XML Schema Support

DB2 requires a SQL identifier for identification.

REGISTER XMLSCHEMA http://www.test.com/order.xsd

FROM file://C:/xmlschema/order.xsd

AS ORDERSCHEMA

COMPLETE [ENABLE DECOMPOSITION];

REMOVE XMLSCHEMA ORDERSCHEMA;

XML Performance

How are you going to use the XML?

INSERT Performance

INSERT

› The obvious the larger and more complex the XML column is the more expensive the insert.

› The more indexes the more overhead

› INSERT with VALIDATE is at least double the overhead

› Use host variables rather than Literals

› Use LOAD instead of SQL INSERT (30 to 40 percent)

Update

› When updating an XML document, an

SQL UPDATE statement is equivalent to performing an SQL DELETE and

INSERT and the performance will be about the same.

› If this is going to happen allot then consider splitting the XML into smaller pieces

Select Performance

› Performance for SQL no real change, the size and complexity of the XML document will determine overhead.

Well coded SQL proper indexes and physical design still make the big difference.

› XML Indexes are different and good ones make a great deal of difference.

CREATE INDEX ON

PurchaseOrders(XMLPO)

Generate Keys Using

XMLPATTERN

‘/purchaseOrder/items/item/productName’ as SQL VARCHAR(100);

Data Considerations

Native XML is good when you need

CREATE TABLE CD_CATALOG

(TITLE VARCHAR(30),

ARTIST VARCHAR(30) ,

Schema flexibility

Search Performance

COUNTRY VARCHAR(25) ,

COMPANY VARCHAR (25)

PRICE DECIMAL(5,2) ,

YEAR SMALINT);

Partial document retrieval

CREATE TABLE CD_CATALOG

(PID VARCHAR(10) NOT NULL ,

OWNER VARCHAR(30) ,

DESCRIPTION XML);

CREATE TABLE CD_CATALOG

(NAME CHAR(20) NOT NULL,

CDID BIGINT,

CDINFO XML,

PRIMARY KEY (NAME));

Summary

› XML is the current standard for sharing data

› How your organization is exploiting XML or will be exploiting XML should be understood by Database Administrators

› You should be familiar of how DB2 9 stores and catalogs the XML data type

› You don’t have to be an SQL GURU but be aware of performance characteristics

› Even if your organization has no immediate plans to use the XML data type your understanding maybe key to the future of new applications. – Try it!

› If you have questions mike_sniezek@bmc.com

Some useful links

› http://www.eccnet.com/acronyms/acronyms.php

- related acronyms

› http://www.w3schools.com/xml/default.asp

- XML Tutorial

› http://www.w3.org/TR/xml/ - XML 1.0 Standard:

› http://www.w3.org/TR/xpath-datamodel/ - XPATH and XQUERY

› http://www.w3.org/TR/xmlschema-0/ - Schema

› http://xml.coverpages.org/xmlApplications.html

- XML applications and standards

› http://www.w3schools.com/xpath/default.asp

-XPATH Tutorial

› http://www.redbooks.ibm.com/redbooks/pdfs/sg247330.pdf

-Red

Book

› http://www.w3schools.com/xml/xml_examples.asp

- examples

Download