sql grouping functions

Upload: domingo-figueroa

Post on 07-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 SQL Grouping Functions

    1/50

    2010 IBM Corporation

    V6R1: SQL Grouping FunctionsMultidimensional Results at the click of a Query

    DB2 for IBM i

    Tom McKinley ([email protected])IBM Systems and Technology GroupLab Services

  • 8/6/2019 SQL Grouping Functions

    2/50

    2010 IBM Corporation

    IBM Power Systems

    2

    Whats in a name?

    Industry refers to Grouping Sets and Super Groups (Rollup and Cube)collectively as Grouping Sets

    Grouping Sets and Super Groups are sometimes referred to as Analytical SQL Grouping Functions

    For clarity, this presentation will use the term Grouping Functions when

    collectively describing Grouping Sets, Rollups, and Cubes

  • 8/6/2019 SQL Grouping Functions

    3/50

  • 8/6/2019 SQL Grouping Functions

    4/50 2010 IBM Corporation

    IBM Power Systems

    4

    Qtr1 Qtr4Qtr2 Qtr3

    Selection can be performed usingany method available, includingparallel enabled methods

    Hash Table Grouping

    Qtr, Units

    42007110Flange

    12005680Widget

    4200630Widget

    420053Dome3200514Flange

    320071Widget

    4200683Widget

    2200619Cog

    420067Widget

    Qtr Year UnitsPart

    Collect all common input values into acommon bucket (by hash value)

    The same input value willalways result in the samehash value

    Only relevantinformation is stored

    and the data is in noparticular order

    Input: Join or Group By value

    Output: Hash value

    Aggregate onunits for eachgroup of quarters

  • 8/6/2019 SQL Grouping Functions

    5/50 2010 IBM Corporation

    IBM Power Systems

    5

    Radix Index Grouping

    12005Widget

    32007Widget

    32005Flange

    42005Dome

    42007Flange

    22006Cog

    42006Widget

    42006Widget

    42006Widget

    Qtr Year Part

    RRNs

    42007110Flange

    12005680Widget4200630Widget

    420053Dome

    3200514Flange

    320071Widget

    4200683Widget

    2200619Cog

    420067Widget

    Qtr Year UnitsPart

    Given an index on Part, Year, Qtr,Apply local selection, and gather RRNs for each group of Quarters

    Use RRNs to access the tablerows to gather the unitsinformation

    Aggregate on units for eachgroup of quarters

  • 8/6/2019 SQL Grouping Functions

    6/50 2010 IBM Corporation

    IBM Power Systems

    6

    Grouping Functions Roadmap

    Grouping Sets and Super Groups Defined

    Grouping Functions Syntax and Examples

    Grouping Functions Performance Considerations and Examples

  • 8/6/2019 SQL Grouping Functions

    7/50

    2010 IBM Corporation

    IBM Power Systems

    7

    Grouping Sets and Super Groups Defined

  • 8/6/2019 SQL Grouping Functions

    8/50

    2010 IBM Corporation

    IBM Power Systems

    8

    Grouping Sets and Super Groups (ROLLUP and CUBE)

    Many BI applications involve hierarchical, multi-dimensional aggregate view of the data.

    Current group by support permits grouping/aggregation data along one dimension SELECT COUNTRY, REGION, STORE, PRODUCT, SUM(SALES),

    COUNT(*) FROM TRANS GROUP BY COUNTRY, REGION, STORE, PRODUCT

    Users naturally view this data in multiple ways e.g. Roll result up to aggregate on the country and region (group by country, region) and then

    to the overall total (no group by clause, whole table aggregation).

    User may also want a different perspective e.g. group by region, product.

    Currently, all these different viewings require different succinct queries.

  • 8/6/2019 SQL Grouping Functions

    9/50

    2010 IBM Corporation

    IBM Power Systems

    9

    Grouping Sets and Super Groups

    Grouping Sets and Super Groups allow a user to groupand aggregate data in multiple ways in one query.

    4 new keywords enable new Grouping Functions support GROUPING SETS ROLLUP CUBE GROUPING Aggregate Function

    Syntax dictates whether multiple sets in a query act in an additive or multiplicative fashion

  • 8/6/2019 SQL Grouping Functions

    10/50

    2010 IBM Corporation

    IBM Power Systems

    10

    Grouping Functions Syntax and Examples

  • 8/6/2019 SQL Grouping Functions

    11/50

    2010 IBM Corporation

    IBM Power Systems

    11

    Grouping Sets Example

    SELECT * FROM ( SELECT COUNTRY, REGION, NULL , SUM(SALES)

    FROM TRANS GROUP BY COUNTRY, REGION, NULLUNION ALL

    SELECT COUNTRY, NULL , STORE, SUM(SALES)FROM TRANS GROUP BY COUNTRY, NULL, STORE )

    Total sales grouped by (country, region) and by (country, store)

    SELECT COUNTRY, REGION, STORE, SUM(SALES)FROM TRANS

    GROUP BY GROUPING SETS ( (COUNTRY, REGION) ,

    (COUNTRY, STORE) )

    Logical Equivalent SQL UNION syntax:

  • 8/6/2019 SQL Grouping Functions

    12/50

    2010 IBM Corporation

    IBM Power Systems

    12

    Grouping Sets Example

    1,310,000-SWU.S.A.

    100,000Bobs-Canada

    350,000Barbs-U.S.A.

    770,000Caining-U.S.A.

    400,000Menes-U.S.A.

    500,000Mills-U.S.A.

    300,000Pensk-U.S.A.

    200,000Shell-U.S.A.

    140,000Targe-U.S.A.

    440,000Toms-U.S.A.

    150,000Wally-U.S.A.

    550,000-SEU.S.A.

    940,000-NWU.S.A.

    450,000-NEU.S.A.

    100,000-NWCanada

    Sum(Sales)StoreRegionCountrySELECT

    COUNTRY,REGION,

    NULL ,SUM(SALES)FROM TRANS

    GROUP BYCOUNTRY,

    REGION,

    NULL

    SELECTCOUNTRY,

    NULL ,STORE,

    SUM(SALES)FROM TRANS

    GROUP BYCOUNTRY,

    NULL, STORE

    SELECT COUNTRY, REGION, STORE, SUM(SALES)FROM TRANS GROUP BY GROUPING SETS((COUNTRY, REGION), (COUNTRY, STORE))

    Store is NULLeven if Store is

    Not Null Capable!

  • 8/6/2019 SQL Grouping Functions

    13/50

    2010 IBM Corporation

    IBM Power Systems

    13

    Rollup Example

    SELECT * FROM (SELECT COUNTRY, REGION, SUM(SALES)

    FROM TRANS GROUP BY COUNTRY, REGION

    UNION ALLSELECT COUNTRY, NULL, SUM(SALES)

    FROM TRANS GROUP BY COUNTRY, NULL UNION ALL

    SELECT NULL, NULL, SUM(SALES)FROM TRANS)

    SELECT COUNTRY, REGION, SUM(SALES)FROM TRANS

    GROUP BY ROLLUP (COUNTRY, REGION)

    Logical equivalent SQL UNION syntax:

    The ROLLUP operation generates a result set showingaggregates for a hierarchy of values in the selected columns

  • 8/6/2019 SQL Grouping Functions

    14/50

    2010 IBM Corporation

    IBM Power Systems

    14

    Rollup Example

    SELECT NULL,NULL,

    SUM(SALES)FROM TRANS

    SELECT COUNTRY,NULL, SUM(SALES)

    FROM TRANS GROUPBY COUNTRY, NULL

    SELECT COUNTRY,REGION,

    SUM(SALES)FROM TRANS

    GROUP BYCOUNTRY, REGION

    1,310,000SWU.S.A.

    550,000SEU.S.A.

    940,000NWU.S.A.

    450,000NEU.S.A.

    100,000NWCanada

    3,250,000-U.S.A.

    100,000-Canada

    3,350,000--

    Sum(Sales)RegionCountry

    Question: Is this data ordered?

    SELECT COUNTRY, REGION, SUM(SALES) FROM TRANSGROUP BY ROLLUP (COUNTRY, REGION)

  • 8/6/2019 SQL Grouping Functions

    15/50

    2010 IBM Corporation

    IBM Power Systems

    15

    Ordered Rollup Example

    SELECT COUNTRY, REGION, SUM(SALES)FROM TRANS GROUP BY ROLLUP (COUNTRY, REGION)

    ORDER BY 1 ASC,2 ASC

    The ROLLUP operation generates a result set showing

    aggregates for a hierarchy of values in the selected columns

  • 8/6/2019 SQL Grouping Functions

    16/50

    2010 IBM Corporation

    IBM Power Systems

    16

    Ordered Rollup Example

    3,250,000-U.S.A.

    1,310,000SWU.S.A.

    550,000SEU.S.A.

    940,000NWU.S.A.

    450,000NEU.S.A.

    100,000-Canada

    100,000NWCanada

    3,350,000--

    Sum(Sales)RegionCountry

    SELECT COUNTRY, REGION, SUM(SALES)FROM TRANS GROUP BY ROLLUP (COUNTRY, REGION)ORDER BY 1 ASC, 2 ASC

  • 8/6/2019 SQL Grouping Functions

    17/50

    2010 IBM Corporation

    IBM Power Systems

    17

    Ordered Rollup Example

    1,310,000SWU.S.A.

    550,000SEU.S.A.

    940,000NWU.S.A.

    450,000NEU.S.A.

    3,250,000-U.S.A.

    100,000NWCanada

    100,000-Canada

    3,350,000--

    Sum(Sales)RegionCountry

    SELECT COUNTRY, REGION, SUM(SALES)FROM TRANS GROUP BY ROLLUP (COUNTRY, REGION)ORDER BY 1 ASC,2 ASC

  • 8/6/2019 SQL Grouping Functions

    18/50

    2010 IBM Corporation

    IBM Power Systems

    18

    Cube Example

    SELECT COUNTRY, REGION, SUM(SALES) FROM TRANS

    GROUP BY GROUPING SETS ((COUNTRY, REGION),(COUNTRY),

    (REGION),())

    SELECT COUNTRY, REGION, SUM(SALES) from TRANS GROUP BY CUBE (COUNTRY, REGION)

    Is equivalent to the following Grouping Sets syntax:

    The CUBE operation is a shorthand way to specify groupingover every possible dimension out of a given set of grouping

    columns

  • 8/6/2019 SQL Grouping Functions

    19/50

    2010 IBM Corporation

    IBM Power Systems

    19

    Cube Example

    SELECT * FROM (SELECT COUNTRY, REGION, SUM(SALES) FROM TRANS

    GROUP BY COUNTRY, REGION UNION ALL

    SELECT COUNTRY, NULL, SUM(SALES) FROM TRANS GROUP BY COUNTRY, NULL

    UNION ALLSELECT NULL, REGION, SUM(SALES) FROM TRANS

    GROUP BY NULL, REGION UNION ALL

    SELECT NULL, NULL, SUM(SALES) FROM TRANS)

    SELECT COUNTRY, REGION, SUM(SALES) from TRANS GROUP BY CUBE (COUNTRY, REGION)

    Logical equivalent SQL UNION syntax:

  • 8/6/2019 SQL Grouping Functions

    20/50

    2010 IBM Corporation

    IBM Power Systems

    20

    Cube Example

    SELECT NULL,REGION,

    SUM(SALES) FROMTRANS

    GROUP BY NULL,REGION

    SELECT COUNTRY,NULL, SUM(SALES)

    FROM TRANSGROUP BY

    COUNTRY, NULL

    SELECT COUNTRY,REGION,

    SUM(SALES) FROMTRANSGROUP BY

    COUNTRY, REGION

    SELECT NULL,

    NULL, SUM(SALES)FROM TRANS

    3350000--

    100000-Canada

    3250000-U.S.A.

    100000 NWCanada

    450000 NEU.S.A.

    940000 NWU.S.A.

    550000SEU.S.A.

    1310000SWU.S.A.

    1310000SW-

    550000SE-

    1040000 NW-

    450000NE-

    Sum(Sales)RegionCountry

    SELECT COUNTRY, REGION, SUM(SALES) from TRANSGROUP BY CUBE (COUNTRY, REGION)

  • 8/6/2019 SQL Grouping Functions

    21/50

    2010 IBM Corporation

    IBM Power Systems

    21

    GROUPING Aggregate functionGROUPING Aggregate function helps to differentiate between a groupingfunction over columns containing NULL values and a group where acolumn is set to NULL because it is not represented.Aggregate function produces a 0 if column is part of the grouping functionAggregate function produces a 1 if column is set to NULL because it is notrepresented

    Example :SELECT A, B, GROUPING(B), SUM(D) FROM T1 GROUP BY ROLLUP (A, B)

    GROUPING(B) returns a value of 0 for group 1: (A,B)GROUPING(B) returns a value of 1 for groups 2 and 3: (A,-),(-.-)

    Helps to differentiate between group 1 result set where B is NULL andresult sets of groups 2 & 3

    ROLLUP(AB) =(A,B), (A,-), (-,-)

  • 8/6/2019 SQL Grouping Functions

    22/50

    2010 IBM Corporation

    IBM Power Systems

    22

    Multiplicative vs. Additive

    CUBE and ROLLUP can appear as part of a GROUP BY or GROUPING

    SETS list

    When CUBE and ROLLUP expressions are combined in one set, they operatelike multipliers, forming additional set entries according to the definition of CUBE, ROLLUP

    When the CUBE or ROLLUP are separate sets, they operate in an additivefashion

    The use of parenthesis and the GROUPING SETS vs. GROUP BY clause willdictate how CUBE and ROLLUP expressions interact with each other

  • 8/6/2019 SQL Grouping Functions

    23/50

  • 8/6/2019 SQL Grouping Functions

    24/50

    2010 IBM Corporation

    IBM Power Systems

    24

    Multiplicative vs. Additive Examples

    EXAMPLE 3:

    GROUP BY A, B, ROLLUP(C,D)Or GROUP BY GROUPING SETS ( ( A, B, ROLLUP(C,D) ) )

    Parenthesis in BLUE specifies that those elements are multiplicative in nature if aROLLUP or CUBE exists.

    GROUPING SETS clause with double parenthesis is multiplicative GROUP BY clause with ROLLUP or CUBE is multiplicative ROLLUP(C,D) expands to GROUPING SETS ((C,D), (C), ()) GROUP BY occurs in a multiplicative fashion to form:

    GROUP BY GROUPING SETS ((a, b, c, d), (a, b, c), (a, b))

  • 8/6/2019 SQL Grouping Functions

    25/50

    2010 IBM Corporation

    IBM Power Systems

    25

    Multiplicative vs. Additive Examples

    EXAMPLE 4:

    GROUP BY CUBE(A, B), ROLLUP(C, D), E OR GROUP BY GROUPING SETS ( ( CUBE(A,B), ROLLUP(C,D), E ) )

    Parenthesis in BLUE specifies that those elements are multiplicative in nature if aROLLUP or CUBE exists

    CUBE(A,B) expands to GROUPING SETS ((A,B), (A), (B), ())

    ROLLUP(C,D) expands to GROUPING SETS ((C,D), (C), ())

    GROUP BY occurs in a multiplicative fashion to form:

    GROUPING SETS ((A, B C, D, E), (A, B, C, E), (A, B, E), (A, C, D, E), (A, C,E), (A, E), (B, C, D, E), (B, C, E), (B, E), (C, D, E), (C, E), (E))

    IBM P S

  • 8/6/2019 SQL Grouping Functions

    26/50

    2010 IBM Corporation

    IBM Power Systems

    26

    Grouping Functions Performance Tuning

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    27/50

    2010 IBM Corporation

    IBM Power Systems

    27

    Performance Tuning Opportunities

    Sound Indexing & Statistics Strategy

    (ibm.com/servers/enable/site/education/ibo/record.html?indxng) Create Radix Indexes for common Grouping Set and ROLLUP sets Run Visual Explain to view query rewrites Use Index Advised to find holes in indexing strategy and temporary index creates

    Sorted Aggregate Hash Table Creates Could be replaced by a permanent index if index encapsulates predicate selection, grouping

    and aggregation columns. Index Advised can help with this Parallelism can greatly improve populate time of the hash table

    Temporary Index Creates Could be replaced by a permanent index if index encapsulates predicate selection, grouping

    and aggregation columns. Index Advised can help with this Can imply that the environment is memory constrained

    Memory Constaints Grouping function queries are generally more memory intensive as they often union multiple

    trees that create and interrogate temporary objects

    Use feedback tools to determine the Optimizers fair share of memory

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    28/50

    2010 IBM Corporation

    IBM Power Systems

    28

    Strategic Indexed Advised MechanismsVisual Explain

    System wide index advice (V5R4) Data is placed into a DB2 table (QSYS2/SYSIXADV) Autonomic with no overhead

    SQE Plan Cache (V5R4) No direct index advice

    Index advice via Snapshot data or Visual Explain

    SQE Plan Cache Snapshot (V5R4) Enhanced SQE index advised 3020 rows to show multiple indexes for same table Temporary index created

    Detailed Database Monitor (V5R4) Enhanced SQE index advised 3020 rows to show multiple indexes for same table Temporary index created

    IndexesAdvised

    SQE PlanCache

    QueryOptimization

    SQL request

    DetailedDB Monitor

    Data

    DebugJob Log

    MessagesPrint SQLInformationMessages

    VisualExplain

    SummarizedDB Monitor

    Data

    SQE PlanCache

    Snapshots

    IndexesAdvised

    SQE PlanCache

    QueryOptimization

    SQL request

    DetailedDB Monitor

    Data

    DebugJob Log

    MessagesPrint SQLInformationMessages

    VisualExplain

    SummarizedDB Monitor

    Data

    SQE PlanCache

    Snapshots

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    29/50

    2010 IBM Corporation

    IBM Power Systems

    29

    Grouping Set Index Advised ExampleSELECT REGION, COUNTRY, COUNT(TERRITORY) FROM CUST_DIM WHERE CONTINENT = 'AMERICA'

    GROUP BY GROUPING SETS ((REGION),(COUNTRY))

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    30/50

    2010 IBM Corporation

    y

    30

    Grouping Set Index Advised Example

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    31/50

    2010 IBM Corporation

    y

    31

    Grouping Set Index Advised Example

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    32/50

    2010 IBM Corporation32

    Grouping Set Index Advised ExampleSELECT REGION, COUNTRY, COUNT(TERRITORY) FROM CUST_DIM GROUP BY GROUPING SETS ((REGION),(COUNTRY))

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    33/50

    2010 IBM Corporation33

    Grouping Set Index Advised Example

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    34/50

    2010 IBM Corporation34

    Grouping Set Index Advised Example

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    35/50

    2010 IBM Corporation35

    Feedback Mechanism Changes for Grouping Sets

    CHANGED

    Visual Explain New icons: Multiple Aggregations, Temporary Distinct Hash TableDetailed Database Monitor Data SQL trace Grouping rows (3028 rows) will get cut for each ROLLUP and Grouping Set leg See V6R1 info Center doc for 3028 record changes

    Indexes Advised System wide Index Advised strategy unchanged Indexes will be advised during normal optimization path

    UNCHANGEDSQE Plan CacheSQE Plan Cache SnapshotsSummarized Database Monitor DataDebug Job Log MessagesPRTSQLINF Messages

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    36/50

    2010 IBM Corporation36

    DB2 Family & MQTs Stage 2 V5R4

    MQT Example:

    CREATE TABLE Example_MQT AS(SELECT Geography, Region, Year, Month,SUM(Revenue) AS Total_Revenue, SUM(Quantity) AS Total_Quantity,COUNT(*) AS Rows_per_Group

    FROM Example_TableGROUP BY Geography,Region,Year,Month)

    DATA INITIALLY IMMEDIATEREFRESH DEFERRED

    ENABLE QUERY OPTIMIZATIONMAINTAINED BY USER

    Optimizer could use the MQT instead of fully executing the following query: SELECT Geography, Year,

    SUM(Revenue) AS Total_Revenue, SUM(Quantity) AS Total_Quantity,FROM Example_Table WHERE Year IN (2004, 2005)

    GROUP BY Geography, Year; white paper: ibm.com/servers/enable/site/education/abstracts/438a_abs.html

    User responsible for keeping MQT data current and activating optimizer MQT awareness withQAQQINI options

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    37/50

    2010 IBM Corporation37

    MQTs and Grouping SetsMQT matching strategy will look for an exact match of the grouping sets in thequery to grouping sets in an MQT.

    Example :SELECT A, B, C, SUM(D) FROM T1 GROUP BY GROUPING SETS ((A), (B, C))

    MQT1 :CREATE TABLE MQT1 AS SELECT A, SUM(D) as SUM_MQT1 FROM T1GROUP BY A

    MQT2 :CREATE TABLE MQT2 AS SELECT B, C, SUM(D) as SUM_MQT2 FROM T1GROUP BY B, C

    The MQTs can be used as follows:SELECT A, NULL, NULL, SUM_MQT1 FROM MQT1

    UNION ALLSELECT NULL, B, C, SUM_MQT2 FROM MQT2

    MQTs which provide a subset of the grouping sets asked for in the query can beunioned with a query that implements the remaining grouping sets

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    38/50

    2010 IBM Corporation38

    MQTs and ROLLUPIf the query contains a ROLLUP, an MQT that does simple grouping or ROLLUPover the ROLLUP columns is useful

    Example:SELECT A, B, C, SUM(D) FROM T1 GROUP BY ROLLUP (A, B, C)

    MQT :SELECT A, B, C, SUM(D) as SUM_MQT FROM T1 GROUP BY A, B, C

    The MQT can be used as follows:SELECT A, B, C, SUM(SUM_MQT) FROM MQT GROUP BY ROLLUP(A, B, C)

    SUM(D) becomes a SUM(SUM) when the MQT is substituted. Other aggregateexpressions can be done with similar compensations. COUNT aggregate expression becomes SUM of the MQT COUNT MAX/MIN becomes MAX/MIN of the MQT MAX/MIN.

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    39/50

    2010 IBM Corporation39

    MQTs with GROUPING aggregate functionExample :

    SELECT A, B, C, SUM(D) FROM T1 GROUP BY ROLLUP (A, B, C)

    MQT :SELECT A, B, C, E, GROUPING(E) as GE, SUM(D) as SUM_MQT FROM T1 GROUP BY ROLLUP(A, B, C, E)

    The MQT can be used as follows:SELECT A, B, C, SUM_MQT FROM MQT WHERE GE = 1

    Rows where GROUPING(E) = 1 are results for ROLLUP(A, B, C) Optimizer must be able to differentiate between result set for GS(A,B,C,E)

    where E contains NULLs and GS(A,B,C,-), GS(A,B,-,-), GS(A,-,-,-) andGS(-,-,-,-)

    MQT would not be usable without GROUPING(E) in the MQT definition

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    40/50

    2010 IBM Corporation40

    MQTs and Grouping SetsMQTs with GROUPING SET, ROLLUP and CUBE syntax can be used to handlegrouping queries

    Example :SELECT SUM(D) FROM T1 GROUP BY GROUPING SETS((A), (B), (C))SELECT SUM(D) FROM T1 GROUP BY GROUPING SETS((A), (B))SELECT SUM(D) FROM T1 GROUP BY GROUPING SETS((A), (C))SELECT SUM(D) FROM T1 GROUP BY GROUPING SETS((B), (C))SELECT SUM(D) FROM T1 GROUP BY (A)SELECT SUM(D) FROM T1 GROUP BY (B)SELECT SUM(D) FROM T1 GROUP BY (C)

    Can be satisfied with MQT :SELECT A, B, C, SUM(D), GROUPING(A), GROUPING(B), GROUPING(C) asSUM_MQT FROM T1 GROUP BY GROUPING SETS((A), (B), (C))

    MQTs specifying CUBE and ROLLUP tend to carry large volumes of data. Used whendisk space is plentiful and:

    Contain commonly used grouping permutations Very commonly run CUBE and ROLLUP queries

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    41/50

    2010 IBM Corporation41

    Summary

    New Grouping Functions Allow for multiple Grouping operations in 1 query,usually using 1 pass through the data

    GROUP BY GROUPING SETS((C1,C2,C3), (C1,C2,C3,C4),(C1,C2) GROUP BY ROLLUP (C1,C2,C3) GROUP BY CUBE(C1,C2)

    Be careful not to Ask for more than you need.

    Use Visual explain to understand and tune query before you run it. Index advise is Key

    Consider MQTs Solve these kinds of queries Provide great performance for expanded use of this grouping technology

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    42/50

    2010 IBM Corporation42

    V6R1 IndexEnhancements

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    43/50

    2010 IBM Corporation43

    V6R1 Creation of Index with Derived values

    Creation of indexes with derived values via SQL

    CREATE INDEX STAR100G.ODERPRIORITYUPPER ONSTAR100G.ITEM_FACT ( UPPER(ORDERPRIORITY ) AS UORDERPRIORITYASC);

    CREATE ENCODED VECTOR INDEX STAR100G.YEARQTR ONSTAR100G.ITEM_FACT (YEAR(ORDERDATE) AS ORDYEAR ASC,

    QUARTER(ORDERDATE) AS ORDQTR ASC);CREATE INDEX STAR100G.TOTALEXTENDEDPRICE ON

    STAR100G.ITEM_FACT ( QUANTITY * EXTENDEDPRICE AS TOTEXTPRICEASC);

    * BEWARE, THERE ARE SOME RESTRICTIONS ON WHAT QUERY OPTIMIZER CANUSE

    IBM Power Systems

    h d h d

  • 8/6/2019 SQL Grouping Functions

    44/50

    2010 IBM Corporation44

    V6R1 Enhanced Native access to the SQL Index

    Specify format name And table columns to be added to that format.

    CREATE INDEX STAR100G.SHIPMODE ON STAR100G.ITEM_FACT

    (SHIPMODE ASC, ZIPCODE ASC ) RCDFMT SHPMODEZIP ADDCUSTNAME, SHIPADDR;

    CREATE INDEX STAR100G.SHIPMDMON ON STAR100G.ITEM_FACT(SHIPMODE ASC, MONTH(SHIPDATE AS SHIPMON for COLUMNSHIPMON ASC) RCDFMT SHPMODEMON ADD CUSTNAME, SHIPADDR,COMMITDATE;

    IBM Power Systems

    C i d f SQL

  • 8/6/2019 SQL Grouping Functions

    45/50

    2010 IBM Corporation45

    Create sparse indexes from SQL

    Support of WHERE clause on SQL create index

    CREATE INDEX STAR100G.FASTDELIVER ON STAR100G.ITEM_FACT (SHIPMODEASC) WHERE SHIPMODE = 'NEXTDAYAIR' OR SHIPMODE = 'COURIER ';

    CREATE INDEX STAR100G.FASTDELIVR ON STAR100G.ITEM_FACT (SHIPMODE ASSHIPMODE FOR COLUMN SHIPMODE ASC) WHERE SHIPMODE = 'NEXTDAYAIR'OR SHIPMODE = 'COURIER' RCDFMT FASTDLVR ADD ORDERKEY, SHIPDATE,COMMITDATE, RECEIPTDATE, SHIPPRIORITY;

    Limitations NOT USED BY THE QUERY OPTIMIZER (BOTH SQE AND CQE)

    IBM Power Systems

    Wh h ld I D i d IX t?

  • 8/6/2019 SQL Grouping Functions

    46/50

    2010 IBM Corporation46

    When should I use Derived IX support?

    Could replace some logical files with SQL indexes for use by Nativeprograms. Modernize those objects Big logical pagesize

    Derived indexes may be useful for Case insensitive searches Data extracted from a column (i.e. SUBSTR, YEAR, MONTH..

    Results of operations ( COL1+COL2 , QTY * COST) Might be useful to allow Index only access in more cases Reduce table scans Index Used more often for local selection

    IBM Power Systems

    Wh Sh ld I WHERE Cl t

  • 8/6/2019 SQL Grouping Functions

    47/50

    2010 IBM Corporation47

    When Should I use WHERE Clause support

    In general its not a good idea to build indexes with the WHERE clause Not yet useable by the Optimizer May be too specific, Only useable by small number of statements DO NOT Replace general purpose index with multiple Sparse indexes

    CREATE INDEX ORDERYEAR on ORDERS (YEAR ASC) NOT the following

    CREATE INDEX ODERYEAR1 on ORDERS(YEAR ASC)WHERE YEAR=2006

    CREATE INDEX ODERYEAR2 on ORDERS(YEAR ASC)WHERE YEAR=2007

    CREATE INDEX ODERYEAR3 on ORDERS(YEAR ASC)WHERE YEAR=2008

    IBM Power Systems

    Are you experiencing performance problems?

  • 8/6/2019 SQL Grouping Functions

    48/50

    2010 IBM Corporation48

    IBM DB2 for i Center of Excellence

    Database modernizationDB2 Web QueryDatabase architecture and design

    DB2 SQL performance analysis and tuningData warehousing and Business IntelligenceDB2 for i education and training

    Contact: Mike Cain [email protected] Systems and Technology Group

    Rochester, MN USA

    Are you experiencing performance problems?

    Are you using SQL?

    Are you getting the most out of DB2 for i?

    Need help?

    mailto:[email protected]:[email protected]
  • 8/6/2019 SQL Grouping Functions

    49/50

    Thank You

    IBM Power Systems

  • 8/6/2019 SQL Grouping Functions

    50/50

    2010 IBM Corporation50

    Trademarks and DisclaimersAdobe, Acrobat, PostScript and all Adobe-based trademarks are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States, other countries, or both.

    Intel, Intel logo, Intel Inside, Intel Inside logo, Intel Centrino, Intel Centrino logo, Celeron, Intel Xeon, Intel SpeedStep, Itanium, and Pentium are trademarks or registered

    trademarks of Intel Corporation or its subsidiaries in the United States and other countries.Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.

    Microsoft, Windows, Windows NT, and the W indows logo are trademarks of Microsoft Corporation in the United States, other countries, or both.

    IT Infrastructure Library is a registered trademark of the Central Computer and Telecommunications Agency which is now part of the Off ice of Government Commerce.

    ITIL is a registered trademark, and a registered community trademark of the Office of Government Commerce, and is registered in the U.S. Patent and TrademarkOffice.

    UNIX is a registered trademark of The Open Group in the United States and other countries.

    Cell Broadband Engine and Cell/B.E. are trademarks of Sony Computer Entertainment, Inc., in the United States, other countries, or both and are used under licensetherefrom.

    Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.

    Other company, product, or service names may be trademarks or service marks of others.

    Information is provided "AS IS" without warranty of any kind.

    The customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actualenvironmental costs and performance characteristics may vary by customer.

    Information concerning non-IBM products was obtained from a supplier of these products, published announcement material, or other publicly available sources anddoes not constitute an endorsement of such products by IBM. Sources for non-IBM list prices and performance numbers are taken from publicly available information,including vendor announcements and vendor worldwide homepages. IBM has not tested these products and cannot confirm the accuracy of performance, capability, or any other claims related to non-IBM products. Questions on the capability of non-IBM products should be addressed to the supplier of those products.

    All statements regarding IBM future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives only.

    Some information addresses anticipated future capabilities. Such information is not intended as a definitive statement of a commitment to specific levels of performance, function or delivery schedules with respect to any future products. Such commitments are only made in IBM product announcements. The information ispresented here to communicate IBM's current investment and development activities as a good faith effort to help with our customers' future planning.

    Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment. The actual throughput or performance that anyuser will experience will vary depending upon considerations such as the amount of multiprogramming in the user's job stream, the I/O configuration, the storageconfiguration, and the workload processed. Therefore, no assurance can be given that an individual user will achieve throughput or performance improvementsequivalent to the ratios stated here.

    Prices are suggested U.S. list prices and are subject to change without notice. Contact your IBM representative or Business Partner for the most current pricing in your

    geography.