0
Advanced Technical Support (ATS), Americas




New Cool SQL Enhancements in DB2
for z/OS Version 8
CDUG – July 20, 2006

 ...
Advanced Technical Support (ATS), Americas




What's new in V8?
   Scalar fullselect
   Multiple DISTINCT clauses
   Dyna...
Advanced Technical Support (ATS), Americas




Scalar Fullselect




                                                     ...
Advanced Technical Support (ATS), Americas




Scalar fullselect

   What is it? .....
    –A  scalar fullselect is a full...
Advanced Technical Support (ATS), Americas




Scalar fullselect -- tables used in examples

          PARTS              ...
Advanced Technical Support (ATS), Americas




Scalar fullselects in a WHERE clause
 Find which products have the prices o...
Advanced Technical Support (ATS), Americas




Nested scalar fullselects in a SELECT list
    Find the cost of inventory f...
Advanced Technical Support (ATS), Americas



Scalar fullselects -- in CASE expression of
UPDATE
  Give discount to the pa...
Advanced Technical Support (ATS), Americas




Scalar fullselect -- restrictions
   Scalar fullselect not supported in...
...
Advanced Technical Support (ATS), Americas




Multiple DISTINCT Clauses




                                             ...
Advanced Technical Support (ATS), Americas




Multiple DISTINCT clauses

   What is it? .....
    –Allows
          more ...
Advanced Technical Support (ATS), Americas




Multiple DISTINCT clauses

   Prior to Version 8 . . .
     –SELECT   DISTI...
Advanced Technical Support (ATS), Americas




Dynamic Scrollable Cursors




                                            ...
Advanced Technical Support (ATS), Americas




Dynamic scrollable cursors

   What is it? .....
    –Dynamic     scrollabl...
Advanced Technical Support (ATS), Americas



Static scrollable cursors -- Version 7 review

    Cursors can be scrolled
 ...
Advanced Technical Support (ATS), Americas




Sensitive and insensitive Static cursors - V7 review
     DECLARE C1 INSENS...
Advanced Technical Support (ATS), Americas




STATIC SCROLL Cursors
           Base Table                              Re...
Advanced Technical Support (ATS), Americas

 What does the TEMP ROW look like?


                 SELECT C1, C2, C1+C2, LO...
Advanced Technical Support (ATS), Americas



Dynamic scrollable cursors -- V8 new function
    Scrollable cursor that pro...
Advanced Technical Support (ATS), Americas



New DECLARE CURSOR statement attributes
 SENSITIVE DYNAMIC
  –Specifies     ...
Implications on FETCH with Dynamic Scroll
      Advanced Technical Support (ATS), Americas

   INSENSITIVE not allowed wit...
Advanced Technical Support (ATS), Americas




DECLARE CURSOR Syntax



 >>-DECLARE-cursor-name---+-----------------------...
Advanced Technical Support (ATS), Americas




• INSENSITIVE is used for static scrollable cursors only. Keep
  in mind th...
Advanced Technical Support (ATS), Americas

FETCH Orientation Syntax

                                       +---NEXT-----...
Advanced Technical Support (ATS), Americas




Cursor position and scrolling

   At OPEN CURSOR, cursor is positioned befo...
Advanced Technical Support (ATS), Americas



 Fetch Orientation and Cursor Position
    Result table                     ...
Advanced Technical Support (ATS), Americas



Cursor Positioning: Mixing Row and Rowset
Positioned Fetches - Multi Row Fet...
Advanced Technical Support (ATS), Americas


Rowset Positioned Fetches
                                                   ...
Advanced Technical Support (ATS), Americas



 Partial Rowsets (Result Sets)




                                         ...
Advanced Technical Support (ATS), Americas



Determining rowset size
    If FOR n ROWS is NOT specified and cursor is dec...
Advanced Technical Support (ATS), Americas




Fetching beyond the result set


   If you try to fetch beyond the result s...
Advanced Technical Support (ATS), Americas



Partial Result Sets
If you fetch beyond the end of the result set, you will
...
Advanced Technical Support (ATS), Americas



Fetching Beyond the Result Set
ABSOLUTE or RELATIVE
If you fetch beyond the ...
Advanced Technical Support (ATS), Americas



   What's my position with Dynamic Scroll?

When you get a +100 fetching for...
Advanced Technical Support (ATS), Americas

    Resulting Cursor Positions
   Fetch       Current         Current      Cur...
Advanced Technical Support (ATS), Americas


Locking with dynamic scrollable cursors
  ISO CS recommended for maximum conc...
Advanced Technical Support (ATS), Americas

                        Serialization Preview
                        Use exis...
OPTIMISTIC LOCKING Americas
     Advanced Technical Support (ATS),



     We release locks after fetch
    Because we are...
Advanced Technical Support (ATS), Americas



Updating via scrollable cursors


  Optimistic locking is used by static scr...
Updating Using Static Scrollable Cursors
           Advanced Technical Support (ATS), Americas

 Positioned Updates & Dele...
Some Advanced TechnicalScrollable Cursor considerations
     Dynamic Support (ATS), Americas
    Dynamic scrollable cursor...
Advanced Technical Support (ATS), Americas


Cursor Type Comparison
       Cursor Type         Result Table        Visibil...
Advanced Technical Support (ATS), Americas

Static Scroll vs. Dynamic Scroll
                                    User tabl...
Advanced Technical Support (ATS), Americas

The HOLE Concept
  In STATIC SCROLL Cursors the number of rows in the result
 ...
Advanced Technical Support (ATS), Americas



          Removing an Update Hole
1                         EXEC SQL OPEN C4...
Advanced Technical Support (ATS), Americas


Savepoints and Rollback

     Declare Cursor CUR1
       SENSITIVE STATIC SCR...
Advanced Technical Support (ATS), Americas

Insensitive scrolling and holes
           Returns the row(s) from the result ...
Update and delete holes for sensitive static
     Advanced Technical Support (ATS), Americas


 Updates the fetched row in...
Advanced Technical Support (ATS), Americas



Considerations using static scrollable cursors - multi
row operations

    W...
Advanced Technical Support (ATS), Americas


Using Multi-Row FETCH with Scrollable Cursors
    As holes may occur, ensure ...
Advanced Technical Support (ATS), Americas


Considerations with dynamic scrollable cursors -
multi row operations
   Star...
Retrieve Data Backward with a Cursor
             Advanced Technical Support (ATS), Americas


* Declare a cursor to retri...
Advanced Technical Support (ATS), Americas




Considerations for positioned update/delete
  It is possible for another ap...
Advanced Technical Support (ATS), Americas



Cursor Related SQLCODEs

  +222 - Update/delete hole detected
  +231- Cursor...
Advanced Technical Support (ATS), Americas

  Determining Attributes of a Cursor By Checking the
  SQLCA - for V8 consider...
Advanced Technical Support (ATS), Americas


    Determining Attributes of a Cursor By Using the GET
    DIAGNOSTICS State...
Advanced Technical Support (ATS), Americas


Example - How would you Improve this?

                                  * TA...
Advanced Technical Support (ATS), Americas


