®




TBRUG
Deep Dive into New DB2 V8
SQL and Features
February 2009

Frank Merino
Sr. IT Specialist
DB2 for z/OS Advisor ...
IBM Software Group | Information Management software


    Disclaimer and Trademarks
Information contained in this materia...
IBM Software Group | Information Management software


DB2 V8 LNL … Agenda

  Multi-row FETCH
  Multi-row INSERT
  GET DIA...
IBM Software Group | Information Management software


Multi-Row FETCH and INSERT

  What are they?
  – Multi-row FETCH:
 ...
IBM Software Group | Information Management software


Multi-row FETCH … Syntax

  DECLARE CURSOR Syntax:
  DECLARE cursor...
IBM Software Group | Information Management software


Multi-row FETCH … Syntax (continued)
  FETCH Syntax:
  FETCH NEXT R...
IBM Software Group | Information Management software


Multi-row FETCH … Notes

  Recommend always coding the FOR n ROWS c...
IBM Software Group | Information Management software


Multi-row FETCH … Examples
  Given cursor C1 is defined as:
    DEC...
IBM Software Group | Information Management software


Multi-row FETCH … Cursor Positioning




                          ...
IBM Software Group | Information Management software


Multi-row FETCH … Cursor Positioning - Partial Rowset




         ...
IBM Software Group | Information Management software


Multi-row FETCH … COBOL Example
  Declare cursor C1 and fetch 10 ro...
IBM Software Group | Information Management software


Multi-row FETCH … Positioned UPDATE

  Syntax:
  UPDATE table-name ...
IBM Software Group | Information Management software


Multi-row FETCH … Positioned DELETE

  Syntax:
  DELETE FROM table-...
IBM Software Group | Information Management software


Multi-row INSERT … Syntax
  INSERT Syntax:
  INSERT INTO table-name...
IBM Software Group | Information Management software


Multi-row INSERT … ATOMIC/NOT ATOMIC…

  ATOMIC (default):
  If any...
IBM Software Group | Information Management software


Multi-row INSERT … Examples

   Insert 10 rows of data into table w...
IBM Software Group | Information Management software


GET DIAGNOSTICS

 Enables applications to retrieve diagnostic infor...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Syntax

GET DIAGNOSTICS statement-information
   ...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Syntax (continued)

 GET DIAGNOSTICS statement-in...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Syntax (continued)

 GET DIAGNOSTICS statement-in...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Examples
 To determine how many rows were updated...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Examples (continued)

 Assume a multi-row INSERT ...
IBM Software Group | Information Management software


GET DIAGNOSTICS … Examples (continued)
 SQLCA shows only 8 rows ins...
IBM Software Group | Information Management software


Common table expression (CTE)

  Are introduced by the WITH keyword...
IBM Software Group | Information Management software


