Professional Documents
Culture Documents
Presentation Overview
How Does DB2 Get Data? Index Considerations Tablespace Considerations Tune the SQL to Get the Biggest Bang for your Buck Access Paths What are the Choices?
DB2 Subsystem
Tablespace Table Page Row
Database
Indexspace Index Page
Page Page
What is a Page?
4K
GETPAGE
Physical I/O
8K
16K
4K page, maximum number of data bytes on the page 4,074. Maximum row size 4,056. Row size greater than 2,020 bytes will end up with just one row per page. 32K
SQL
DBM1
Database Services
DSNDB01
DSNDB06
Directory
Catalog
SQL, DDL, DML, DCL Optimization Data Manager Relational Data System Buffer Manager Sort Pool EDM Pool RID Pool Bind/Rebind IRLM Interaction
DSNDB07
WorkFile
User Data
DB2 Stogroups
SMS Managed Stogroup CREATE STOGROUP GSMSVOL VOLUMES(*) VCAT S0;
STOGROUP GPRVVOL
Volume B2RP01
Non-SMS User Managed Stogroup CREATE STOGROUP GPRVVOL VOLUMES(B2RP01 ,B2RP02 ,B2RP03) VCAT P0;
Volume B2RP02
Volume B2RP03
SMS Allocations
DB2
Tablespace or Indexspace Allocations (Create, Extent Allocation, Load, Reorg)
ACS Routines
(4th node is NOT X or I) (4th node is X or I)
SMS
Storage Group T
cache cache
Storage Group I
cache cache
tablespace
volume PB34
tablespace
volume PBL8
indexspace
volume PBP2
indexspace
volume PBP8
DSNDB07 BP7
DSNDB04
Default Tablespace
Default Indexspace
BP4
BP48
BP49
ERP Indexes
CRM Indexes
Other Indexes
Random Tablespaces
BP1
BP3
BP5
BP9
BP12
ERP Tablespaces
CRM Tablespaces
Other Tablespaces
32K Tablespaces
BP2
BP6
BP8
BP10
BP32K
Buffer Manager
Group Buffer Pool
Hiperpool
cache
indexspace
Physical I/O
STAGE 1 PREDICATES
BUFFER POOL
Get Page Request
DATA MANAGER
indexable sargable
tablespace
SORT POOL
1. selected columns 2. calc/derived values 3. sort columns
STAGE 2 PREDICATES
BP7
DSNDB07
CREATE INDEX DEFER NO key columns, RIDs
STAGE 1 PREDICATES
DATA MANAGER
indexable sargable
Index Considerations
Index Limits based on # of pages? What's the I/O rate per page? Is this even realistic?
Clustering Vs. Non Clustering Unique Vs. Duplicate Piecesize with NPIs Uniqueness, Performance, RI, Sorting Table Size Minimize I/O
10
15 # Indexes C
TS Type Partitioned big Partitioned large Partitioned medium Segmented medium - large Segmented small Code Tables Segmented or Partitioned
Number Pages > 5,000,000 < 5,000,000 > 200,000 < 200,000 > 5,000 > 5,000 < 500,000 < 5,000 < 200
Page 501 Page 552 14831 AK Mount Bear p. 501, 2 p. 502, 2 Page 502 14494 CA Mount Whitney 14420 CO Mount Harvard Page 503 14420 CO Mount Harvard 20320 AK Mount McKinley 14831 AK Mount Bear
Page 553
14270 CO Grays Peak 14196 CO Mount Yale
p. 503, 2
p. 504, 2
Page 554 14110 CO Pikes Peak 14059 CO Sunlight Peak p. 505, 2 p. 506, 2
14172 CO Mount Bross 14110 CO Pikes Peak Page 506 14080 CA Starlight Peak 14059 CO Sunlight Peak Page 507
Page 555 14037 CO Little Bear Peak 14000 CA Thunderbolt Peak NON-LEAF Page p. 507, 2 p. 508, 2
14058 CA Split Mountain 14037 CO Little Bear Peak Page 508 14005 CO Mount o/t Holy Cross 14000 CA Thunderbolt Peak
Page 401
Page 501 p. 503, 2 p. 507, 2 20320 AK Mount McKinley 14831 AK Mount Bear Page 502 p. 504, 1 p. 501, 2 14494 CA Mount Whitney 14420 CO Mount Harvard Page 503 p. 505, 1 p. 502, 2 14410 WA Mount Rainier 14270 CO Grays Peak Page 504 p. 501, 1 p. 508, 1 14255 CO Longs Peak 14196 CO Mount Yale Page 505
NON-CLUSTERED INDEX
Page 301
Grays Peak Little Bear Peak p. 401, 2 p. 402, 2 Longs Peak Mount Bear Page 402
Peak Name
Page 201 Mount Bear Mount o/t Holy Cross p. 301, 2 p. 302, 2
Page 403 Mount Bross Mount Harvard Page 302 Mount Harvard p. 403, 2 p. 404, 2 Page 404 Mount McKinley Mount o/t Holy Cross Page 405 Page 303 Mount Rainier p. 405, 2 p. 406, 2 Mount Yale Pikes Peak Mount Whitney Page 406 p. 504, 2 p. 505, 2 Page 407 Split Mountain p. 507, 1 p. 506, 1 Page 304 Starlight Peak Thunderbolt Peak p. 407, 2 p. 408, 2 Starlight Peak Page 408 Sunlight Peak Thunderbolt Peak LEAF Page p. 506, 2 p. 508, 2
Page 101 Mount o/t Holy Cross Thunderbolt Peak ROOT Page p. 201, 2 p. 202, 2
p. 503, 1
p. 502, 1
14172 CO Mount Bross 14110 CO Pikes Peak Page 506 14080 CA Starlight Peak 14059 CO Sunlight Peak Page 507 14058 CA Split Mountain 14037 CO Little Bear Peak Page 508 14005 CO Mount o/t Holy Cross 14000 CA Thunderbolt Peak DATA Page
p. 303, 2 p. 304, 2
NON-LEAF Page
NON-LEAF Page
Page 461
363438N 1181733W
370116N 1182520W
p. 461, 2
p. 462, 2
Page 462 370539N 1183119W 370539N 1183124W Page 463 373400N 1052948W 373738N 1073543W Page 464 385026N 1050238W 385039N 1061848W Page 465 385528N 1061912W 392007N 1060625W Page 466 392805N 1062845W p. 508, 1 p. 502, 2 p. 505, 1 p. 505, 2 p. 504, 2 p. 507, 2 p. 506, 2 p. 506, 1 p. 508, 2
14494 CA Mount Whitney 14420 CO Mount Harvard Page 503 14410 WA Mount Rainier 14270 CO Grays Peak Page 504
Page 282
393802N 1054901W 630410N 1510013W p. 333, 2 p. 334, 2 Page 334 465110N 1214531W 630410N 1510013W p. 467, 2 p. 468, 2
393802N 1054901W
Page 467 401517N 1053655W
p. 503, 2
p. 504, 1 p. 503, 1
NON-LEAF Page
465110N 1214531W Page 468 611702N 1410832W 630410N 1510013W LEAF Page
p. 501, 2 p. 501, 1
14005 CO Mount o/t Holy Cross 14000 CA Thunderbolt Peak DATA Page
NON-LEAF Page
Index Statistics
PEAKS CLIMBED TABLE
LATITUDE 630410N 630410N 630410N 630410N 630410N 630410N 630410N 630410N 630410N 630410N LONGITUDE 1510013W 1510013W 1510013W 1510013W 1510013W 1510013W 1510013W 1510013W 1510013W 1510013W CLIMBER_ID STRT_DT CA000015 4/20/2001 CA000015 2/11/2000 CE000008 2/7/2000 CE000008 3/3/1999 CE000008 1/14/1998 CJ000033 3/17/2002 CJ000033 12/19/2000 CR000039 2/14/2002 CR000039 3/30/2001 CR000039 1/7/1999 END_DT TB_TIME SUMMIT 5/28/2001 11:00 YES 3/13/2000 10:00 NO 3/22/2000 12:00 YES 4/16/1999 13:00 YES 3/14/1998 15:30 NO 5/13/2002 14:30 YES 2/14/2001 15:00 NO 4/9/2002 13:30 YES 5/12/2001 14:00 YES 3/10/1999 13:00 NO O2 YES NO YES YES YES NO YES NO NO NO GUIDED NO YES NO YES YES NO YES NO YES NO
SORT Required?
Indexes & Stats: PK Index on: Latitude, Longitude, Climber_ID, Strt_DT NPI: Climber_ID, Summit, O2, Guided RUNSTATS TABLESPACE dbname.tsname INDEX(ALL)
1st Column & Fullkey Cardinality on all indexes Top 10 most frequent values for 1st column of all indexes
SELECT CLIMBER_ID, LATITUDE, LONGITUDE, O2, GUIDED FROM PEAKS_CLIMBED WHERE CLIMBER_ID = CE000008 AND SUMMIT = YES ORDER BY O2, GUIDED WITH UR;
Page 307 RUNSTATS TABLESPACE dbname.tsname INDEX(ALL) SELECT PEAKNAME FROM PEAKS WHERE STATE = CO; RUNSTATS INDEX DB2.STELEV FREQVAL 10 NUMCOLS 2 AK 20320 AK 14831 p. 501, 1 p. 501, 2
Page 501
20320 AK Mount McKinley 14831 AK Mount Bear Page 502 14494 CA Mount Whitney 14420 CO Mount Harvard Page 503 14410 WA Mount Rainier 14270 CO Grays Peak Page 504 14255 CO Longs Peak 14196 CO Mount Yale Page 505 14172 CO Mount Bross 14110 CO Pikes Peak Page 506 14080 CA Starlight Peak 14059 CO Sunlight Peak Page 507 14058 CA Split Mountain 14037 CO Little Bear Peak Page 508 14005 CO Mount o/t Holy Cross 14000 CA Thunderbolt Peak DATA Page
CA 14080
Colvalue AK CA CO WA Colvalue AK CA CO WA CardF 2 4 9 1 CardF 21 15 54 1 FrequencyF 0.1250 0.2500 0.5625 0.0625 FrequencyF 0.2308 0.1648 0.5934 0.0110
ColGroupColNo AK 20320 AK 14831 CA xxxxx CO xxxxx WA xxxxx
ColGroupColNo AK xxxxx CA xxxxx CA 14162 CA 14000 CO xxxxx CO 14265 CO 14162 CO 14042 CO 14014 CO 14005 CO 14197 WA 14410
CardF 1 1 1 1 1
CardF 1 1 2 2 1 2 2 2 2 2 3 1
CREATE INDEX DB2.STELEV ON DB2.PEAKS STATE, ELEVATION DEFER YES PIECESIZE 2G;
p. 503, 1
LEAF Page
List Prefetch
Page 461 363438N 1181733W 370116N 1182520W Page 462 370539N 1183119W 370539N 1183124W Page 463 373400N 1052948W p. 507, 2 p. 506, 1 p. 508, 2 p. 502, 1 p. 507, 1 RID LIST 363438N 1181733W 370116N 1182520W 370539N 1183119W 370539N 1183124W p. 502, 1 p. 507, 1 p. 506, 1 p. 508, 2 Page 501 20320 AK Mount McKinley 14831 AK Mount Bear Page 502 14494 CA Mount Whitney 14420 CO Mount Harvard Page 503 14410 WA Mount Rainier
373738N 1073543W
Page 464 385026N 1050238W 385039N 1061848W Page 465 385528N 1061912W 392007N 1060625W Page 466 392805N 1062845W 393802N 1054901W Page 467 401517N 1053655W 465110N 1214531W Page 468 611702N 1410832W 630410N 1510013W
p. 506, 2
373738N 1073543W
(poor cluster ratio)
p. 506, 2
p. 505, 2 p. 504, 2
p. 502, 2 p. 505, 1
Sorted RID LIST 363438N 1181733W 370539N 1183119W 373738N 1073543W p. 502, 1
14172 CO Mount Bross 14110 CO Pikes Peak Page 506 14080 CA Starlight Peak 14059 CA Sunlight Peak Page 507
p. 506, 1
p. 506, 2 p. 507, 1 p. 508, 2
p. 508, 1 p. 503, 2
p. 504, 1 p. 503, 1
p. 501, 2 p. 501, 1
14058 CA Split Mountain 14037 CO Little Bear Peak Page 508 14005 CO Mount o/t Holy Cross 14000 CA Thunderbolt Peak
Tablespace Types
Segmented Tablespace - 3 table example Simple Tablespace - 1 table example
Simple Tablespaces can have multiple tables; however, rows can be intermingled. Use with caution on small static read only tables. Spacemap keeps track of segments (pages) by table. Spacemap page in a segmented tablespace helps when a program deletes without a where clause by marking those segments logically deleted. Spacemap page also makes segments tied to dropped tables accessible. Segsize between 4-64 increasing in increments of 4. See recommendations in notes section.
Partitioned tablespaces allow only 1 table. Maximum number of partitions 254. Above 64 considered Large.
Random I/O
Buffer Manager initiates a GETPAGE request for P3 and P13.
CO WA
Sequential Detection
Page 501 20320 AK Mount McKinley 14831 AK Mount Bear Page 502 14494 CA Mount Whitney
ACTIVE 6 out of 8
p. 532
p. 570
24
38
20
20
17
47
Minimize Logging
Blue represents static columns that do not change once the row is created Purple represents column grouping updated associated to daily billing transactions Teal represents column grouping updated associated to monthly inventory transactions Dashed box represents variable data
Tuning SQL
Tuning Steps Static and Dynamic SQL Get What You Need, Not What You Want Reoptimization Explain & Optimization Hints
WHAT is Estimate & Select Access Path with Cheapest Estimated Cost
Proper statistics can reduce the amount of SQL tuning required Lackluster statistics can be a Pandora's box!
REBIND DATE < RUNSTATS DATE RUNSTATS performed yearly EXPLAIN(NO)
Optimizer does not use Real Time Stats for access path selection Better filtering = fewer rows returned
sort DSNDB07
DASD
data manager
BP3
(DB2 RI)
EMP_ID 77009134 77009134 93989203 99080622 ROLE 0001 0003 0002 0004
PROJECT_ROLE (child)
PROJECT_1ST CLIENT REWRITE CUST RETENTION AGENT COMPENSATION CLIENT REWRITE PROJECT_2ND AGENT COMPENSATION CUST RETENTION CUST RETENTION
DELETE RESTRICT
. Insert new row to add a new role (data modeler) . User, Programmer, or DBA can insert the row . Slower performance
. DDL change to add a new role (data modeler) . DBA involvement needed for the table change . Better Performance, Tighter Control
OPTIMIZATION HINTS
Sounds Great! Difficult to implement and maintain. DSNZPARM change to activate Programmers should add QUERYNO to their code. How are you going to name and manage your opthints? CHAR(8) After code has been bound with explain yes, need to update plan_table rows to add a OPTHINT. To return to the good access path you have previously established, you would rebind your package with OPTHINT(ohstring). Verify hint is in use! SQL code +394, HINT_USED column of PLAN_TABLE, or Query special register: CURRENT OPTIMIZATION HINT
Reoptimization is done at open cursor Static SQL package level. Consider isolating/consolidating your reopt statements to a few static packages. Dynamic SQL statement level. More granular and easier to invoke on a smaller scale. IFCID 0022 Did you get a better access path? Would static SQL benefit from being dynamic and taking advantage of dynamic cache?
Access Paths
More on QUERYNO = 103; DB2s Choices Understanding Predicates Small Tables in Memory Plan_Table Considerations
I
I
mc>0
mc=0
Matching index scan (using IX structure - root, non-leaf, leaf) Non-matching index scan (scan of index leaf pages) One fetch index access (min/max) Perform 1 matching index scan for each value in (LIST) Multiple index scan (same index twice or two different indexes) Qualifying RIDs for each index used in multiple index scan AND predicate, intersection of the qualifying RIDs from each index OR predicate, union of the qualifying RIDs from each index
MATCHCOLS PREFETCH
INDEXONLY ACCESNAME
I1 N M MX MI
MU
OPTHINT RID =
record identifier
PRIMARY_ACCESSTYPE
Predicates
Know Your Predicates
Indexable, matching predicates on index key columns Stage 1, not picked as matching but still refer to index columns (index screening) Stage 1, operating on a data page Stage 2, everything else
PEAKS Table
Peak Name Mount McKinley Mount Bear Mount Whitney Mount Harvard Mount Rainier Grays Peak Longs Peak Mount Yale Mount Bross Pikes Peak Starlight Peak Sunlight Peak Split Mountain Little Bear Peak Mount o/t Holy Cross Thunderbolt Peak State Elevation Latitude AK AK CA CO WA CO CO CO CO CO CA CO CA CO CO CA 20320 14831 14494 14420 14410 14270 14255 14196 14172 14110 14080 14059 14058 14037 14005 14000 630410N 611702N 363438N 385528N 465110N 393802N 401517N 385039N 392007N 385026N 370539N 373738N 370116N 373400N 392805N 370542N Longitude 1510013W 1410832W 1181733W 1061912W 1214531W 1054901W 1053655W 1061848W 1060625W 1050238W 1183119W 1073543W 1182520W 1052948W 1062845W 1183124W Rating VERY DIFFICULT MODERATE MODERATE EASY DIFFICULT EASY MODERATE EASY EASY EASY EASY MODERATE MODERATE MODERATE EASY MODERATE EASY
Questions...