XML technologies / Oracle & XML / Working with XML / Storing XML data in the database

(Excerpt from "Oracle PL/SQL: Das umfassende Handbuch", chapter 16 "Arbeiten mit XML", by Jürgen Sieben)

The data type XMLType can be stored in three basic ways:

  1. Storage as CLOB
    This method writes the XML instance as a text stream into a CLOB (Character Large Object), preserving whitespace and order. It is the simplest method and fastest for reading or writing entire instances. Accessing or modifying parts, however, requires either building a complete DOM tree or reading the entire document, changing it externally and writing it back. These operations consume time and resources. More powerful methods will probably make this approach less common.

  2. Storage as BinaryXML
    This newer method, available from version 11, uses what Oracle calls Compacted Schema-aware XML (CSX). It is nearly as compact as compressed storage but optimized for fine-grained element access. This eases indexing and permits validation and partial changes or extensions without a DOM tree. Updated Oracle parsers also exchange CSX over the network for better performance. BinaryXML is compact, fast to write and read, and quick and easy to search and modify.

  3. Storage in object-relational tables
    Unlike the first two methods, which may optionally use an XML schema, object-relational XML storage requires one. Schema annotations specify which elements belong in which object-relational tables, creating an automatic mapping. Oracle can create these tables itself. This gives very fast element access and permits B*-tree indexing, whereas other methods use the capable but generic XMLIndex. Reading and writing whole instances is slower because they must be split and reassembled. Order and whitespace are not protected, so this suits cases where they are unimportant. The benefit is very fast access to element and attribute values.

Decide whether stored instances should validate against a schema. Registration details appear under Managing documents through XDB. In short, database validation requires loading and registering the schema using the PL/SQL package dbms_xmlschema. The schema is then referenced when creating the table or XMLType column through its namespace. This is mandatory for object-relational storage. For the other methods, registering a schema also enables validation before insertion and thus improves data quality.

Storage choice depends chiefly on use; changing it later is costly. General advice is difficult because scenarios differ. Yet discussions in books, documentation and forums seem too XML-centered, rarely asking whether XML must be persisted at all. That should be the first question: why store it as XML? I dislike order systems storing every order as an XML instance. To me these are message-based systems, whose information should normally go straight into relational tables. I would hesitate to use Oracle-generated object-relational tables, mainly because they impose SQL-query restrictions unusual for relational databases.

If XML must be stored, ask whether you need its internal data or only the complete instance. For whole-instance use, choose either XMLType and CLOB storage or BinaryXML, preferable when element access is also needed. If you merely need storage and XML’s nature is irrelevant, consider BLOB (CLOB could cause character-encoding problems).

If relational storage is needed and XML is only transport, use object-relational storage or manually shred XML into relational tables in Java (where SAX avoids requiring a DOM tree). For simple incoming XML, especially when element names match table names, direct mapping through dbms_xmlstore can store XML directly in those tables. This package does not support more complex mapping tasks.

Object-relational storage is easier initially but can complicate later development and administration. The key issue, as so often, is schema migration: how should the database respond to message-format changes? SQL restrictions also matter. In my experience, this approach mostly attracts application-focused developers seeking easy database mapping. Annotated schemas can also automate object-type and even JavaBean generation. I am not one of those developers and favor this approach less, but my preferences need not be yours.

New features in version 12c
Technologies are increasingly standardized. Since 11g, XQuery has expanded its role in processing existing XMLType instances. Proprietary functions such as extract, updatexml and others are deprecated in favor of XQuery counterparts XQuery, XMLTable, XMLExists and others. This does not always help users: updatexml was more intuitive than complex XQuery, but that is the direction of development. Learning XQuery now seems essential for XML developers using Oracle.
When generating XMLType instances from relational data, Oracle appears to favor SQL/XML functions such as xmlelement, xmlattributes and xmlforest over object-oriented generation through sys_xmlgen, which is deprecated.
For storing XMLType instances, CLOB storage has been superseded by BinaryXML. As in 11g, indexing uses only the domain index XMLIndex, except that object-relational storage remains possible. Under present conditions it seems less necessary, since XMLIndex has greatly improved XML-element access speed.

  

 

 

 

back to top