Common table expression (CTE) … Example
 WITH
  E AS (
    SELECT E...
IBM Software Group | Information Management software


Sequence objects

  Stand-alone objects and are not tied to a table...
IBM Software Group | Information Management software


Sequence objects (continued)

  Defined using SQL DDL statements an...
IBM Software Group | Information Management software


Sequence objects … NEXT and PREVIOUS VALUE

  Applications refer to...
IBM Software Group | Information Management software


Sequence objects … Examples
  Assume sequence created with START WI...
IBM Software Group | Information Management software


INSERT within SELECT

  Provides the capability of retrieving colum...
IBM Software Group | Information Management software


INSERT within SELECT … Examples

  Retrieve inserted sequence value...
IBM Software Group | Information Management software


Materialized Query Table (MQT)

  What is it?
  – Table containing ...
IBM Software Group | Information Management software


Materialized Query Table (MQT) … CREATE Syntax

 CREATE TABLE MQT1 ...
IBM Software Group | Information Management software


Materialized Query Table (MQT) … CREATE Example

    CREATE TABLE M...
IBM Software Group | Information Management software


Materialized Query Table (MQT) … ALTER Syntax

  ALTER syntax to re...
IBM Software Group | Information Management software


Materialized Query Table (MQT) … ALTER Example

    ALTER TABLE T1 ...
IBM Software Group | Information Management software


Materialized Query Table … Populate & Refresh

  Ensure data curren...
IBM Software Group | Information Management software


Materialized Query Table … Automatic Query Rewrite

  Two special r...
IBM Software Group | Information Management software


Materialized Query Table … Admin & Considerations

  Information ab...
IBM Software Group | Information Management software


Additional SQL Enhancements

  GROUP BY expression:
   SELECT SUBST...
IBM Software Group | Information Management software


Documentation
 DB2 V8 for z/OS Manuals:
 – http://www-1.ibm.com/sup...
IBM Software Group | Information Management software




   Questions???


                                               ...
Upcoming SlideShare
Loading in...5
×

TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009

566

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
566
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009

  1. 1. ® TBRUG Deep Dive into New DB2 V8 SQL and Features February 2009 Frank Merino Sr. IT Specialist DB2 for z/OS Advisor © 2008 IBM Corporation fmerino@us.ibm.com
  2. 2. IBM Software Group | Information Management software Disclaimer and Trademarks 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 2
  3. 3. IBM Software Group | Information Management software 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 3
  4. 4. IBM Software Group | Information Management software 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 4
  5. 5. IBM Software Group | Information Management software Multi-row FETCH … Syntax DECLARE CURSOR Syntax: DECLARE cursor-name CURSOR WITH ROWSET POSITIONING FOR 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 5
  6. 6. IBM Software Group | Information Management software Multi-row FETCH … Syntax (continued) FETCH Syntax: FETCH NEXT ROWSET PRIOR ROWSET FIRST ROWSET LAST ROWSET CURRENT ROWSET ROWSET STARTING AT ABSOLUTE host-variable or integer-value RELATIVE FROM cursor-name FOR host-variable ROWS INTO host-variable-array integer-value NOTE: The rowset-positioned clauses specify cursor positioning like the existing row-positioned clauses NEXT, PRIOR, FIRST, LAST, CURRENT, ABSOLUTE and RELATIVE. 6
  7. 7. IBM Software Group | Information Management software 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 number specified. 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. 7
  8. 8. IBM Software Group | Information Management software 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 8
  9. 9. IBM Software Group | Information Management software Multi-row FETCH … Cursor Positioning 9
  10. 10. IBM Software Group | Information Management software Multi-row FETCH … Cursor Positioning - Partial Rowset 10
  11. 11. IBM Software Group | Information Management software Multi-row FETCH … COBOL Example Declare 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 FOR SELECT NAME, SERIAL# FROM EMPLOYEE END-EXEC. EXEC SQL OPEN C1 END-EXEC. EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :NAME:INDSTRUC1,:SERIAL-NUMBER:INDSTRUC2 END-EXEC. 11
  12. 12. IBM Software Group | Information Management software Multi-row FETCH … Positioned UPDATE Syntax: UPDATE table-name SET assignment-clause WHERE CURRENT OF cursor-name FOR ROW host-variable OF ROWSET integer-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 12
  13. 13. IBM Software Group | Information Management software Multi-row FETCH … Positioned DELETE Syntax: DELETE FROM table-name WHERE CURRENT OF cursor-name FOR ROW host-variable OF ROWSET integer-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 13
  14. 14. IBM Software Group | Information Management software 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 14
  15. 15. IBM Software Group | Information Management software 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 15
  16. 16. IBM Software Group | Information Management software Multi-row INSERT … Examples Insert 10 rows of data into table with 2 columns: INSERT INTO T1 (C1, C2) FOR 10 ROWS VALUES (: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 ROWS VALUES (: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. 16
  17. 17. IBM Software Group | Information Management software 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. 17
  18. 18. IBM Software Group | Information Management software GET DIAGNOSTICS … Syntax GET DIAGNOSTICS statement-information condition-information combined–information statement-information: host-variable = DB2_LAST_ROW DB2_SQL_ATTR_CURSOR_ROWSET NUMBER ROW_COUNT 18
  19. 19. IBM Software Group | Information Management software GET DIAGNOSTICS … Syntax (continued) GET DIAGNOSTICS statement-information condition-information combined–information condition-information: CONDITION host-variable or integer host-variable = DB2_RETURNED_SQLCODE DB2_ROW_NUMBER DB2_SQLERRD1 – 6 DB2_ORDINAL_TOKEN_n DB2_TOKEN_COUNT 19
  20. 20. IBM Software Group | Information Management software GET DIAGNOSTICS … Syntax (continued) GET DIAGNOSTICS statement-information condition-information combined–information combined-information: host-variable = ALL STATEMENT CONDITION blank or host-variable or integer 20
  21. 21. IBM Software Group | Information Management software GET DIAGNOSTICS … Examples To 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 INSERT GET DIAGNOSTICS :numerrors = NUMBER; – Then code a loop to execute the following for the number of errors GET DIAGNOSTICS CONDITION :i :retstate = DB2_RETURNED_SQLCODE To see all diagnostic information for an SQL statement GET DIAGNOSTICS :diags = ALL STATEMENT Sample output in :diags Number=1; Returned_SQLSTATE=02000; DB2_RETURNED_SQLCODE=+100; – Would continue for all applicable items and for all conditions – Items are delimited by semicolons 21
  22. 22. IBM Software Group | Information Management software 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 = 0 SQLSTATE = 0 SQLERRD3 = 8 22
  23. 23. IBM Software Group | Information Management software 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 23
  24. 24. IBM Software Group | Information Management software 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. 24
  25. 25. IBM Software Group | Information Management software Common table expression (CTE) … Example WITH E AS ( SELECT EMPNO, LASTNAME, SALARY, SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9‘ AS HIREDECADE FROM EMPLOYEE ), M (HIREDECADE, MINIMUM_SALARY) AS ( SELECT HIREDECADE, MIN(SALARY) FROM E GROUP BY HIREDECADE ) SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARY FROM E INNER JOIN M ON E.HIREDECADE = M.HIREDECADE 25
  26. 26. IBM Software Group | Information Management software 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. 26
  27. 27. IBM Software Group | Information Management software 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. 27
  28. 28. IBM Software Group | Information Management software 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 28
  29. 29. IBM Software Group | Information Management software Sequence objects … Examples Assume sequence created with START WITH 1, INCREMENT BY 1 SELECT NEXT VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1; Generates Value of 1 SELECT NEXT VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1; Generates Value of 2 COMMIT; SELECT PREVIOUS VALUE FOR SQ1 FROM SYSIBM.SYSDUMMY1; Returns most recently generated value (2) Specified within SET clause of UPDATE statement UPDATE T1 SET C1 = (SELECT PREVIOUS VALUE FOR SQ1 FROM T); UPDATE T1 SET C1 = NEXT VALUE FOR SQ1; Specified within VALUES clause of INSERT statement INSERT INTO T1 (COL1, COL2, COL3) VALUES ( NEXT VALUE FOR SQ1, NEXT VALUE FOR SQ2, :hval1 ) 29
  30. 30. IBM Software Group | Information Management software 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 30
  31. 31. IBM Software Group | Information Management software 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 FOR SELECT EMPNO FROM FINAL TABLE (INSERT INTO EMPLOYEE (NAME, TELE) FOR 3 ROWS VALUES(:HVA1, :HVA2)) ORDER BY INPUT SEQUENCE 31
  32. 32. IBM Software Group | Information Management software 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’. 32
  33. 33. IBM Software Group | Information Management software 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. 33
  34. 34. IBM Software Group | Information Management software Materialized Query Table (MQT) … CREATE Example CREATE TABLE MQT1 AS ( SELECT T.PDATE, T.TRANSID, SUM(QTY * PRICE) AS TOTVAL, COUNT(QTY * PRICE) AS CNT FROM SCNDSTAR.TRANSITEM TI, SCNDSTAR.TRANS T WHERE TI.TRANSID = T.TRANSID GROUP BY T.PDATE, T.TRANSID) DATA INITIALLY DEFERRED REFRESH DEFERRED MAINTAINED BY SYSTEM ENABLE QUERY OPTIMIZATION IN MYDBMQT.MYTSMQT; 34
  35. 35. IBM Software Group | Information Management software 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 DEFERRED REFRESH DEFERRED MAINTAINED BY SYSTEM/USER ENABLE/DISABLE QUERY OPTIMIZATION 35
  36. 36. IBM Software Group | Information Management software Materialized Query Table (MQT) … ALTER Example ALTER TABLE T1 ADD MATERIALIZED QUERY ( SELECT T.PDATE, SUM(QTY * PRICE) AS TOTVAL, COUNT(QTY * PRICE) AS CNT FROM SCNDSTAR.TRANSITEM TI, SCNDSTAR.TRANS T WHERE TI.TRANSID = T.TRANSID GROUP BY T.PDATE) DATA INITIALLY DEFERRED REFRESH DEFERRED MAINTAINED BY USER ENABLE QUERY OPTIMIZATION; 36
  37. 37. IBM Software Group | Information Management software 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. 37
  38. 38. IBM Software Group | Information Management software 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. 38
  39. 39. IBM Software Group | Information Management software 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. 39
  40. 40. IBM Software Group | Information Management software Additional SQL Enhancements GROUP BY expression: SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9' AS HIREDECADE, MIN(SALARY) AS MINIMUM_SALARY FROM EMPLOYEE GROUP 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; 40
  41. 41. IBM Software Group | Information Management software Documentation DB2 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/ 41
  42. 42. IBM Software Group | Information Management software Questions??? 42
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×