简介
DB2 9 中的 pureXML 支持为管理 XML 数据提供了有效的、多方面的功能。对于很多 XML 应用程序而言,性能是高度优先考虑的一个方面。DBA 和应用程序设计人员都可以通过他们份内的工作来确保良好的性能。首先,我们有关于 DB2 各个方面的所有传统的性能指南,包括 CPU/内存/磁盘配置的平衡、表空间和缓冲池的调优、锁、日志记录、查询执行计划等。所有这些话题在之前的 DB2 文章里都曾论述过(见 参考资料),并在管理 DB2 中的 XML 数据时仍然适用。
DB2 9 是同时支持关系型数据与 XML 数据的新一代数据服务器。请到 DB2 9 技术资源中心 获取更多的关于 DB2 9 的技术文章、教程、下载以及多媒体信息。在对这些性能提示的讨论中,我们假设您熟悉基本的 DB2 管理和性能实践,并熟悉基本的 DB2s pureXML 支持。例如,您应该知道 XML 列、XML 索引,以及如何用 SQL/XML 和 XQuery 查询 XML 数据。
select info from customerwhere xmlexists('$i/customerinfo[phone = $p]' passing info as "i", cast(? as varchar(12)) as "p") |
提示 15:避免 XML 插入和检索期间出现代码页转换
create table customer(info XML);select info from customer where...;select xmlquery('$i/customerinfo/name' passing info as "i") from customerwhere...; |
select XMLSERIALIZE(info as BLOB(1M)) from customer where...; |
runstats on table myschema.customer with distribution on all columns and detailed indexes all;runstats on table myschema.customer with distribution on all columns and detailed indexes all tablesample system (10);runstats on table myschema.customer on all columns and sample detailed indexes all tablesample bernoulli (15); |
提示 12:如何使用 SQL/XML 发布视图将关系数据暴露为 XML
create table unit( unitID char(8), name char(20), manager varchar(20));create view UnitView(unitID, name, unitdoc) as select unitID, name, XMLELEMENT(NAME "Unit", XMLELEMENT(NAME "ID", u,unitID), XMLELEMENT(NAME "UnitName", u.name), XMLELEMENT(NAME "Mgr", u.manager) ) from unit u; |
select unitdocfrom UnitViewwhere xmlexists('$i/Unit[ID = "WWPR"]' passing unitdoc as "i");for $u in db2-fn:xmlcolumn('UNITVIEW.UNITDOC')/UNIT where $u/ID = "WWPR"return $u; |
select unitdocfrom UnitViewwhere UnitID = "WWPR"; |
提示 13:如何使用 XMLTABLE 视图以关系格式暴露 XML 数据
创建一个视图以 XML 格式暴露关系数据,这样做很有用。同样,您可能也想使用一个视图将 XML 数据暴露为关系格式。提示 12 中的提醒也适用于这里的情况,只不过顺序要倒过来。让我们来看看下面的例子,其中使用了 SQL/XML 函数 XMLTABLE 以标量格式返回 XML 文档中的值:
create table customer(info XML);create view myview(CustomerID, Name, Zip, Info) as SELECT T.*, info FROM customer, XMLTABLE ('$c/customerinfo' passing info as "c" COLUMNS "CID" INTEGER PATH './@Cid', "Name" VARCHAR(30) PATH './name', "Zip" CHAR(12) PATH './addr/pcode' ) as T; |
-- may perform suboptimal:select CustomerID, Name from myviewwhere Zip = "95141";-- will perform well:select CustomerID, Namefrom myViewwhere xmlexists('$i/customerinfo[addr/pcode = "95141"]' passing info as "i"); |
提示 14:对于短小的查询或 OLTP 应用程序,使用带参数占位符的 SQL/XML 语句
for $c in db2-fn:xmlcolumn('CUSTOMER.INFO')/customerwhere $c/phone = "905-555-4789"return $c;select info from customerwhere xmlexists('$i/customerinfo[phone = "905-555-4789"]' passing info as "i") |
[table=0%] |
提示 10:使用方括号 [ ] 来避免 XMLEXISTS 中的 Boolean 谓词
提示 11:使用 RUNSTATS 收集 XML 数据和索引的统计信息
提示 8:定义倾斜的 XML 索引,并避免为任何东西都建索引
提示 9:将文档过滤谓词放入 XMLEXISTS 中,而不是放入 XMLQUERY 中
[table=0%] |
提示 5:使用新的快照监视器元素检查 XML 性能
Buffer pool data logical reads = 221759Buffer pool data physical reads = 48580Buffer pool temporary data logical reads = 10730Buffer pool temporary data physical reads = 0Buffer pool data writes = 6Asynchronous pool data page reads = 0Asynchronous pool data page writes = 6Buffer pool index logical reads = 8340915Buffer pool index physical reads = 54517Buffer pool temporary index logical reads = 0Buffer pool temporary index physical reads = 0Buffer pool index writes = 0Asynchronous pool index page reads = 0Asynchronous pool index page writes = 0Buffer pool xda logical reads = 2533633Buffer pool xda physical reads = 189056Buffer pool temporary xda logical reads = 374243Buffer pool temporary xda physical reads = 0Buffer pool xda writes = 0Asynchronous pool xda page reads = 97728Asynchronous pool xda page writes = 0Asynchronous data read requests = 0Asynchronous index read requests = 0Asynchronous xda read requests = 83528 |
提示 6:了解 XML 模式验证的开销
XML 模式可以定义一组 XML 文档所允许的结构、元素和属性、它们的数据类型、取值范围等。DB2 允许(可选地)根据 XML 模式验证 XML 文档。如果选择验证文档,那么通常是在插入时进行验证。这是为了达到两个目的。首先,通过验证可以确保插入到数据库中的数据符合模式定义,也就是说可以防止将 “垃圾数据(junk data)” 输入到表中。其次,模式验证将来自模式的类型注释添加到每个 XML 元素和属性,这些类型在 DB2 XML 存储中得到持久化。例如,如果一个 XML 模式定义 dept 表中的雇员 ID(如 提示 1 所示)为整数,并根据该模式对文档进行验证,那么 DB2 会记得,在每个文档中雇员 ID 具有类型 xs:integer。在查询运行时,尝试在雇员 ID 上执行字符串比较将遭到失败,并产生一个类型错误。
提示 7:在 XPath 表达式中,尽可能使用全限定路径
create table customer(info XML); |
|
提示 4:如何配置 DB2,以便快速地成块插入 XML 数据
提示 2:为了取得更好的 XML 性能,使用 DMS 和更大的页
提示 3:必要时,如何将 XML 数据放入一个单独的表空间中
create bufferpool bp4k pagesize 4k;create bufferpool bp32k pagesize 32k; create tablespace relDatapagesize 4Kmanaged by automatic storagebufferpool bp4k; create tablespace xmlDatapagesize 32Kmanaged by automatic storagebufferpool bp32k; create table product(pid bigint, name varchar(20), brand varchar(35), category integer, price decimal, description XML) in relDatalong in xmlData; |
DB2 XML 性能提示
提示 1: 理智选择 XML 文档的粒度
unitID | deptdoc |
WWPR | [table=0%] |