Example - Alternative

  Sample application                   * TABLE TXN ( T...
Advanced Technical Support (ATS), Americas




       ZPARM Considerations (V8)
• MAX_NUM_CUR (MAX OPEN CURSORS)
 – Number...
Advanced Technical Support (ATS), Americas




Limit Result Table Size


   Scrollable Cursors support FETCH FIRST n ROWS
...
Advanced Technical Support (ATS), Americas




REXX SUPPORT


  REXX to DB2 interface has changes for Scrollable
  Cursors...
Advanced Technical Support (ATS), Americas


DRDA considerations
   DB2 Linux, UNIX, and Windows V8.1 clients support
   d...
Advanced Technical Support (ATS), Americas

Intersection with Other New V8 Functions
  Insert within SELECT
    –Adynamic ...
Advanced Technical Support (ATS), Americas


Index Manager Backward Scan Support
 Backward Scan in Index Manager is enable...
Advanced Technical Support (ATS), Americas



Backward index scan enabled
   In V8, DB2 selects an ascending index and can...
Advanced Technical Support (ATS), Americas


 Avoid Sort by using Backward Index Scan with
 ORDER BY in V8
               ...
Advanced Technical Support (ATS), Americas

IFCID Updates and SQL CODES in V8
  IFCID 65 for OPEN CURSOR
    –adding   inf...
Basic Advanced Technical Support Application Design
       Decisions for (ATS), Americas
   What Result Types?
    –STATIC...
Advanced Technical Support (ATS), Americas




Performance
                                 based on:


DB2 UDB for z/OS V...
Advanced Technical Support (ATS), Americas




Hardware and software environment
All the test cases use the following envi...
Advanced Technical Support (ATS), Americas




       Measurement methodology
• One table defined in one segmented table s...
Advanced Technical Support (ATS), Americas




                Measurement cases
• Read cursor (single row fetch) - Static...
Advanced Technical Support (ATS), Americas
 Test scenario 1: Read cursor - Static vs. dynamic
 This test uses two types of...
Advanced Technical Support (ATS), Americas


The second program declares an ASENSITIVE SCROLLABLE cursor, which
is the def...
Advanced Technical Support (ATS), Americas




                                       TEST 1
• The measurements have been ...
Test 1 - Read cursor (single row fetch) - Static vs. dynamic
       Advanced Technical Support (ATS), Americas




       ...
Advanced Technical Support (ATS), Americas



           Test scenario 2:
   Update cursor - Static vs. dynamic


• This c...
Advanced Technical Support (ATS), Americas
DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH HOLD
         FOR SELECT
       ...
Advanced Technical Support (ATS), Americas




                                        Test 2
• As in the previous test ca...
Advanced Technical Support (ATS), Americas



Test 2 - Update cursor (single row fetch and update) - Static vs. dynamic


...
Test 3 - Multi-row FETCH using static or dynamic cursors –
           Advanced Technical Support (ATS), Americas
         ...
Test 3 - Multi-row FETCH using static or dynamic cursors –
           Advanced Technical Support (ATS), Americas
         ...
Advanced Technical Support (ATS), Americas



     Test 4 - Single row vs. multi-row FETCH &
                UPDATE or DEL...
Advanced Technical Support (ATS), Americas
DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR...
Test 4 - Single row vs. multi-row FETCH & UPDATE or DELETE
      Advanced Technical Support (ATS), Americas
              ...
Advanced Technical Support (ATS), Americas


Performance Considerations – Page 1 of 2
    Choosing the right Cursor Type
 ...
Advanced Technical Support (ATS), Americas


Performance Considerations – Page 2 of 2
The summary of the recommendations f...
Cursor Attributes on(ATS), Americas
     Advanced Technical Support PREPARE Statement


 EXEC SQL DECLARE mycursor
       ...
Advanced Technical Support (ATS), Americas


ODBC Scrollable Cursors Usage
   DefineCursor Type
    –Determine          wh...
Advanced Technical Support (ATS), Americas


ODBC and Scrollable Cursors

   SQLFetchScroll(fetch orientation, offset)
   ...
Advanced Technical Support (ATS), Americas


JDBC Scrollable Cursors Usage
  Method to get type of Result Set (which is li...
Advanced Technical Support (ATS), Americas



JDBC Scrollable Cursors Example

  Connection con = DriverManager.getConnect...
Advanced Technical Support (ATS), Americas




Multi-row FETCH and INSERT




                                            ...
Advanced Technical Support (ATS), Americas


Multi-Row FETCH and INSERT
What is it? .....
 –Multi-row     FETCH:
     A si...
Advanced Technical Support (ATS), Americas


Rowsets
  A ROWSET is a group of rows from the result table of a query,
  whi...
Advanced Technical Support (ATS), Americas




DECLARE CURSOR




                                                © 2004 I...
Advanced Technical Support (ATS), Americas



FETCH examples
EXAMPLE 1:
Fetch the previous rowset and have the cursor posi...
Advanced Technical Support (ATS), Americas



DECLARE CURSOR - Example
Declare C1 as the cursor of a query to retrieve a r...
Advanced Technical Support (ATS), Americas



Distributed Multiple Row Fetch Flow
 Application             Client         ...
Advanced Technical Support (ATS), Americas




Local FETCH Flow
 x1000     x2000                     Internal Buffers   ‘N...
Advanced Technical Support (ATS), Americas



  MRF Performance Considerations
• Up to 50% faster FETCH performance
   Per...
Advanced Technical Support (ATS), Americas


Multiple Row INSERT
Different Forms of INSERT
  ƒINSERT via VALUES is used to...
Advanced Technical Support (ATS), Americas



Host Variable Arrays
Host variable array is an array in which each element o...
Advanced Technical Support (ATS), Americas




COBOL




                                               © 2004 IBM Corpora...
Advanced Technical Support (ATS), Americas




                                      COBOL
Example 1: Declare a CURSOR C1 ...
Advanced Technical Support (ATS), Americas




PL/I




                                                © 2004 IBM Corpora...
Advanced Technical Support (ATS), Americas



 PL/1
Example 2: You can retrieve 10 rows from the table
CORPDATA.DEPARTMENT...
Advanced Technical Support (ATS), Americas




C/C++




                                                © 2004 IBM Corpor...
Advanced Technical Support (ATS), Americas




C++
Example 3: Declare an integer and varying character array to hold
colum...
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
New Cool SQL Enhancements in DB2 for z/OS Version 8
Upcoming SlideShare
Loading in...5
×

New Cool SQL Enhancements in DB2 for z/OS Version 8

1,033

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
1,033
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "New Cool SQL Enhancements in DB2 for z/OS Version 8"

  1. 1. Advanced Technical Support (ATS), Americas New Cool SQL Enhancements in DB2 for z/OS Version 8 CDUG – July 20, 2006 John Iczkovits iczkovit@us.ibm.com © 2004 IBM Corporation
  2. 2. Advanced Technical Support (ATS), Americas What's new in V8? Scalar fullselect Multiple DISTINCT clauses Dynamic scrollable cursors Multi-row FETCH and INSERT GET DIAGNOSTICS statement INSERT within SELECT statement Sequence objects Identity column enhancements CURRENT PACKAGE PATH special register Common table expressions and recursive SQL DB2 And maybe more.... DB2 DB2 © 2004 IBM Corporation
  3. 3. Advanced Technical Support (ATS), Americas Scalar Fullselect © 2004 IBM Corporation
  4. 4. Advanced Technical Support (ATS), Americas Scalar fullselect What is it? ..... –A scalar fullselect is a fullselect, enclosed in parentheses, that returns a single value –Scalar fullselect can be used in an expression –Example: SELECT PRODUCT, PRICE FROM PRODUCTS WHERE PRICE <= 0.7 * (SELECT AVG(PRICE) FROM PRODUCTS); Benefits ..... –Enhances usability and power of SQL –Facilitates portability –Conforms with SQL standards © 2004 IBM Corporation
  5. 5. Advanced Technical Support (ATS), Americas Scalar fullselect -- tables used in examples PARTS PARTPRICE PART PROD# SUPPLIER PART _ PROD# SUPPLIER PRICE WIRE 10 ACWF WIRE 10 ACWF 3.50 OIL 160 WESTERN_CHEM OIL 160 WESTERN_CHEM 1.50 MAGNETS 10 BATEMAN MAGNETS 10 BATEMAN 59.50 PLASTIC 30 PLASTIC_CORP PLASTIC 30 PLASTIC_CORP 2.00 BLADES 205 ACE_STEEL BLADES 205 ACE_STEEL 8.90 PRODUCTS INVENTORY PROD# PRODUCT PRICE PART PROD# SUPPLIER ONHAND# 505 SCREWDRIVER 3.70 WIRE 10 ACWF 8 30 RELAY 7.55 OIL 160 WESTERN_CHEM 25 205 SAW 18.90 MAGNETS 10 BATEMAN 3 10 GENERATOR 45.75 PLASTIC 30 PLASTIC_CORP 5 BLADES 205 ACE_STEEL 10 © 2004 IBM Corporation
  6. 6. Advanced Technical Support (ATS), Americas Scalar fullselects in a WHERE clause Find which products have the prices of at least twice the lowest price of all the products. SELECT PRODUCT, PRICE FROM PRODUCTS A WHERE PRICE >= 2 * (SELECT MIN(PRICE) FROM PRODUCTS); PRODUCT PRICE RELAY 7.55 SAW 18.90 GENERATOR 45.75 © 2004 IBM Corporation
  7. 7. Advanced Technical Support (ATS), Americas Nested scalar fullselects in a SELECT list Find the cost of inventory for each product by calculating the sum of (price * onhand#) for each part in the product. SELECT PRODUCT, (SELECT COALESCE(SUM(X.COST),0) AS INV_COST FROM (SELECT ( (SELECT PRICE FROM PARTPRICE P WHERE P.PART = B.PART) *(SELECT ONHAND# FROM INVENTORY I WHERE I.PART =B.PART) ) AS COST FROM PARTS B WHERE B.PROD# = A.PROD# PRODUCT INV_COST ) X(COST) SCREWDRIVER .00 ) RELAY 10.00 FROM PRODUCTS A; SAW 89.00 GENERATOR 206.50 © 2004 IBM Corporation
  8. 8. Advanced Technical Support (ATS), Americas Scalar fullselects -- in CASE expression of UPDATE Give discount to the parts that have the large inventory and raise price on the parts that have the small inventory. CREATE TABLE NEW_PARTPRICE LIKE PARTPRICE; INSERT INTO NEW_PARTPRICE SELECT * FROM PARTPRICE; UPDATE NEW_PARTPRICE N SET PRICE = CASE WHEN( (SELECT ONHAND# FROM INVENTORY WHERE PART=N.PART) < 7) THEN 1.1 * PRICE WHEN( (SELECT ONHAND# FROM INVENTORY WHERE PART=N.PART) > 20) THEN .8 * PRICE PART PROD# SUPPLIER PRICE ELSE PRICE WIRE 10 ACWF 3.50 END; OIL 160 WESTERN_CHEM 1.20 MAGNETS 10 BATEMAN 65.45 SELECT * FROM NEW_PARTPRICE; PLASTIC 30 PLASTIC_CORP 2.20 BLADES 205 ACE_STEEL 8.90 © 2004 IBM Corporation
  9. 9. Advanced Technical Support (ATS), Americas Scalar fullselect -- restrictions Scalar fullselect not supported in... a CHECK constraint a grouping expression a view that has a WITH CHECK OPTION a CREATE FUNCTION (SQL scalar) a column function ORDER BY clause join-condition of the ON clause for INNER and OUTER JOINS © 2004 IBM Corporation
  10. 10. Advanced Technical Support (ATS), Americas Multiple DISTINCT Clauses © 2004 IBM Corporation
  11. 11. Advanced Technical Support (ATS), Americas Multiple DISTINCT clauses What is it? ..... –Allows more than one DISTINCT keyword on the SELECT or HAVING clause for a query –DB2 can now evaluate standard deviation & variance column functions Benefits ..... –Enhances usability and power of SQL –DB2 family compatibility © 2004 IBM Corporation
  12. 12. Advanced Technical Support (ATS), Americas Multiple DISTINCT clauses Prior to Version 8 . . . –SELECT DISTINCT C1, C2 FROM T1; –SELECT COUNT(DISTINCT C1) FROM T1; –SELECT C1, COUNT(DISTINCT C2) FROM T1 GROUP BY C1; –SELECT COUNT(DISTINCT(C1)),SUM(DISTINCT C1) FROM T1; -- same col With Version 8 . . . –SELECT DISTINCT COUNT(DISTINCT C1), SUM(DISTINCT C2) FROM T1; –SELECT COUNT(DISTINCT C1), AVG(DISTINCT C2) FROM T1 GROUP BY C1; –SELECT SUM(DISTINCT C1), COUNT(DISTINCT C1), AVG(DISTINCT C2) FROM T1 GROUP BY C1 HAVING SUM(DISTINCT C1) = 1; Not supported in Version 8 . . . –SELECT COUNT(DISTINCT A1,A2) FROM T1 GROUP BY A2; –SELECT COUNT(DISTINCT(A1,A2)) FROM T1 GROUP BY A2; © 2004 IBM Corporation
  13. 13. Advanced Technical Support (ATS), Americas Dynamic Scrollable Cursors © 2004 IBM Corporation
  14. 14. Advanced Technical Support (ATS), Americas Dynamic scrollable cursors What is it? ..... –Dynamic scrollable cursor: Scrolls directly on base table (TS scan, IX scan & DPSI) No result table materialized at any time Immediate visibility of committed updates, inserts, deletes Positioned updates and deletes allowed when possible Avoid sort by using backward index scan for ORDER BY Benefits ..... –Enhances usability and power of SQL –Facilitates portability –Conforms to SQL standards –Performance improved by sort elimination –Elimination of work file (temporary table) © 2004 IBM Corporation
  15. 15. Advanced Technical Support (ATS), Americas Static scrollable cursors -- Version 7 review Cursors can be scrolled –backwards –forwards –to an absolute position –to a position relative to the current cursor FETCH CURSOR... –before/after position Result table in TEMP DB Rows refreshed on demand © 2004 IBM Corporation
  16. 16. Advanced Technical Support (ATS), Americas Sensitive and insensitive Static cursors - V7 review DECLARE C1 INSENSITIVE DECLARE C1 SENSITIVE DECLARE C1 SENSITIVE SCROLL.. STATIC SCROLL.. STATIC SCROLL.. OPEN C1 ... OPEN C1 ... OPEN C1 ... FETCH INSENSITIVE... FETCH INSENSITIVE... FETCH SENSITIVE... PROGRAM PROGRAM PROGRAM TEMP TABLE TEMP TABLE TEMP TABLE BASE TABLE BASE TABLE BASE TABLE ƒRead only cursor ƒUpdatable cursor ƒUpdatable cursor ƒNot aware of updates or ƒAware of own updates or ƒAware of own updates and deletes in base table deletes within cursor deletes within cursor ƒOther changes to base ƒsees all committed table not visible to cursor updates and deletes ƒAll inserts not recognized ƒAll inserts not recognized Order by, table join and aggregate functions will force READ ONLY © 2004 IBM Corporation
  17. 17. Advanced Technical Support (ATS), Americas STATIC SCROLL Cursors Base Table Result Table DB2 Table DB2 Declared Temp Table SCROLL Base Table & Result Table are kept in sync as each Exclusive access by agent row is fetched if sensitive. Insensitive Fixed number of rows reads off the DTT – no need Goes away at Close Cursor to fetch updated or Requires declared TEMP database and Accessed by many deleted data. predefined table spaces © 2004 IBM Corporation
  18. 18. Advanced Technical Support (ATS), Americas What does the TEMP ROW look like? SELECT C1, C2, C1+C2, LOB1 WHERE C3 < 5 Insensitive cursor temp table row 16 byte C1 C2 C1+C2 Lob1 Absolute 120 byte number Length Length Max lob of C1 of C2 Length(C1,C descriptor 2) Sensitive cursor temp table row 16 byte 6 byte 5 byte C1 C2 C1+C2 Lob1 Absolute control Rid 120 byte number information Length Length Max lob of C1 of C2 Length(C1,C descriptor 2) © 2004 IBM Corporation
  19. 19. Advanced Technical Support (ATS), Americas Dynamic scrollable cursors -- V8 new function Scrollable cursor that provides access to the base table rather than a DTT (Declared Temporary Table) -- allows visibility of updates and inserts done by you or other users Defaults to single row FETCH, so DDF applications should use: –Multi-row FETCH –Positioned UPDATE/DELETE for multi-row FETCH DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR FOR SELECT C1, C2 FROM T1; © 2004 IBM Corporation
  20. 20. Advanced Technical Support (ATS), Americas New DECLARE CURSOR statement attributes SENSITIVE DYNAMIC –Specifies that size of result table is not fixed at OPEN cursor time –Cursor has complete visibility to changes All committed inserts, updates, deletes by other application processes All positioned updates and deletes within cursor All inserts, updates, deletes by same application processes, but outside cursor –FETCH executed against base table since no temporary result table created ASENSITIVE –DB2 determines sensitivity of cursor –If read-only... It behaves as an insensitive cursor Cursor is INSENSITIVE if SELECT statement does not allow it to be SENSITIVE (UNION, UNION ALL, FOR FETCH ONLY, FOR READ ONLY) –If not read only, SENSITIVE DYNAMIC is used for maximum sensitivity –Mainlyfor client applications that do not care whether or not the server supports the sensitivity or scrollability © 2004 IBM Corporation
  21. 21. Implications on FETCH with Dynamic Scroll Advanced Technical Support (ATS), Americas INSENSITIVE not allowed with FETCH statement (SQLCODE -244) if –Theassociated cursor is declared as SENSITIVE DYNAMIC SCROLL –The cursor is declared ASENSITIVE and DB2 chooses the maximum allowable sensitivity of SENSITIVE DYNAMIC SCROLL There are no "holes" (almost) as there is no temporary result table –Specialcase: If FETCH CURRENT or FETCH RELATIVE +0 requested but row on which cursor is positioned was deleted or updated so that it no longer meets selection criteria (SQLCODE +231) which says "hole" detected –Forexample, can occur with ISOLATION(CS) and CURRENTDATA(NO) Inserts by the application itself are immediately visible -- inserts by others are visible after commit Order is always maintained –Ifcurrent row is updated, the cursor is positioned before the next row of the original location and there is no current row –Ifcurrent row deleted, the cursor is positioned before the next row of the original location and there is no current row © 2004 IBM Corporation
  22. 22. Advanced Technical Support (ATS), Americas DECLARE CURSOR Syntax >>-DECLARE-cursor-name---+--------------------------------------------------------------+----> | | | |-ASENSITIVE-------------------| | +---|-INSENSITIVE-------------------|----SCROLL--+ |-SENSITIVE---STATIC--------| |-DYNAMIC-| >-----CURSOR--------+-----------------------+-----FOR---+-select-statement---+--------->< +-WITH HOLD-----+ +-statement-name---+ +-WITH RETURN-+ © 2004 IBM Corporation
  23. 23. Advanced Technical Support (ATS), Americas • INSENSITIVE is used for static scrollable cursors only. Keep in mind that for static scrollable cursors you can have a combination of SENSATIVE and INSENSATIVE for the DECLARE and FETCH statements. Make sure you know which one is being discussed on which statement. • SENSITIVE can be used for either static or dynamic scrollable cursors. Which one are you dealing with? • Be careful when using ASENSITIVE, you may not get what you want. • Isolation levels and CURRENTDATA values matter, mostly because of the ‘holes’ issue. Make sure you are getting the data you want. © 2004 IBM Corporation
  24. 24. Advanced Technical Support (ATS), Americas FETCH Orientation Syntax +---NEXT------------------------------------------+ >>-FETCH----+--------------------+--+-----------------------------------------------------+---------> +-INSENSITIVE-+ |---PRIOR------------------------------------------| +-SENSITIVE----+ |---FIRST-------------------------------------------| |---LAST--------------------------------------------| |---CURRENT-------------------------------------| |---BEFORE---------------------------------------| |---AFTER------------------------------------------| |---ABSOLUTE--+-host-variable-------+---| | +-integer-constant--+ | |---RELATIVE----+-host-variable-------+---| +-integer-constant--+ +-FROM-+ >---+------------+----cursor-name-------+--------------------------------+------------------>< +--single-fetch-clause---+ single-fetch-clause: +---,----------------------+ |--+-----INTO---v---host-variable-----+-------------------+------------------------------------| +----INTO DESCRIPTOR----descriptor-name-----+ © 2004 IBM Corporation
  25. 25. Advanced Technical Support (ATS), Americas Cursor position and scrolling At OPEN CURSOR, cursor is positioned before first row After FETCH, fetched row becomes current row and cursor positioned on current row •When FETCH reaches end of file, cursor positioned after last row if scroll number positive and before first row if scroll number negative •Ability to scroll backwards and forwards through result set which is ever changing –Notethat scroll quantity counts new inserts and has no way of counting deleted rows –Usage example: airline reservation or credit card processing © 2004 IBM Corporation
  26. 26. Advanced Technical Support (ATS), Americas Fetch Orientation and Cursor Position Result table FETCH BEFORE 1 Independent of FETCH FIRST 2 start 3 cursor position 4 5 FETCH ABSOLUTE 5 6 7 FETCH RELATIVE - 3 8 Current FETCH PRIOR Ending position 9 Cursor 10 XXXX XXXX XXXX XXXX FETCH CURRENT depends on start Position 11 cursor position FETCH or FETCH NEXT 12 13 FETCH RELATIVE 3 14 15 FETCH ABSOLUTE - 5 16 17 Independent of 18 start 19 cursor position 20 FETCH LAST FETCH AFTER © 2004 IBM Corporation
  27. 27. Advanced Technical Support (ATS), Americas Cursor Positioning: Mixing Row and Rowset Positioned Fetches - Multi Row Fetch (V8) © 2004 IBM Corporation
  28. 28. Advanced Technical Support (ATS), Americas Rowset Positioned Fetches Result table CUST_NO CUST_TYP CUST_NAME FETCH FIRST ROWSET 1 P Ian FOR 3 ROWS 2 P Mark 3 P John 4 P Karen FETCH NEXT ROWSET 5 P Sarah 6 M Florence 7 M Dylan 8 M Bert FETCH ROWSET STARTING 9 M Jo 10 R Karen AT ABSOLUTE 8 11 R Gary 12 R Bill FETCH NEXT ROWSET 13 R Geoff FOR 10 ROWS 14 R Julia 15 R Sally Partial Result Set © 2004 IBM Corporation
  29. 29. Advanced Technical Support (ATS), Americas Partial Rowsets (Result Sets) © 2004 IBM Corporation
  30. 30. Advanced Technical Support (ATS), Americas Determining rowset size If FOR n ROWS is NOT specified and cursor is declared for rowset positioning.. Size of rowset will be the same as the previous rowset fetch as long as –It was the previous fetch for this cursor –Or the previous fetch was a FETCH BEFORE or FETCH AFTER and the fetch before that was a rowset fetch Else rowset is 1 FETCH FIRST ROWSET FOR 5 ROWS Returns 5 rows FETCH NEXT ROWSET Returns the next 5 rows FETCH NEXT Returns a single row FETCH NEXT ROWSET Returns a single row FETCH NEXT ROWSET FOR 3 ROWS Returns 3 rows FETCH BEFORE Returns 0 rows FETCH NEXT ROWSET Returns 3 rows © 2004 IBM Corporation
  31. 31. Advanced Technical Support (ATS), Americas Fetching beyond the result set If you try to fetch beyond the result set you will receive end of data condition –i.e., When there are only 5 rows left in result table and you request FETCH NEXT ROWSET FOR 10 ROWS, 5 rows will be returned with an SQLCODE +100 This includes where FETCH FIRST n ROWS ONLY has been specified © 2004 IBM Corporation
  32. 32. Advanced Technical Support (ATS), Americas Partial Result Sets If you fetch beyond the end of the result set, you will receive an end of data condition ƒi.e., When there are only 5 rows left in result table and you request FETCH NEXT ROWSET FOR 10 ROWS, 5 rows will be returned - SQLCODE +100 ƒSQLERRD(3) will contain the number or rows returned ƒThis includes where FETCH FIRST n ROWS ONLY has been specified If you fetch beyond the beginning of the result set, you will receive an end of data condition ƒ i.e., if you are positioned on rows 3,4,5,6, and 7, and you request FETCH PRIOR ROWSET FOR 10 ROWS, 2 rows will be returned (Rows 1 and 2) - SQLCODE +20237 ƒSQLERRD(3) will contain the number or rows returned © 2004 IBM Corporation
  33. 33. Advanced Technical Support (ATS), Americas Fetching Beyond the Result Set ABSOLUTE or RELATIVE If you fetch beyond the end of the result set, or beyond the beginning of the result set, you will receive an end of data condition ƒAssume you are positioned on row 5 in a result set with 10 rows. ƒFETCH ROWSET STARTING AT ABSOLUTE 15 ƒFETCH ROWSET STARTING AT RELATIVE -7 ƒNo rows will be returned - SQLCODE +100 ƒSQLERRD(3) will contain 0 ƒCursor position will be either “BEFORE” or “AFTER” depending on the direction of the FETCH. © 2004 IBM Corporation
  34. 34. Advanced Technical Support (ATS), Americas What's my position with Dynamic Scroll? When you get a +100 fetching forward Application 1 Application 2 Cursor fetch doing searched updates –Position is AFTER CS, currentdata(no) Subsequent +100 –FETCH PRIOR will give you the last row, or –FETCH NEXT will give you +100 again Pos Delete When you get a +100 fetching backward Fetch –Position is BEFORE Current Subsequent +231 –FETCH NEXT will give you the first row, or –FETCH PRIOR will give you +100 When you get a +231 (row not found fetch current ) –Position is before the next row FETCH NEXT will give you the next row FETCH PRIOR will give you the previous row FETCH CURRENT will give you +231 +100 © 2004 IBM Corporation
  35. 35. Advanced Technical Support (ATS), Americas Resulting Cursor Positions Fetch Current Current Current Current Resulting Resulting Resulting Resulting Orientation Position Position on Position on Position After Position on Position on Position Before Position After before first Last row Delete or First row Last Row first row last row normal Row row Update Hole NEXT OK OK +100 +100 +222 IF OK N/A IF +100 FROM LAST ROW PRIOR +100 +100 OK OK +222 IF OK IF+100 FROM N/A LAST ROW FIRST OK OK OK OK +222 IF OK N/A N/A LAST OK OK OK OK N/A IFOK N/A N/A BEFORE OK OK OK OK N/A N/A IF OK N/A AFTER OK OK OK OK +222 N/A N/A IF OK CURRENT +231 OK OK +231 +222 IF OK N/A N/A RELATIVE 0 ABSOLUTE OK OK OK OK +222 IF OK N/A IF +100 AND N +N OUT OF RANGE ABSOLUTE - OK OK OK OK +222 IF OK IF +100 AND N N/A N OUT OF RANGE RELATIVE OK OK +100 +100 +222 IF OK N/A IF +100 AND N +N OUT OF RANGE RELATIVE -N +100 +100 OK OK +222 IF OK IF +100 AND N/A N OUT OF RANGE © 2004 IBM Corporation
  36. 36. Advanced Technical Support (ATS), Americas Locking with dynamic scrollable cursors ISO CS recommended for maximum concurrency –RR and RS severely restrict update of table by other users, therefore defeating purpose of SENSITIVE DYNAMIC Lock is held on current row as base table is always accessed directly - beware of multi row operations! As before, if ISO UR is on BIND option and SELECT statement contains FOR UPDATE OF, ISO promoted to CS As normal, if cursor defined WITH HOLD, locks will be held until first commit after close of cursor unless DSNZPARM SPRMRCHL = NO, in which case locks held for held cursors released at COMMIT © 2004 IBM Corporation
  37. 37. Advanced Technical Support (ATS), Americas Serialization Preview Use existing locking mechanisms, added one RR - lock every page/row as it is read RS - lock every row that qualifies stage 1 predicate Review CS - lock each row fetched(currentdata(YES)) - lock only if cursor FOR UPDATE OF if (currentdata(no)) UR - No rows locked New! Optimistic Locking Mechanism (Static cursor only) Provides Maximum concurrency with isolation level of Cursor Stability No locks held between fetches Values Concurrency model (as opposed to the Lock Concurrency model which is pessimistic locking) © 2004 IBM Corporation
  38. 38. OPTIMISTIC LOCKING Americas Advanced Technical Support (ATS), We release locks after fetch Because we are optimistic that no positioned update or delete will occur Even if it does, we are optimistic that the pertinent values will not have changed So we compare by value under a new lock to ensure data integrity No SCROLL FETCH row 1 FETCH Update row 2 and dynamic Lock row 1 Unlock row 1 Cursor Lock row 2 Time Row 1 locked Row 2 locked No Locks No Locks Update row 2 FETCH row 1 FETCH Lock row 2 Static Lock row 1 Lock row 2 Reevaluate SCROLL Unlock row 1 Unlock row 2 Compare Cursor Update © 2004 IBM Corporation
  39. 39. Advanced Technical Support (ATS), Americas Updating via scrollable cursors Optimistic locking is used by static scrollable cursors –Static scrollable cursors hold no locks on base tables after OPEN CURSOR –Check at update time whether the row has not changed Optimistic locking is not used by dynamic scrollable cursors –Locks against the underlying table while fetching, similar to non-scrollable cursors Positioned UPDATE and DELETE always allowed if cursor not read-only and the page or row lock was acquired successfully © 2004 IBM Corporation
  40. 40. Updating Using Static Scrollable Cursors Advanced Technical Support (ATS), Americas Positioned Updates & Deletes based on position on Temporary Result Table DB2 locks corresponding Base Table row on a positioned update or delete and verifies that the row still satisfies the search condition DB2 then matches the old values to the current values, while the row is locked, and allows the update or delete only if the values match Update/Delete disallowed if either the row fails search condition or the values do not match If the row passes the above two conditions then DB2 does the following –For UPDATE Updates the Base Table Refetches from the Base Table to reevaluate the predicate Updates the Result Table –If search condition fails - marks as Update Hole –If search condition satisfied - updates it with latest values –For DELETE Deletes the Base Table Row Updates (deletes row from) the Result Table –If search condition fails - marks as delete Hole –If search condition satisfied – deletes row The next FETCH will see these updates © 2004 IBM Corporation
  41. 41. Some Advanced TechnicalScrollable Cursor considerations Dynamic Support (ATS), Americas Dynamic scrollable cursors are supported with stored procedures –SP itself can update via dynamic scrollable cursor but program calling SP is restricted from updating using allocated cursor Scalar functions and arithmetic expressions in SELECT list are reevaluated at every fetch Column functions (AVG, MIN, MAX, etc.) are calculated once at open cursor –Functions may not be meaningful because size of result set can change Use of non-deterministic function (built-in or UDF) in WHERE clause of select statement or statement name of scrollable cursor can cause misleading results –Result of function can vary from one FETCH to subsequent FETCH of same row Cursors requiring use of a workfile cannot be declared SENSITIVE DYNAMIC –e.g. - SELECT COL1, MAX(COL2), COUNT(*) FROM T1 GROUP BY COL1 ORDER BY 3 Changes to tables referenced in subqueries are not reflected Parallelism is not supported with dynamic scrollable cursors © 2004 IBM Corporation
  42. 42. Advanced Technical Support (ATS), Americas Cursor Type Comparison Cursor Type Result Table Visibility of Visibility of Updatability Own Changes Others' (*) Changes SCROLL not Fixed, workfile No No No Specified (In V6) SCROLL not No workfile, base No Yes Yes Specified table access (No fetch (In V6) current) INSENSITIVE Fixed, declared No No No SCROLL temp table (In V7) SENSITIVE STATIC Fixed, declared Yes Yes Yes SCROLL temp table (Inserts not (Not Inserts) (In V7) allowed) SENSITIVE No declared temp Yes Yes Yes DYNAMIC SCROLL table, base table New (In V8) access * A cursor can become read-only if the SELECT statement references more than one table, or contains a GROUP BY etc. (read-only by SQL) © 2004 IBM Corporation
  43. 43. Advanced Technical Support (ATS), Americas Static Scroll vs. Dynamic Scroll User table Dynamic scroll Static Result Cursor Position Table copy in Unique temp database Index Dynamic scroll Cursor Position Y OP Dynamic scroll C Static scroll Cursor Position Index Cursor Position NPSI Dynamic scroll Cursor Position DPSI Index © 2004 IBM Corporation
  44. 44. Advanced Technical Support (ATS), Americas The HOLE Concept In STATIC SCROLL Cursors the number of rows in the result table is fixed but deletes and updates to the underlying base table can create DELETE HOLES or UPDATE HOLES Holes are detected on a Sensitive FETCH –DELETE HOLE - Created when the corresponding base table row has been deleted Once a delete hole, always a delete hole, delete holes are not refetched - HOWEVER, You can remove a delete hole only by opening the scrollable cursor, setting a savepoint, executing a positioned DELETE statement with the scrollable cursor, and rolling back to the savepoint. –UPDATE HOLE - Created when the corresponding base table row has been modified such that the values of the rows do not qualify the row for the query any longer Every SENSITIVE FETCH reevaluates the row against the predicate, if the evaluation fails, the row is marked as an Update Hole and a SQLCODE is returned An Update Hole can turn into a row again on a subsequent sensitive fetch of an Update hole. Basically, the row is updated back to its original value therefore the hole no longer exists. © 2004 IBM Corporation
  45. 45. Advanced Technical Support (ATS), Americas Removing an Update Hole 1 EXEC SQL OPEN C4; 2 2 3 EXEC SQL 3 4 4 UPDATE A 5 Update 5 SET COL1=COL1+1; 6 hole EXEC SQL UPDATE A SET COL1=COL1-1; 1 2 3 Update 4 hole 5 disappears © 2004 IBM Corporation
  46. 46. Advanced Technical Support (ATS), Americas Savepoints and Rollback Declare Cursor CUR1 SENSITIVE STATIC SCROLL for SELECT... Set EXT Savepoint SVPT1; (1) Open CURSOR CUR1; Fetch from CUR1 into...; Update ....where current of CUR1; (2) Fetch from CUR1 into...; (3) Rollback to Savepoint SVPT1; The update (2) is rolled back Cursor stays open Cursor keeps position from fetch (3) © 2004 IBM Corporation
  47. 47. Advanced Technical Support (ATS), Americas Insensitive scrolling and holes Returns the row(s) from the result table as it is. DECLARE C1 INSENSITIVE SCROLL CURSOR FOR ACCOUNT ACCOUNT_NAME TYPE SELECT ACCOUNT, ACCOUNT_NAME FROM ACCOUNT ABC010 BIG PETROLEUM C WHERE TYPE = 'P' BWH450 HUTH & DAUGHTERS C FOR UPDATE OF ACCOUNT_NAME ZXY930 MIGHTY BEARS PLC C MNP230 MR P TENCH P OPEN C1 BMP291 BASEL FERRARI C XPM673 SCREAM SAVER PTY LTD C ULP231 MS S FLYNN P RID ACCOUNT ACCOUNT_NAME XPM961 MICHAL LINGERIE C A04 MNP230 MR P TENCH A07 ULP231 MS S FLYNN ACCOUNT table Cursor temporary table DELETE FROM ACCOUNT WHERE ACCOUNT = 'MNP230'; COMMIT; ACCOUNT ACCOUNT_NAME TYPE FETCH C1 INTO :hv_account, :hv_account_name ABC010 BIG PETROLEUM C BWH450 HUTH & DAUGHTERS C ZXY930 MIGHTY BEARS PLC C MNP230 MR P TENCH C ƒFETCH / FETCH ... ROWSET will not BMP291 BASEL FERRARI C recognize the delete XPM673 SCREAM SAVER PTY LTD C ƒhv_account will contain MNP230 ULP231 MS S FLYNN P XPM961 MICHAL LINGERIE C ƒhv_account_name will contain MR P ACCOUNT table TENCH ƒreturns 0 SQLCODE © 2004 IBM Corporation
  48. 48. Update and delete holes for sensitive static Advanced Technical Support (ATS), Americas Updates the fetched row in the result table from the corresponding row in the base table of the cursor’s SELECT statement and returns the current values. Thus, it reflects changes made outside this cursor. DECLARE C1 SENSITIVE STATIC SCROLL CURSOR ACCOUNT ACCOUNT_NAME TYPE FOR SELECT ACCOUNT, ACCOUNT_NAME ABC010 BIG PETROLEUM C FROM ACCOUNT BWH450 HUTH & DAUGHTERS C WHERE TYPE = 'P' ZXY930 MIGHTY BEARS PLC C FOR UPDATE OF ACCOUNT_NAME MNP230 MR P TENCH P BMP291 BASEL FERRARI C OPEN C1 XPM673 SCREAM SAVER PTY LTD C ULP231 MS S FLYNN P RID ACCOUNT ACCOUNT_NAME XPM961 MICHAL LINGERIE C A04 MNP230 MR P TENCH A07 ULP231 MS S FLYNN ACCOUNT table Cursor temporary table DELETE FROM ACCOUNT WHERE ACCOUNT = 'MNP230'; COMMIT; ACCOUNT ACCOUNT_NAME TYPE ABC010 BIG PETROLEUM C FETCH C1 INTO :hv_account, :hv_account_name BWH450 HUTH & DAUGHTERS C ZXY930 MIGHTY BEARS PLC C ƒFETCH / FETCH ... ROWSET positions MNP230 MR P TENCH C at first row in temp table BMP291 BASEL FERRARI C XPM673 SCREAM SAVER PTY LTD C ƒrow has been deleted from base table ULP231 MS S FLYNN P ƒDB2 flags that FETCH is positioned on XPM961 MICHAL LINGERIE C ACCOUNT table a delete hole by returning SQL code +222 ƒhost variables are not reset © 2004 IBM Corporation
  49. 49. Advanced Technical Support (ATS), Americas Considerations using static scrollable cursors - multi row operations When scrolling between rowsets: –With insensitive fetches, updates by the application itself could cause changes and holes –With sensitive fetches, updates by other applications could cause changes and holes –Forexample, FETCH PRIOR ROWSET may return update or delete holes in place of rows that were fetched before Row contents can change between fetches © 2004 IBM Corporation
  50. 50. Advanced Technical Support (ATS), Americas Using Multi-Row FETCH with Scrollable Cursors As holes may occur, ensure at least one indicator variable array is defined for a column –Even if no nullable columns exist add an indicator variable array for at least one column –Ifnullable columns exist all indicator variable arrays provided are updated if a hole is found New value of -3 indicates hole •SQLCODE +222 will also be returned CUSTNO NULLABLE_COL IND_VAR CUST_TYPE 1000 M P 2000 -1 B -3 4000 F P © 2004 IBM Corporation
  51. 51. Advanced Technical Support (ATS), Americas Considerations with dynamic scrollable cursors - multi row operations Starting point and contents of rowsets will change when scrolling back and forth Note that just after fetching the CURRENT ROWSET, other applications can insert rows in between the rows being returned as part of the rowset –Refetching current rowset would return different rows, unless ISOLATION(RR) FETCH PRIOR ROWSET will return the previous n rows that qualify from the start of the current cursor position –Therefore n rows will be returned as long as start of rowset is not reached © 2004 IBM Corporation
  52. 52. Retrieve Data Backward with a Cursor Advanced Technical Support (ATS), Americas * Declare a cursor to retrieve the data backward * ********************************************* * from the EMP table. The cursor has access to * * Fetch the previous row in the table. * * changes by other processes. * ********************************************* EXEC SQL EXEC SQL DECLARE THISEMP SENSITIVE STATIC FETCH SENSITIVE PRIOR FROM THISEMP SCROLL CURSOR FOR INTO :EMP-NUM, :NAME2, :DEPT, :JOB-NAME SELECT EMPNO, LASTNAME, END-EXEC. WORKDEPT, JOB ********************************************* FROM DSN8810.EMP * Check that the fetched row is not a hole * END-EXEC. * (SQLCODE +222). If not, print the contents. * * Open the cursor * ********************************************* EXEC SQL IF SQLCODE IS GREATER THAN OR EQUAL TO 0 OPEN THISEMP AND SQLCODE IS NOT EQUAL TO +100 END-EXEC. AND SQLCODE IS NOT EQUAL TO +222 * Indicate what action to take when all rows * THEN PERFORM PRINT-RESULTS. * in the result table have been fetched. * ... EXEC SQL * Branch back to fetch the previous row. * WHENEVER NOT FOUND GO TO ... CLOSE-THISEMP * Close the cursor * END-EXEC. CLOSE-THISEMP. * Position the cursor after the last row of the result* EXEC SQL * table * CLOSE THISEMP EXEC SQL END-EXEC. FETCH AFTER FROM THISEMP END-EXEC. © 2004 IBM Corporation
  53. 53. Advanced Technical Support (ATS), Americas Considerations for positioned update/delete It is possible for another application process to update or delete a row in the base table of the SELECT statement so that the specified row of the cursor no longer has a corresponding row in the base table –Ifthe cursor is non-scrollable, this could happen if the cursor is ISOLATION(CS) CURRENTDATA(NO) and lock avoidance succeeds –Ifthe cursor is static scrollable, this could happen, since the result set is materialized into a temporary table and all underlying locks are released (unless ISOLATION(RR) or ISOLATION(RS) –Ifthe cursor is dynamic scrollable, this could happen if the cursor is ISOLATION(CS) CURRENTDATA(NO) and lock avoidance succeeds. © 2004 IBM Corporation
  54. 54. Advanced Technical Support (ATS), Americas Cursor Related SQLCODEs +222 - Update/delete hole detected +231- Cursor position invalid -222 - Update or delete attempted against an update or delete hole -224 - Result table does not agree with base table -225 - FETCH statement incompatible with Cursor definition -228 - For update of clause specified for Read-only cursor -243 - Sensitive Cursor cannot be defined for specific SELECT stmt -244 - SENSITIVE specified on FETCH is not valid -5012 - Host variable not exact numeric with scale zero Existing SQLCODEs in new situations +100, -104, -151, -199, -508, -510, -628 © 2004 IBM Corporation
  55. 55. Advanced Technical Support (ATS), Americas Determining Attributes of a Cursor By Checking the SQLCA - for V8 consider GET DIAGNOSTICS After you open a cursor, you can determine the following attributes of the cursor by checking the following SQLWARN and SQLERRD fields of the SQLCA: SQLWARN1 Indicates whether the cursor is scrollable or non-scrollable. SQLWARN4 Indicates whether the cursor is insensitive (I), sensitive static (S), or sensitive dynamic (D). SQLWARN5 Indicates whether the cursor is read-only, readable and deletable, or readable, deletable, and updatable. SQLERRD(1) The number of rows in the result table of a cursor when the cursor position is after the last row (when SQLCODE is equal to +100). This field is not set for dynamic scrollable cursors. SQLERRD(2) The number of rows in the result table of a cursor when the cursor position is after the last row (when SQLCODE is equal to +100). This field is not set for dynamic scrollable cursors. SQLERRD(3) The number of rows in the result table of an INSERT when the SELECT statement of the cursor contains the INSERT statement. If the OPEN statement executes with no errors or warnings, DB2 does not set SQLWARN0 when it sets SQLWARN1, SQLWARN4, or SQLWARN5. See Appendix D of DB2 SQL Reference for specific information about fields in the SQLCA. © 2004 IBM Corporation
  56. 56. Advanced Technical Support (ATS), Americas Determining Attributes of a Cursor By Using the GET DIAGNOSTICS Statement After you open a cursor, you can determine the following attributes of the cursor by checking these GET DIAGNOSTICS items: DB2_SQL_ATTR_CURSOR_HOLD Indicates whether the cursor can be held open across commits (Y or N) DB2_SQL_ATTR_CURSOR_ROWSET Indicates whether the cursor can use rowset positioning (Y or N) DB2_SQL_ATTR_CURSOR_SCROLLABLE Indicates whether the cursor is scrollable (Y or N) DB2_SQL_ATTR_CURSOR_SENSITIVITY Indicates whether the cursor is asensitive, insensitive, or sensitive to changes that are made by other processes (A, I, or S) DB2_SQL_ATTR_CURSOR_TYPE Indicates whether the cursor is declared static (S for INSENSITIVE or SENSITIVE STATIC) or dynamic (D for SENSITIVE DYNAMIC) © 2004 IBM Corporation
  57. 57. Advanced Technical Support (ATS), Americas Example - How would you Improve this? * TABLE TXN ( TXNID CHAR(8), * TXNDATE CHAR(8) Sample application * DESC VARCHAR(8), * AMT INTEGER) program to DECLARE CURSOR CU1 display the last five SENSITIVE STATIC SCROLL FOR rows from a table SELECT TXNDATE, AMT WHERE TXNID = 'SMITH' ORDER BY TXNDATE; e.g. Last 5 transactions OPEN CURSOR CU1; from checking account FETCH LAST FROM CU1 INTO :HV1,:HV2 FETCH RELATIVE -4 FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen DO I = 1 TO 3 FETCH NEXT FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen END CLOSE CURSOR CU1 © 2004 IBM Corporation
  58. 58. Advanced Technical Support (ATS), Americas Example - Alternative Sample application * TABLE TXN ( TXNID CHAR(8), * TXNDATE CHAR(8) program to * DESC VARCHAR(8), display the last five * AMT INTEGER) rows from a table DECLARE CURSOR CU1 e.g. Last 5 transactions SENSITIVE STATIC SCROLL FOR from checking account SELECT TXNDATE, AMT WHERE TXNID = 'SMITH' ORDER BY TXNDATE; OPEN CURSOR CU1; FETCH INSENSITIVE ABSOLUTE -5 FROM CU1 INTO :HV1,:HV2 DO I = 1 TO 4 FETCH NEXT FROM CU1 INTO :HV1,:HV2 App logic to Output row to screen END CLOSE CURSOR CU1 © 2004 IBM Corporation
  59. 59. Advanced Technical Support (ATS), Americas ZPARM Considerations (V8) • MAX_NUM_CUR (MAX OPEN CURSORS) – Number of cursors, including allocated cursors, that are open at a given DB2 site per thread. – RDS will keep a total of currently open cursors. – If an application attempts to open a thread after the maximum is reached, the statement will fail. – In a data sharing group, this parameter has member scope. – Acceptable values: 0 to 99999 – Default (review default setting): 500 © 2004 IBM Corporation
  60. 60. Advanced Technical Support (ATS), Americas Limit Result Table Size Scrollable Cursors support FETCH FIRST n ROWS ONLY –New function to limit number of rows in the result table –Limits the result table to FIRST n ROWS © 2004 IBM Corporation
  61. 61. Advanced Technical Support (ATS), Americas REXX SUPPORT REXX to DB2 interface has changes for Scrollable Cursors V7 REXX to DB2 interface for V8 Dynamic Scrollable Cursors not implemented © 2004 IBM Corporation
  62. 62. Advanced Technical Support (ATS), Americas DRDA considerations DB2 Linux, UNIX, and Windows V8.1 clients support dynamic scrollable cursors with FP4 – Only via the ODBC interface – Only when calling DB2 for z/OS V8 Dynamic scrollable cursors do not support the DRDA limited block fetch protocol – Toachieve blocking, use multi-row fetch operations. This is currently supported between: DB2 for z/OS V8 systems You can use any DB2 application programming interface on the requesting mainframe which supports the use of multi-row in DB2 V8. DB2 ODBC distributed clients using dynamic scrollable cursors and DB2 for z/OS V8 © 2004 IBM Corporation
  63. 63. Advanced Technical Support (ATS), Americas Intersection with Other New V8 Functions Insert within SELECT –Adynamic cursor is NOT allowed with a SELECT statement containing INSERT, but STATIC cursor is allowed. E.g.: SELECT CHAR(MYROWID), MYIDENT into :HV1, HV2 FROM FINAL TABLE (INSERT INTO VEHICLE (COLOR , TYPE) VALUES (:HV3,:HV4)); Multi Row Fetch –Adynamic cursor can have a SELECT statement with a multi row fetch option. E.g.: DECLARE C1 SENSITIVE DYNAMIC CURSOR WITH ROWSET POSITIONING FOR SELECT * FROM EMP; FETCH ROWSET STARTING AT -3 FROM C1 FOR 3 ROWS INTO..... Data Partitioning Secondary Index(DPSI) –Dynamicscrolling is supported using a DPSI if selected by the optimizer. Nuance - Order may or may not be important. –Externally, DPSI is like any other index, however, DB2 has several LOC (Lines Of Code) to accommodate scrolling with DPSI, specially when the request includes an ORDER BY. © 2004 IBM Corporation
  64. 64. Advanced Technical Support (ATS), Americas Index Manager Backward Scan Support Backward Scan in Index Manager is enabled in V7 Used for performance improvement of Max and Min Functions –e.g.. When MutualFundsTable has an ascending index on RateOfReturn SELECT FundName, MAX(RateOfReturn) WHERE Sector = 'Technology' FROM MutualFundTable; The ascending index is scanned backwards starting from the end until the WHERE clause is satisfied. Eliminates need for descending indexes for Max function performance (or ascending indexes for MIN) •Backward Index Scan is used more for DYNAMIC SCROLL Cursors during index scan in V8 No indexes on static table. © 2004 IBM Corporation
  65. 65. Advanced Technical Support (ATS), Americas Backward index scan enabled In V8, DB2 selects an ascending index and can use a backward scan to avoid the sort for the descending order In V8, DB2 uses the descending index to avoid the sort and can scan the descending index backwards to provide the ascending order To be able to use an index for backward scan, –Index must be defined on the same columns as ORDER BY and –Ordering must be exactly opposite of what is requested in ORDER BY. –i.e., if index defined as DATE DESC, TIME ASC, can do: Forward scan for ORDER BY DATE DESC, TIME ASC Backward scan for ORDER BY DATE ASC, TIME DESC – But must sort for ORDER BY DATE ASC, TIME ASC ORDER BY DATE DESC, TIME DESC © 2004 IBM Corporation
  66. 66. Advanced Technical Support (ATS), Americas Avoid Sort by using Backward Index Scan with ORDER BY in V8 Backward SELECT STATUS_DATE, STATUS index scan FROM ACCT_STAT Same WHERE ACCT_NUM = :HV Index is ORDER BY STATUS_DATE DESC, STATUS_TIME DESC; used. Forward SELECT STATUS_DATE, STATUS index scan FROM ACCT_STAT WHERE ACCT_NUM = :HV For ORDER BY STATUS_DATE ASC, STATUS_TIME ASC; scrollable and non DB2 optimizer will select an ascending index to scrollable provide a descending sort order by traversing the cursors index backwards rather than do a sort Index on ACCT_STAT is ACCT_NUM, STATUS_DATE, STATUS_TIME © 2004 IBM Corporation
  67. 67. Advanced Technical Support (ATS), Americas IFCID Updates and SQL CODES in V8 IFCID 65 for OPEN CURSOR –adding information for Scrollability -scrollable/non-scrollable Sensitivity - sensitive/insensitive/not known Result Table Type -static/dynamic/not known IFCID 59 for FETCH –adding information for Sensitivity -sensitive/insensitive/not specified Fetch Orientation - first/last/before/after/next/previous/absolute/relative/unknown No new SQLCODES Updated SQLCODES: –+231 - Current position is not valid for fetch current Only possible with dynamic scrollable cursors with isolation level CS, CURRENTDATA(NO) when FETCH CURRENT is issued and the current row has been deleted or updated so that it does not meet the selection criterion –-244 - Keyword INSENSITIVE cannot be specified on the FETCH statement when fetching from a SENSITIVE DYNAMIC SCROLL cursor © 2004 IBM Corporation
  68. 68. Basic Advanced Technical Support Application Design Decisions for (ATS), Americas What Result Types? –STATIC - fixed membership in result table –DYNAMIC - result table membership changes as rows are inserted and deleted in the table Restricted with SELECT statement No ORDER BY with UPDATE No Joins, Functions, complex SQL What Sensitivity? –ASENSITIVE - you don't know what you'll get INSENSITIVE SENSITIVE DYNAMIC –INSENSITIVE - explicitly specified (membership is fixed) –SENSITIVE - explicitly specified (membership depends on STATIC or DYNAMIC specification) • What Isolation Level and Lock size? –CS with CURRENTDATA(YES) recommended if doing updates. –CS with CURRENTDATA(NO) if not doing updates –Row level locking will give max concurrency, however at a CPU cost, so be careful. –But all locking semantics supported © 2004 IBM Corporation
  69. 69. Advanced Technical Support (ATS), Americas Performance based on: DB2 UDB for z/OS Version 8 Performance Topics SG24-6465 © 2004 IBM Corporation
  70. 70. Advanced Technical Support (ATS), Americas Hardware and software environment All the test cases use the following environment: • Hardware: IBM z900 Series 2064-216 (z900 turbo) – One LPAR with 2 dedicated processors – 7 GB real storage – Dedicated ESS model 800 (ESS) – 4 dedicated FICON channels • Software – z/OS V1.4 – DB2 V8 – Non-data sharing • Measurement data collected with DB2 Performance Expert V2 – Accounting trace class 1,2,3 – Statistics trace class 1,3,4,5,6,8 © 2004 IBM Corporation
  71. 71. Advanced Technical Support (ATS), Americas Measurement methodology • One table defined in one segmented table space is used for all measurements. • All the measurements involved only table space scans (1 index is defined). • First time effect – Bring up the DB2 subsystem before each measurement to clear the data in the buffer pool, eliminating the inconsistency of different buffer hit ratios from run to run. • No concurrency issues – There is no other program accessing the table space during the measurements. • Read programs are bound with ISOLATION(CS) and CURRENT DATA(NO) • Update programs are bound with ISOLATION(RS) • Each program issues one commit after closing the cursor © 2004 IBM Corporation
  72. 72. Advanced Technical Support (ATS), Americas Measurement cases • Read cursor (single row fetch) - Static vs. dynamic • Update cursor (single row fetch and update) - Static vs. dynamic • Multi-row FETCH using static or dynamic cursors - Single row vs. multi-row • Multi-row FETCH and UPDATE or DELETE using static or dynamic cursors - Single row vs. multi-row © 2004 IBM Corporation
  73. 73. Advanced Technical Support (ATS), Americas Test scenario 1: Read cursor - Static vs. dynamic This test uses two types of FETCH programs. The intent is to compare insensitive with asensitive cursors for static and dynamic. The first program declares an insensitive scrollable cursor to retrieve data from the table. It means this program is strictly read-only (does not allow positioned update and delete). DECLARE C1 INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; DO I = 1 TO 50000; FETCH INSENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  74. 74. Advanced Technical Support (ATS), Americas The second program declares an ASENSITIVE SCROLLABLE cursor, which is the default in DECLARE CURSOR of V8. ASENSITIVE scrollable cursors allow DB2 to choose the maximum sensitivity of a cursor as possible, and in this case the following SELECT statement does not indicate read-only cursor (that is, a UNION or UNION ALL, ORDER BY, FOR READ ONLY, FOR FETCH ONLY) so DB2 determines dynamic as the maximum sensitivity. DECLARE C1 ASENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; DO I = 1 TO 50000; FETCH NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  75. 75. Advanced Technical Support (ATS), Americas TEST 1 • The measurements have been done varying the number of FETCHes and the number of qualified rows opened as a cursor. This is done to show how the performance is affected by different result set sizes (the size of the temporary table in the case of the static cursor examples). The summary of our test cases is: • Use ISO(CS) and CD(NO) as a bind parameter • For the read program these options provide maximum performance and concurrency. • 50k or 10 FETCHes • FETCH of 10 columns for a total of 50 bytes • Qualified 1 million or 100k rows opened as a cursor • In the program we specified 1,000,000 or 100,000 in • ‘ROWnnnnnnnnn’ row-id. • test results for static and dynamic cursors from the output of the DB2 PE trace record. © 2004 IBM Corporation
  76. 76. Test 1 - Read cursor (single row fetch) - Static vs. dynamic Advanced Technical Support (ATS), Americas 95% performance 99% performance 85% performance improvement over improvement over improvement over Static! Static! Static! Fetch of 50K and 10 rows take The amount of FETCHes Times are almost the same time. This is becomes the deciding factor much lower due to populating the temp for the dynamic cursor’s because we table with 1 million rows. performance, whereas the only Class 3 time is about the same, number of qualifying rows populate overhead for opening and was a deciding factor 100K rows. © 2004 IBM Corporation formatting the temp table. in the static cursor model.
  77. 77. Advanced Technical Support (ATS), Americas Test scenario 2: Update cursor - Static vs. dynamic • This case tests positioned updates and deletes. The intent is to compare sensitive cursors for static and dynamic. © 2004 IBM Corporation
  78. 78. Advanced Technical Support (ATS), Americas DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5,COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; DO I = 1 TO 1000; FETCH SENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; DO I = 1 TO 1000; FETCH SENSITIVE NEXT C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) © 2004 IBM Corporation
  79. 79. Advanced Technical Support (ATS), Americas Test 2 • As in the previous test case, we now examine UPDATEs and DELETEs with different numbers of qualified rows. The summary description of this test case is: • Use ISO(RS) as a bind parameter – This option protects data integrity (not for performance). • 100k or 10 FETCHes followed by 100k or 10 UPDATEs • 100k or 10 FETCHes followed by 100k or 10 DELETEs • FETCH of 6 columns for a total of 37 bytes • Qualified 1 M or 50k rows opened as a cursor © 2004 IBM Corporation
  80. 80. Advanced Technical Support (ATS), Americas Test 2 - Update cursor (single row fetch and update) - Static vs. dynamic NOTE – same type of results as in test 1! © 2004 IBM Corporation
  81. 81. Test 3 - Multi-row FETCH using static or dynamic cursors – Advanced Technical Support (ATS), Americas Single row vs. multi-row DECLARE C1 INSENSITIVE SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; FETCH INSENSITIVE FIRST ROWSET FROM C1 FOR 100 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; DO I = 1 TO 499; FETCH INSENSITIVE NEXT ROWSET C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; DECLARE C1 ASENSITIVE SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’; FETCH FIRST ROWSET FROM C1 FOR 100 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; DO I = 1 TO 499; FETCH NEXT ROWSET C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5, :COL6,:COL7,:COL8,:COL9,:COL10; END; © 2004 IBM Corporation
  82. 82. Test 3 - Multi-row FETCH using static or dynamic cursors – Advanced Technical Support (ATS), Americas Single row vs. multi-row Multi-row FETCH static No great difference Since the 1 million row temporary table is built row by row, regardless of the fetch implementation. Multi-row FETCH dynamic Reducing the number of trips between the application and database engine by a factor of 100 (50,000 fetches vs. 500 multi-fetches) accounts for the performance gains observed. © 2004 IBM Corporation
  83. 83. Advanced Technical Support (ATS), Americas Test 4 - Single row vs. multi-row FETCH & UPDATE or DELETE The summary of this test case is: •Use ISO (RS) as a bind parameter •These options protect data integrity (not for performance). •40 FETCHes followed by UPDATE on a rowset of 25 rows •40 FETCHes followed by DELETE on a rowset of 25 rows •Qualified 1 million rows at open cursors © 2004 IBM Corporation
  84. 84. Advanced Technical Support (ATS), Americas DECLARE C1 SENSITIVE STATIC SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5,COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; FETCH SENSITIVE FIRST ROWSET FROM C1 FOR 25 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6+10 WHERE CURRENT OF C1; DO I = 1 TO 39; FETCH SENSITIVE NEXT ROWSET FROM C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) DECLARE C1 SENSITIVE DYNAMIC SCROLL CURSOR WITH ROWSET POSITIONING WITH HOLD FOR SELECT COL1, COL2, COL3, COL4, COL5, COL6 FROM TABLE WHERE COL2 < ‘ROWNNNNNNNNN’ FOR UPDATE OF COL6; FETCH SENSITIVE FIRST ROWSET FROM C1 FOR 25 ROWS INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6+10 WHERE CURRENT OF C1; DO I = 1 TO 39; FETCH SENSITIVE NEXT ROWSET FROM C1 INTO :COL1,:COL2,:COL3,:COL4,:COL5,:COL6; UPDATE TABLE SET COL6 = :COL6 +10 WHERE CURRENT OF C1; END; (Same for Deletes) © 2004 IBM Corporation
  85. 85. Test 4 - Single row vs. multi-row FETCH & UPDATE or DELETE Advanced Technical Support (ATS), Americas Multi-row FETCH and UPDATE or DELETE static No significant changes in performance because of DTT build and population Multi-row FETCH and UPDATE or DELETE dynamic 17.8% performance improvement in DB2 class 2 CPU time (0.135 vs. 0.111) and a 7% performance improvement in DB2 class 2 elapsed time (0.589 vs. 0.547). © 2004 IBM Corporation
  86. 86. Advanced Technical Support (ATS), Americas Performance Considerations – Page 1 of 2 Choosing the right Cursor Type –If you just want to blast out your data, choose forward only cursors –If you want to maintain position, go back and forth - choose Scrollable Cursors –If you don't care about the freshness of data, choose INSENSITIVE CURSORS –Ifyou want fresh some times, choose SENSITIVE on DECLARE and SENSITIVE or INSENSITIVE on FETCH –If you want to see inserts and not deletes, choose DYNAMIC CURSORS –Ifyou only want to scroll only between the first few rows of the result set - use FIRST n ROWS –Use stored procedures to OPEN in distributed environment if update not required Choose the appropriate isolation level - usually CS (CURRENTDAT(NO)) –RR/RS provides low concurrency - may not be feasible –Take advantage of optimistic locking (static only) Provide sufficient declared TEMP database table space storage for STATIC and INSENSITIVE cursors Commit as often as practical, use CURSOR HOLD © 2004 IBM Corporation
  87. 87. Advanced Technical Support (ATS), Americas Performance Considerations – Page 2 of 2 The summary of the recommendations for static and dynamic cursors is: •Static scrollable cursors can be the better solution if: – You need to scroll backwards to obtain initial values – You do not care about constant changes (such as business intelligence or representative samples). - Optimistic locking if more concurrency is required. •Dynamic scrollable cursors can be preferable in situations where: – It is important for the application to access/see updated/inserted rows – Performance is important •Use multi-row operations whenever possible, particularly for the dynamic cursor model. © 2004 IBM Corporation
  88. 88. Cursor Attributes on(ATS), Americas Advanced Technical Support PREPARE Statement EXEC SQL DECLARE mycursor CURSOR FOR mystmt; Flexibility EXEC SQL PREPARE mystmt ATTRIBUTES :attrvar FROM :stmttxt; EXEC SQL DESCRIBE mystmt INTO :mysqlda; EXEC SQL OPEN mycursor; EXEC SQL FETCH FROM mycursor USING DESCRIPTOR :mysqlda ; Applications can pass a host variable containing a list of attributes, such as SCROLL, WITH HOLD, WITH RETURN, INSENSITIVE, SENSITIVE STATIC, optimize-clause, isolation-clause, etc. © 2004 IBM Corporation
  89. 89. Advanced Technical Support (ATS), Americas ODBC Scrollable Cursors Usage DefineCursor Type –Determine which cursor types supported by driver SQLGetInfo() SQL_CURSOR_SENSITIVITY SQL_SCROLL_OPTIONS SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1 SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2 SQL_KEYSET_CURSOR_ATTRIBUTES1 SQL_KEYSET_CURSOR_ATTRIBUTES2 SQL_STATIC_CURSOR_ATTRIBUTES1 SQL_STATIC_CURSOR_ATTRIBUTES2 SQL_DYNAMIC_CURSOR_ATTRIBUTES1 (Not in V7) SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2 (Not in V7) –Set Cursor Characteristic and Type e.g.. SQLSetStmtAttr SQL_ATTR_CURSOR_TYPE SQL_ATTR_CURSOR_STATIC Specify Rowset Size – e.g.. SQLSetStmtAttr SQL_ATTR_ROW_ARRAY_SIZE ROWSET_SIZE Fetch – e.g.. SQLFetchScroll SQL_FETCH_RELATIVE 5 © 2004 IBM Corporation
  90. 90. Advanced Technical Support (ATS), Americas ODBC and Scrollable Cursors SQLFetchScroll(fetch orientation, offset) SQL_FETCH_NEXT SQL_FETCH_PRIOR SQL_FETCH_FIRST SQL_FETCH_LAST SQL_FETCH_ABSOLUTE SQL_FETCH_RELATIVE SQL_FETCH_BOOKMARK SQLSetPos(rownumber, operation, locktype) SQL_POSITION SQL_REFRESH SQL_UPATE SQL_DELETE SQL_ADD SQLBulkOperations(operation) SQL_ADD SQL_UPDATE_BY_BOOKMARK SQL_DELETE SQL_FETCH © 2004 IBM Corporation
  91. 91. Advanced Technical Support (ATS), Americas JDBC Scrollable Cursors Usage Method to get type of Result Set (which is like a cursor) –int getType() TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE Methods for Positioning –boolean next() –boolean previous() –boolean first() –boolean last() –boolean absolute() –boolean relative() –void afterLast() –void beforeFirst() Methods for retrieving column values –string getString(String columnName) –int getInt(int columnIndex) –... © 2004 IBM Corporation
  92. 92. Advanced Technical Support (ATS), Americas JDBC Scrollable Cursors Example Connection con = DriverManager.getConnection( "jdbc:my_subprotocol:my_subname"); con.setTransactionIsolation(TRANSACTION_READ_COMMITTED); PreparedStatement pstmt = con.prepareStatement( "SELECT EMP_NO, SALARY FROM EMPLOYEES WHERE EMP_NO =?", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); pstmt.setFetchSize(25); pstmt.setString(1,"1000010"); boolean b = rs.beforeFirst(); ResultSet rs3 = pstmt.executeQuery(); © 2004 IBM Corporation
  93. 93. Advanced Technical Support (ATS), Americas Multi-row FETCH and INSERT © 2004 IBM Corporation
  94. 94. Advanced Technical Support (ATS), Americas Multi-Row FETCH and INSERT What is it? ..... –Multi-row FETCH: A single FETCH statement can retrieve multiple rows of data from the result table of a query as a rowset – A rowset is a group of rows of data that are grouped together and operated on as a set –Supports dynamic and static SQL (Fetch always static) –Multi-row INSERT: A single SQL statement can insert one or more rows into a table or view Multi-row INSERT can be implemented as either static or dynamic SQL Benefits ..... –Enhances usability and power of SQL –Performance is improved by eliminating multiple trips between application and database engine; for distributed access, reduced network traffic –Combined with scrollable cursors important for browse applications © 2004 IBM Corporation
  95. 95. Advanced Technical Support (ATS), Americas Rowsets A ROWSET is a group of rows from the result table of a query, which are returned by a single FETCH statement (or inserted by a single (multi-row) INSERT statement) •The program controls how many rows are returned in a rowset (it controls the size of the rowset) –Can be specified on the FETCH ... FOR n ROWS statement (n is the rowset size and can be up to 32767) Each group of rows are operated on as a rowset •Ability to intertwine single row and multiple row fetches for a multi-fetch cursor FETCH FIRST ROWSET STARTING AT ABSOLUTE 10 FROM CURS1 FOR 6 ROWS INTO :hva1, :hva2; © 2004 IBM Corporation
  96. 96. Advanced Technical Support (ATS), Americas DECLARE CURSOR © 2004 IBM Corporation
  97. 97. Advanced Technical Support (ATS), Americas FETCH examples EXAMPLE 1: Fetch the previous rowset and have the cursor positioned on that rowset EXEC SQL FETCH PRIOR ROWSET FROM C1 FOR 3 ROWS INTO... -- OR -- EXEC SQL FETCH ROWSET STARTING AT RELATIVE -3 FROM C1 FOR 3 ROWS INTO... EXAMPLE 2: Fetch 3 rows starting with row 20 regardless of the current position of the cursor EXEC SQL FETCH ROWSET STARTING AT ABSOLUTE 20 FROM C1 FOR 3 ROWS INTO... © 2004 IBM Corporation
  98. 98. Advanced Technical Support (ATS), Americas DECLARE CURSOR - Example Declare C1 as the cursor of a query to retrieve a rowset from the table DEPT. The prepared statement is MYCURSOR EXEC SQL DECLARE CURSOR C1 CURSOR WITH ROWSET POSITIONING FOR MYCURSOR; Rowset positioning specifies whether multiple rows of data can be accessed as a rowset on a single FETCH statement – default is WITHOUT ROWSET POSITIONING © 2004 IBM Corporation
  99. 99. Advanced Technical Support (ATS), Americas Distributed Multiple Row Fetch Flow Application Client Server Intermediate Table C1 C2 Com-Buffer Com-Buffer Buffer Page SQLCA Diagnostic Diagnostic Diagnostic Info Info Info Limited Block Fetch Flow Client Server Intermediate Table Application Com-Buffer Com-Buffer Buffer Page C1 C2 SQLCA Diagnostic Diag Info Info SQLCA Diag Info SQLCA © 2004 IBM Corporation
  100. 100. Advanced Technical Support (ATS), Americas Local FETCH Flow x1000 x2000 Internal Buffers ‘N’ Table 1204 0 … … 4 1204 1300 0 1300 1700 0 1700 1900 0 1900 SQLDAID = SQLDA SQLDABC = 104 SQLN = 2 SQLD = 2 SQLTYPE[1] = 496 SQLLEN[1] = 4 SQLDATA[1] = x00001000 SQLIND[1] = x00002000 SQLNAME[1] = x‘0008000000010004’ © 2004 IBM Corporation
  101. 101. Advanced Technical Support (ATS), Americas MRF Performance Considerations • Up to 50% faster FETCH performance Performance improvement largely due to savings of API costs Performance variable based on • Number of rows fetched • Number of columns fetched • Class 2 accounting (on or off) – savings larger if Class 2 is on • Examples – DSNTEP4 – 35% improvement on FETCH of 1000 rows with 5 cols and 20 cols – DSNTIAUL – 50% improvement on FETCH of 10000 rows with 5 cols and 20 cols • Up to 50% reduction in CPU cost for LBF –vs- V7 © 2004 IBM Corporation
  102. 102. Advanced Technical Support (ATS), Americas Multiple Row INSERT Different Forms of INSERT ƒINSERT via VALUES is used to insert a single row into the table or view using values provided or referenced ƒINSERT via SELECT is used to insert one or more rows into table or view using values from other tables or views ƒINSERT via VALUES… FOR "n" ROWS form is used to insert multiple rows into table or view using values provided in host variable array FOR "n" ROWS ƒFor static, specify FOR "n" ROWS on INSERT statement (for dynamic INSERT, you may also specify FOR "n" ROWS on EXECUTE statement) ƒMaximum value of n is 32767 specified as host-variable, parameter marker, or literal value ƒInput provided with literal, host variable, or host variable array -- each array represents cells for multiple rows of a single column VALUES… FOR “n” ROWS clause allows specification of multiple rows of data ƒHost variable arrays used to provide values for a column on INSERT ƒExample: VALUES (:hva1, :hva2) FOR 10 ROWS © 2004 IBM Corporation
  103. 103. Advanced Technical Support (ATS), Americas Host Variable Arrays Host variable array is an array in which each element of the array contains a value for the same column Changes have been made to allow host variable arrays in: ƒCOBOL ƒPL/1 ƒC++ ƒNOTE: Assembler support is limited to cases where USING DESCRIPTOR is allowed. Assembler pre-compiler does not recognize declaration of host variable arrays. The programmer is responsible for allocating storage correctly, etc. Can only be referenced in multi-row fetch or insert In general, arrays may not be arrays of structures © 2004 IBM Corporation
  104. 104. Advanced Technical Support (ATS), Americas COBOL © 2004 IBM Corporation
  105. 105. Advanced Technical Support (ATS), Americas COBOL Example 1: Declare a 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)COMP-4 SY C. 49 NAME-DATA PIC X(40). 05 SERIAL-NUMBER PIC S9(9)COMP-4 OCCURS 10 TIMES. PROCEDURE DIVISION. EXEC SQL DECLARE C1 CURSOR WITH ROWSET POSITIONING FOR SELECT NAME, SERIAL# FROM CORPORATE.EMPLOYEE END-EXEC. EXEC SQL OPEN C1 END-EXEC. EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :NAME, :SERIAL-NUMBER END-EXEC. © 2004 IBM Corporation
  106. 106. Advanced Technical Support (ATS), Americas PL/I © 2004 IBM Corporation
  107. 107. Advanced Technical Support (ATS), Americas PL/1 Example 2: You can retrieve 10 rows from the table CORPDATA.DEPARTMENT with: DCL DEPTNO(10) CHAR(3), DEPTNAME(10) CHAR(29) VAR, MGRNO(10) CHAR(6), ADMRDEPT(10) CHAR(3); DCL IND_ARRAY1(10)BIN FIXED(15); DCL IND_ARRAY2(10)BIN FIXED(15); ... EXEC SQL DECLARE C1 CURSOR WITH ROWSET POSITIONING FOR SELECT * FROM CORPDATA.DEPARTMENT; ... EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :DEPTNO :IND_ARRAY1, :DEPTNAME :IND_ARRAY2, :MGRNO :IND_ARRAY3, :ADMRDEPT :IND_ARRAY4; © 2004 IBM Corporation
  108. 108. Advanced Technical Support (ATS), Americas C/C++ © 2004 IBM Corporation
  109. 109. Advanced Technical Support (ATS), Americas C++ Example 3: Declare an integer and varying character array to hold columns retrieved from a multi-row fetch statement long serial_num(10); struct { short len; char data [18]; }name [10]; ... EXEC SQL DECLARE C1 CURSOR FOR SELECT NAME, SERIAL# FROM CORPDATA.EMPLOYEE WITH ROWSET POSITIONING; ... EXEC SQL OPEN C1; EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 10 ROWS INTO :NAME, :SERIAL_NUM; © 2004 IBM Corporation
  1. A particular slide catching your eye?

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

×