TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009
Transcript of TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009
© 2008 IBM Corporation
®
TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009
Frank MerinoSr. IT SpecialistDB2 for z/OS [email protected]
IBM Software Group | Information Management software
2
Information contained in this material has not been submitted to any formal IBM review and is distributed on "as is" basis without any warranty either expressed or implied. Measurements data have been obtained in laboratory environment. Information in this presentation about IBM's future plans reflect current thinking and is subject to change at IBM's business discretion. You should not rely on such information to make business plans. The use of this information is a customer responsibility.
IBM MAY HAVE PATENTS OR PENDING PATENT APPLICATIONS COVERING SUBJECT MATTER IN THIS DOCUMENT. THE FURNISHING OF THIS DOCUMENT DOES NOT IMPLY GIVING LICENSE TO THESE PATENTS.
TRADEMARKS: THE FOLLOWING TERMS ARE TRADEMARKS OR ® REGISTERED TRADEMARKS OF THE IBM CORPORATION IN THE UNITED STATES AND/OR OTHER COUNTRIES: AIX, AS/400, DATABASE 2, DB2, e-business logo, Enterprise Storage Server, ESCON, FICON, OS/390, OS/400, ES/9000, MVS/ESA, Netfinity, RISC, RISC SYSTEM/6000, iSeries, pSeries, xSeries, SYSTEM/390, IBM, Lotus, NOTES, WebSphere, z/Architecture, z/OS, System z, System p
The FOLLOWING TERMS ARE TRADEMARKS OR REGISTERED TRADEMARKS OF THE MICROSOFT CORPORATION IN THE UNITED STATES AND/OR OTHER COUNTRIES: MICROSOFT, WINDOWS, WINDOWS NT, ODBC, WINDOWS 95
For additional information see ibm.com/legal/copytrade.phtml
Disclaimer and Trademarks
IBM Software Group | Information Management software
3
DB2 V8 LNL … Agenda
Multi-row FETCH
Multi-row INSERT
GET DIAGNOSTICS
Common table expression (CTE)
Sequence objects
INSERT within SELECT
Materialized Query Table (MQT)
Additional SQL Enhancements
Documentation
Questions
IBM Software Group | Information Management software
4
Multi-Row FETCH and INSERT
What are they?
– Multi-row FETCH:• The ability to retrieve multiple rows of data as a rowset.
– Rowset: A group of rows that are grouped together and operated on as a set.
– Multi-row INSERT:• The ability to insert one or more rows.• Like the INSERT INTO… SELECT FROM, except using a
VALUES clause.
– Both can be used in static and dynamic SQL
IBM Software Group | Information Management software
5
Multi-row FETCH … Syntax
DECLARE CURSOR Syntax:DECLARE cursor-name CURSOR
WITH ROWSET POSITIONINGFOR SELECT select-statement
NOTES:• WITH ROWSET POSITIONING defines whether multiple rows can be
retrieved as a rowset on a single FETCH• Default is WITHOUT ROWSET POSITIONING
IBM Software Group | Information Management software
6
Multi-row FETCH … Syntax (continued)
FETCH Syntax:FETCH NEXT ROWSET
PRIOR ROWSETFIRST ROWSETLAST ROWSETCURRENT ROWSETROWSET STARTING AT ABSOLUTE host-variable or integer-value
RELATIVE
FROM cursor-name FOR host-variable ROWS INTO host-variable-arrayinteger-value
NOTE: The rowset-positioned clauses specify cursor positioning like the existing row-positioned clauses NEXT, PRIOR, FIRST, LAST, CURRENT, ABSOLUTE and RELATIVE.
IBM Software Group | Information Management software
7
Multi-row FETCH … Notes
Recommend always coding the FOR n ROWS clause, which controls the number of rows retrieved by a FETCH.
FETCH FIRST n ROWS ONLY clause can be coded in the SELECT. However, this controls the total number of rows that can retrieved. SQLCODE +100 will occur if attempt to retrieve beyond the numberspecified.
Host-variable-array:– One array per column being retrieved.
– Size needs to be large enough to hold number of rows retrieved.
– Null indicator array needed to handle nulls, and size must equal column array.
Locks are held on all rows in rowset depending on isolation level.
IBM Software Group | Information Management software
8
Multi-row FETCH … Examples
Given cursor C1 is defined as:DECLARE C1 CURSOR WITH ROWSET POSITIONING
FOR SELECT COL1, COL2 FROM T1
Fetch 5 rows from the current cursor position:FETCH NEXT ROWSET FROM C1 FOR 5 ROWS INTO :HVA1,:HVA2
Fetch 5 rows starting with row 20 regardless of the current position of the cursor, and cause the cursor to be positioned on that rowset at the completion of the fetch:
FETCH ROWSET STARTING AT ABSOLUTE 20 FROM C1 FOR 5 ROWS INTO :HVA1,:HVA2
Fetch the previous rowset, and have the cursor positioned on that rowset:FETCH PRIOR ROWSET FROM C1 FOR 5 ROWS INTO :HVA1,:HVA2 … or …FETCH ROWSET STARTING AT RELATIVE -5 FROM C1 FOR 3 ROWS INTO :HVA1,:HVA2
Fetch the first 5 rows and leave the cursor positioned on that rowset at the completion of the fetch:
FETCH FIRST ROWSET FROM C1 FOR 5 ROWS INTO :HVA1,:HVA2
IBM Software Group | Information Management software
9
Multi-row FETCH … Cursor Positioning
IBM Software Group | Information Management software
10
Multi-row FETCH … Cursor Positioning - Partial Rowset
IBM Software Group | Information Management software
11
Multi-row FETCH … COBOL ExampleDeclare cursor C1 and fetch 10 rows using a multi-row FETCH statement
01 OUTPUT-VARS.05 NAME OCCURS 10 TIMES.
49 NAME-LEN PIC S9(4) USAGE COMP.49 NAME-TEXT PIC X(40).
05 SERIAL-NUMBER PIC S9(9) USAGE COMP OCCURS 10 TIMES.01 IND-VARS.
10 INDSTRUC1 PIC S9(4) USAGE COMP OCCURS 10 TIMES.10 INDSTRUC2 PIC S9(4) USAGE COMP OCCURS 10 TIMES.
PROCEDURE DIVISION.EXEC SQL
DECLARE C1 SCROLL CURSOR WITH ROWSET POSITIONING FORSELECT NAME, SERIAL# FROM EMPLOYEE
END-EXEC.EXEC SQL OPEN C1 END-EXEC.EXEC SQL
FETCH FIRST ROWSET FROM C1 FOR 10 ROWSINTO :NAME:INDSTRUC1,:SERIAL-NUMBER:INDSTRUC2
END-EXEC.
IBM Software Group | Information Management software
12
Multi-row FETCH … Positioned UPDATE
Syntax:UPDATE table-name SET assignment-clause
WHERE CURRENT OF cursor-name
FOR ROW host-variable OF ROWSETinteger-value
Examples: (Cursor CS1 positioned on a 10 row rowset of table T1.)– Following statement updates all 10 rows of the rowset:
UPDATE T1 SET C1 = 5 WHERE CURRENT OF CS1
– Following statement updates row 4 of the rowset:UPDATE T1 SET COL1='ABC‘ WHERE CURRENT OF CS1 FOR ROW 4 OF
ROWSET
IBM Software Group | Information Management software
13
Multi-row FETCH … Positioned DELETE
Syntax:DELETE FROM table-name
WHERE CURRENT OF cursor-name
FOR ROW host-variable OF ROWSETinteger-value
Examples: (Cursor CS1 positioned on a 10 row rowset of table T1.)– Following statement deletes all 10 rows of the rowset:
DELETE FROM T1 WHERE CURRENT OF CS1
– Following statement deletes row 4 of the rowset:DELETE FROM T1 WHERE CURRENT OF CS1 FOR ROW 4 OF ROWSET
IBM Software Group | Information Management software
14
Multi-row INSERT … Syntax
INSERT Syntax:INSERT INTO table-name (column-name1, etc)
FOR host-variable ROWS integer-value
VALUES ( expression or host-variable-array or NULL or DEFAULT, etc.)
ATOMIC or NOT ATOMIC CONTINUE ON SQLEXCEPTION
NOTES:– Same rules apply for host-variable-array as with FETCH– For dynamic statements:
• FOR n ROWS specified on EXECUTE statement• ATOMIC/NOT ATOMIC CONTINUE ON SQLEXCEPTION specified on PREPARE
statement
IBM Software Group | Information Management software
15
Multi-row INSERT … ATOMIC/NOT ATOMIC…
ATOMIC (default):If any insert fails, all changes are undone.
NOT ATOMIC CONTINUE ON SQLEXCEPTION– Allows processing to continue if any insert fails
– Use GET DIAGNOSTICS to examine failure by row
– SQLCODEs & SQLSTATEs:• All inserts failed: SQLCODE -254 & SQLSTATE 22530• All inserts successful, but warnings: SQLCODE +252 & SQLSTATE 01659• At least one insert failed: SQLCODE -253 & SQLSTATE 22529
IBM Software Group | Information Management software
16
Multi-row INSERT … Examples
Insert 10 rows of data into table with 2 columns:INSERT INTO T1 (C1, C2) FOR 10 ROWSVALUES (:hva1:hvaind1, :hva2:hvaind2)NOT ATOMIC CONTINUE ON SQLEXCEPTION
Insert a variable number of rows for a column in a table:INSERT INTO T1 FOR :hv ROWSVALUES (:hva:hvaind) ATOMIC
Note: SQLCODE -246 & SQLSTATE 42873 returned if host-variable- array has fewer entries than specified by :hv. On the other hand, if array has more, excess is ignored.
IBM Software Group | Information Management software
17
GET DIAGNOSTICS
Enables applications to retrieve diagnostic information.
Complements and extends diagnostics available in the SQLCA.
Can be used in conjunction with or instead of the SQLCA to interrogate the results of SQL statements.
Is especially important when using multi-row INSERT with NOT ATOMIC CONTINUE ON SQLEXCEPTION.
Can only be embedded in an application program.
IBM Software Group | Information Management software
18
GET DIAGNOSTICS … Syntax
GET DIAGNOSTICS statement-information
condition-informationcombined–information
statement-information:host-variable = DB2_LAST_ROW
DB2_SQL_ATTR_CURSOR_ROWSETNUMBERROW_COUNT
IBM Software Group | Information Management software
19
GET DIAGNOSTICS … Syntax (continued)
GET DIAGNOSTICS statement-information
condition-informationcombined–information
condition-information:CONDITION host-variable or integer
host-variable = DB2_RETURNED_SQLCODEDB2_ROW_NUMBERDB2_SQLERRD1 – 6DB2_ORDINAL_TOKEN_nDB2_TOKEN_COUNT
IBM Software Group | Information Management software
20
GET DIAGNOSTICS … Syntax (continued)
GET DIAGNOSTICS statement-information
condition-informationcombined–information
combined-information:host-variable = ALL STATEMENT
CONDITION blank or host-variable or integer
IBM Software Group | Information Management software
21
GET DIAGNOSTICS … ExamplesTo determine how many rows were updated in an UPDATE statement
GET DIAGNOSTICS :rcount = ROW_COUNT;
To handle multiple SQL errors during a NOT ATOMIC multi-row INSERTGET DIAGNOSTICS :numerrors = NUMBER;
– Then code a loop to execute the following for the number of errorsGET DIAGNOSTICS CONDITION :i :retstate = DB2_RETURNED_SQLCODE
To see all diagnostic information for an SQL statementGET DIAGNOSTICS :diags = ALL STATEMENT
Sample output in :diagsNumber=1; Returned_SQLSTATE=02000; DB2_RETURNED_SQLCODE=+100;– Would continue for all applicable items and for all conditions– Items are delimited by semicolons
IBM Software Group | Information Management software
22
GET DIAGNOSTICS … Examples (continued)
Assume a multi-row INSERT of 10 rows is performed on table T1 with 2 columns (C1/smallint & C2/integer ). All but 2 rows are inserted, cause values are invalid.
INSERT INTO T1 (C1, C2) FOR 10 ROWS VALUES (:hva1:hvind1, :hva2:hvind2) NOT ATOMIC CONTINUE ON SQLEXCEPTION;
After execution of the INSERT statement, SQLCA shows:SQLCODE = 0SQLSTATE = 0SQLERRD3 = 8
IBM Software Group | Information Management software
23
GET DIAGNOSTICS … Examples (continued)
SQLCA shows only 8 rows inserted. Using the following GET DIAGNOSTICS statements the following can be determined:
GET DIAGNOSTICS :num_rows = ROW_COUNT, :num_cond = NUMBER;
Would result in NUM_ROW = 8 and NUM_COND = 2 (2 conditions)
GET DIAGNOSTICS CONDITION 1 :sqlstate = RETURNED_SQLSTATE,:sqlcode = DB2_RETURNED_SQLCODE, :row_num = DB2_ROW_NUMBER;
Could result in SQLSTATE = 22003, SQLCODE = -302, and ROW_NUM = 4
GET DIAGNOSTICS CONDITION 2 :sqlstate = RETURNED_SQLSTATE,:sqlcode = DB2_RETURNED_SQLCODE, :row_num = DB2_ROW_NUMBER;
Could result in SQLSTATE = 22003, SQLCODE = -302, and ROW_NUM = 8
IBM Software Group | Information Management software
24
Common table expression (CTE)
Are introduced by the WITH keyword at the beginning of a query.
Multiple CTEs can be coded, and are separated by a comma.
Can be reference elsewhere in the query, including by other CTEs.
Help simplify the coding of a complex query that could otherwise need nested table expressions.
Avoids the need to create and maintain views.
Like a view, columns names for the CTE are specified in parentheses after the CTE name.
No AS clause required for calculated columns in the SELECT.
Are materialized if referenced more than once.
IBM Software Group | Information Management software
25
Common table expression (CTE) … ExampleWITH
E AS (SELECT EMPNO, LASTNAME, SALARY, SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9‘ AS HIREDECADEFROM EMPLOYEE),
M (HIREDECADE, MINIMUM_SALARY) AS (SELECT HIREDECADE, MIN(SALARY)FROM EGROUP BY HIREDECADE)
SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE,E.SALARY, M.MINIMUM_SALARYFROM E INNER JOIN MON E.HIREDECADE = M.HIREDECADE
IBM Software Group | Information Management software
26
Sequence objects
Stand-alone objects and are not tied to a table like an identity column.
Unlike an identity column which has a one to one relationship to a table, a sequence can be used for many tables or many sequences can be used in a table.
Avoids the concurrency and performance when applications generate their own sequence numbers.
Allows multiple transactions to concurrently access and increment number, and still guarantee each will be unique, without having to wait for one transaction to commit before allowing another.
IBM Software Group | Information Management software
27
Sequence objects (continued)
Defined using SQL DDL statements and attributes are similar to those for identity columns.
Data type for values generated can be smallint, integer and decimal with a zero scale.
All attributes, except the data type, can be altered at any time via the ALTER SEQUENCE statement, as needed.
Value retrieved via the NEXT VALUE FOR or PREVIOUS VALUE FOR expressions. A ROLLBACK has no effect on the value retrieved.
IBM Software Group | Information Management software
28
Sequence objects … NEXT and PREVIOUS VALUE
Applications refer to the sequence object by its name.– NEXT VALUE FOR sequence-name– PREVIOUS VALUE FOR sequence-name
Can be invoked during:– SELECT statement or SELECT INTO statement– INSERT statement within VALUES clause– INSERT statement within select-clause of fullselect– UPDATE statement with SET clause (except NEXT VALUE cannot be in
select-clause)– VALUES or VALUES INTO statement– CREATE PROCEDURE, FUNCTION TRIGGER– SET host-variable-assignment statement
IBM Software Group | Information Management software
29
Sequence objects … ExamplesAssume sequence created with START WITH 1, INCREMENT BY 1SELECT NEXT VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1; Generates Value of 1SELECT NEXT VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1; Generates Value of 2COMMIT;SELECT PREVIOUS VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1;
Returns most recently generated value (2)
Specified within SET clause of UPDATE statementUPDATE T1 SET C1 = (SELECT PREVIOUS VALUE FOR SQ1 FROM T);UPDATE T1 SET C1 = NEXT VALUE FOR SQ1;
Specified within VALUES clause of INSERT statementINSERT INTO T1 (COL1, COL2, COL3)VALUES ( NEXT VALUE FOR SQ1, NEXT VALUE FOR SQ2, :hval1 )
IBM Software Group | Information Management software
30
INSERT within SELECT
Provides the capability of retrieving column values inserted by DB2 such as:– Identity columns and sequence object values– User-defined defaults and expressions– Columns modified by BEFORE INSERT triggers– ROWIDs
Retrieve rows in the sequence as they were inserted with ORDER BY INPUT SEQUENCE
Benefits include:– Reduced network costs– Simplified procedural logic in stored procedures
IBM Software Group | Information Management software
31
INSERT within SELECT … Examples
Retrieve inserted sequence values:SELECT * FROM FINAL TABLE(INSERT INTO T1 (COL1, COL2, COL3)VALUES ( NEXT VALUE FOR SQ1, NEXT VALUE FOR SQ2, :hval1 ))
Retrieve rows in sequence they are inserted:DECLARE CS2 SCROLL CURSOR WITH ROWSET POSITIONING FORSELECT EMPNO FROM FINAL TABLE(INSERT INTO EMPLOYEE (NAME, TELE)FOR 3 ROWS VALUES(:HVA1, :HVA2))ORDER BY INPUT SEQUENCE
IBM Software Group | Information Management software
32
Materialized Query Table (MQT)
What is it?– Table containing materialized data derived, usually from complex
aggregated functions, from one or more source tables, which can be base tables, views, or table expressions.
– MQTs can be accessed directly or chosen by the optimzer (through automatic query rewrite) when base table or view is referenced.
– Two types:• System maintained using REFRESH TABLE statement• User maintained using functions like LOAD utility, INSERT, UPDATE,
DELETE and can use REFRESH TABLE statement.– Can be created from scratch or an existing tables can be registered
as an MQT.– Identified in SYSIBM.SYSTABLES as TYPE ‘M’.
IBM Software Group | Information Management software
33
Materialized Query Table (MQT) … CREATE Syntax
CREATE TABLE MQT1 AS (fullselect)
DATA INITIALLY DEFERRED
REFRESH DEFERRED
MAINTAINED BY SYSTEM/USER
ENABLE/DISABLE QUERY OPTIMIZATION
NOTE: ENABLE/DISABLE QUERY OPTIMIZATION controls exploitation of automatic query rewrite.
IBM Software Group | Information Management software
34
Materialized Query Table (MQT) … CREATE Example
CREATE TABLE MQT1 AS (SELECT T.PDATE, T.TRANSID,SUM(QTY * PRICE) AS TOTVAL,COUNT(QTY * PRICE) AS CNTFROM SCNDSTAR.TRANSITEM TI, SCNDSTAR.TRANS TWHERE TI.TRANSID = T.TRANSIDGROUP BY T.PDATE, T.TRANSID)
DATA INITIALLY DEFERREDREFRESH DEFERREDMAINTAINED BY SYSTEMENABLE QUERY OPTIMIZATIONIN MYDBMQT.MYTSMQT;
IBM Software Group | Information Management software
35
Materialized Query Table (MQT) … ALTER Syntax
ALTER syntax to register an existing table as an MQT:
ALTER TABLE T1 ADD MATERIALIZED QUERY (fullselect)DATA INITIALLY DEFERREDREFRESH DEFERREDMAINTAINED BY SYSTEM/USERENABLE/DISABLE QUERY OPTIMIZATION
IBM Software Group | Information Management software
36
Materialized Query Table (MQT) … ALTER Example
ALTER TABLE T1 ADD MATERIALIZED QUERY (SELECT T.PDATE,SUM(QTY * PRICE) AS TOTVAL,COUNT(QTY * PRICE) AS CNTFROM SCNDSTAR.TRANSITEM TI, SCNDSTAR.TRANS TWHERE TI.TRANSID = T.TRANSIDGROUP BY T.PDATE)
DATA INITIALLY DEFERREDREFRESH DEFERREDMAINTAINED BY USERENABLE QUERY OPTIMIZATION;
IBM Software Group | Information Management software
37
Materialized Query Table … Populate & Refresh
Ensure data currency meets user requirements.
Issuing REFRESH TABLE statement periodically:– Deletes all rows in MQT– Executes the fullselect– Inserts calculated data– Updates the catalog with refresh timestamp and cardinality– Executes in a single UOW– Consider logging and performance impact
For user-maintained, other methods like LOAD, Insert and Update, can be used.
IBM Software Group | Information Management software
38
Materialized Query Table … Automatic Query Rewrite
Two special registers control whether automatic query rewrite is enabled at the application level.– CURRENT REFRESH AGE = 0 or ANY
• 0 – No MQTs considered• ANY – All MQTs considered
– CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION =ALL/NONE/SYSTEM/USER• ALL – All MQTs considered• NONE – No MQTs considered• SYSTEM – Only system-maintained MQTs considered• USER – Only user-maintained MQTs considered
Two DSNZPARMs (REFSHAGE & MAINTYPE) provide defaults for above registers.
IBM Software Group | Information Management software
39
Materialized Query Table … Admin & Considerations
Information about MQTs is stored in SYSIBM.SYSVIEWS.
No primary keys or unique indexes or triggers are allowed for MQTs.
MQTs and their indexes are dropped if their associated base tables are dropped.
Design issue:– Few generic MQTs or more specialized MQTs
– Consider trade off between performance and maintenance.
IBM Software Group | Information Management software
40
Additional SQL Enhancements
GROUP BY expression:SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3)CONCAT '0 - 9' AS HIREDECADE,MIN(SALARY) AS MINIMUM_SALARYFROM EMPLOYEEGROUP BY SUBSTR(CHAR(HIREDATE,ISO),1,3)CONCAT '0 - 9‘
Multiple DISTINCT clauses on different columns:SELECT COUNT(DISTINCT C1), SUM(DISTINCT C2) FROM T1;
IBM Software Group | Information Management software
41
DocumentationDB2 V8 for z/OS Manuals:– http://www-1.ibm.com/support/docview.wss?rs=64&uid=swg27011659
Redbooks:– DB2 V8 for z/OS Everything You Ever Wanted to Know… and More
• http://publib-b.boulder.ibm.com/abstracts/sg246079.html?Open– DB2 V8 for z/OS Performance Topics
• http://publib-b.boulder.ibm.com/abstracts/sg246465.html?Open
DB2 for z/OS Library: (links to additional document for DB2 V8 for z/OS)
– http://www-306.ibm.com/software/sw- library/en_US/products/L374840M53366O19/
IBM Software Group | Information Management software
42
Questions???