Using declared temporary tables with XML data
Declared global temporary tables can be useful when implementing solutions that require the creation of non-persistent tables. For example, an application can create a declared temporary table to process intermediate results. When the application session ends, the temporary table is dropped.
A declared global temporary tables exists only for the session during which it was declared. The table cannot be shared with other sessions. When the session ends, the rows of the table are deleted, and the description of the temporary table is dropped. Declared temporary tables do not have catalog contention issues because there is no catalog entry for declared temporary tables.
Declared global temporary table can contain XML columns and XML data can be queried and updated. Declared global temporary tables can also be used in a partitioned database environment. When the temporary table contains a column specified as a partitioning key, work performed on the XML data in the temporary table can be distributed among the database partitions.
Examples
<company name="MyFirstComany">
<emp id="31201" salary="60000" gender="Female">
<name>
<first>Laura</first>
<last>Brown</last>
</name>
<dept id="M25">Finance</dept>
</emp>
</company>
CREATE TABLE COMPANYINFO (ID INT, DOC XML)
DECLARE GLOBAL TEMPORARY TABLE INSTMPTB (ID INT, DOC XML)
ON COMMIT DELETE ROWS in USR_TBSP
DECLARE GLOBAL TEMPORARY TABLE INSTMPTB LIKE COMPANYINFO
ON COMMIT DELETE ROWS in USR_TBSP
DECLARE GLOBAL TEMPORARY TABLE TEMPTB (ID INT, DOC XML INLINE LENGTH 3000)
ON COMMIT PRESERVE ROWS NOT LOGGED
CREATE INDEX SESSION.TEMP_IDX ON SESSION.INSTMPTB (DOC)
GENERATE KEY USING XMLPATTERN '/company/emp/@id'
AS SQL INTEGER
CREATE UNIQUE INDEX SESSION.TEMP_IDX2 ON SESSION.INSTMPTB (DOC)
GENERATE KEY USING XMLPATTERN '/company/name/last'
AS SQL VARCHAR(100)
DECLARE GLOBAL TEMPORARY TABLE INSTMPTB (ID INT, DOC XML)
ON COMMIT DELETE ROWS
IN USR_TBSP
DISTRIBUTE BY HASH (ID)
XQUERY
for $i in db2-fn:xmlcolumn("SESSION.INSTMPTB.DOC")/company/emp
for $j in db2-fn:xmlcolumn("COMPANYINFO.DOC ")[/company/emp/@id = $i/@id ]
where $i/dept = "Finance"
return $j ;
INSERT INTO COMPANYINFO FROM
(SELECT ID, DOC FROM SESSION.INSTMPTB)
Usage notes
- Data row compression is enabled for a declared temporary table. When the database manager determines that there is a performance gain, table row data including XML documents stored inline in the base table object is compressed. However, data compression of the XML storage object of a declared temporary table is not supported.
- In a partitioned database environment, use the DISTRIBUTE BY clause to create a declared temporary table with partitioning key. If you create a declared temporary table using the LIKE clause and the source table has a partitioning key, the temporary table does not have a partitioning key.
- Index compression is enabled for indexes that are created on declared temporary tables including user-created indexes over XML data.
- User-created indexes over XML data have the same restrictions as indexes created for non-temporary tables. For example, in a partitioned database environment, unique XML indexes over XML data are not supported.