December 01-03, 2009 •Minneapolis, Chicago, Milwaukee


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

December 01-03, 2009 •Minneapolis, Chicago, Milwaukee

  1. 1. Best Practices to Improve Query Performance in a Data Warehouse - 1 Calisto Zuzarte, STSM, IBM,
  2. 2. Data Warehouse Life Cycle <ul><li>Database design / Application design </li></ul><ul><ul><li>The Warehouse Application architects and Database Administrators work together to design the queries and schema before they put the application in production </li></ul></ul><ul><li>Database performance layer implementation </li></ul><ul><ul><li>In order to meet SLAs, DBAs usual go through some iterations augmenting the database with performance layer objects and set up the initial configuration to get good performance </li></ul></ul><ul><li>Database tuning operations </li></ul><ul><ul><li>During production, with changing requirements and change in data, there is on-going tuning required to keep operations smooth. </li></ul></ul>
  3. 3. Motivation <ul><li>Data warehouse environments characteristics: </li></ul><ul><ul><li>Large volumes of data </li></ul></ul><ul><ul><ul><li>Millions/Billions of rows involved in some tables </li></ul></ul></ul><ul><ul><ul><li>Large Joins </li></ul></ul></ul><ul><ul><ul><li>Large Sorts, </li></ul></ul></ul><ul><ul><ul><li>Large Aggregations </li></ul></ul></ul><ul><ul><ul><li>Many tables involved </li></ul></ul></ul><ul><ul><ul><li>Large amount of data rolled-in and rolled-out </li></ul></ul></ul><ul><ul><li>Complex queries </li></ul></ul><ul><ul><ul><li>Report Queries </li></ul></ul></ul><ul><ul><ul><li>Ad Hoc Queries </li></ul></ul></ul><ul><li>It is important to pay attention to query performance </li></ul>
  4. 4. Objective <ul><li>Provide recommendations from a DB2 optimizer perspective to improve query performance through the Data Warehouse life cycle </li></ul>
  5. 5. Agenda <ul><li>SESSION 1 </li></ul><ul><li>Best Practices – Database Design </li></ul><ul><li>Best Practices – Application Design </li></ul><ul><li>Best Practices – Configuration and Operations </li></ul><ul><li>SESSION 2 </li></ul><ul><li>Best Practices – Performance Layer </li></ul>
  6. 6. Best Practices – Database Design <ul><li>Parallelism </li></ul><ul><ul><li>Inter-partition Shared nothing parallelism (DPF) </li></ul></ul><ul><ul><li>Intra-Query Parallelism (SMP) </li></ul></ul><ul><li>Partitioning </li></ul><ul><ul><li>Database Partitioning </li></ul></ul><ul><ul><li>Table Partitioning </li></ul></ul><ul><ul><ul><li>Table (Range) Partitioning </li></ul></ul></ul><ul><ul><ul><li>UNION ALL Views </li></ul></ul></ul><ul><ul><li>Multi-Dimension Clustering </li></ul></ul><ul><li>Schema </li></ul>
  7. 7. Best Practices - Parallelism <ul><li>DPF or SMP or both ? </li></ul><ul><li>Database partition feature (DPF) is generally recommended to achieve parallelism in a data warehouse </li></ul><ul><ul><li>Achieves scalability and query performance </li></ul></ul><ul><li>SMP (Intra-Query Parallelism) is NOT recommended in concurrent multi-user environments with heavy CPU usage </li></ul><ul><li>SMP is only recommended </li></ul><ul><ul><li>When CPUs are highly under utilized and when DPF is not an option </li></ul></ul>
  8. 8. Partitioning (Complimentary Strategies in DB2) <ul><li>Database Partitioning (DPF) </li></ul><ul><ul><li>CREATE TABLE … DISTRIBUTE BY HASH </li></ul></ul><ul><ul><li>Key Benefit : Better scalability and performance through parallelism </li></ul></ul><ul><li>Table Partitioning </li></ul><ul><ul><li>Table (Range) Partitioning </li></ul></ul><ul><ul><li>CREATE TABLE …PARTITION BY RANGE </li></ul></ul><ul><ul><li>Key Benefit : Better data management (roll-in and roll-out of data) </li></ul></ul><ul><ul><li>UNION ALL Views </li></ul></ul><ul><ul><li>CREATE VIEW V AS (SELECT … FROM F1 UNION ALL … ) </li></ul></ul><ul><ul><li>Key Benefit : Independent branch optimization </li></ul></ul><ul><li>Multidimensional Clustering (MDC) </li></ul><ul><ul><li>CREATE TABLE … ORGANIZE BY DIMENSION </li></ul></ul><ul><ul><li>Key Benefit : Better query performance through data clustering </li></ul></ul>“ Database Partitioning” “ Distribution Key” “ Table Partitioning” “ Table Partitioning Key” “ UNION ALL branch Partitioning” “ Cells”, “Blocks”, “Dimensions”
  9. 9. Distribute By … Partition By … Organize By .. <ul><li>CREATE TABLE … </li></ul><ul><li>DISTRIBUTE BY HASH </li></ul><ul><li>PARTITION BY RANGE </li></ul><ul><li>ORGANIZE BY DIMENSION </li></ul>TS1 TS2 TS1 TS2 TS1 TS2 Database Partition 1 Database Partition 2 Database Partition 3 East West East West East West East West East West East West North South North South North South North South North South North South Jan Feb Jan Feb Jan Feb
  10. 10. Best Practices – DPF Partitioning <ul><li>Collocate the fact and largest frequently joined dimension </li></ul><ul><li>Choose to avoid significant skew on some partitions </li></ul><ul><li>Avoid DATE dimension where active transactions for current date all fall on one database partition (TIMESTAMP is good) </li></ul><ul><li>Possibilities for workload isolation for data marts </li></ul><ul><ul><li>Different partition groups but common dimension tables </li></ul></ul><ul><ul><li>Recommend that dimension tables be replicated (discussed later) </li></ul></ul>
  11. 11. Best Practices – Table Partitioning <ul><li>Recommend partitioning the fact tables </li></ul><ul><li>Recommend using the DATE dimension </li></ul><ul><li>Works better with application key predicates applied directly </li></ul><ul><li>Table (Range) Partitioning </li></ul><ul><ul><li>Consider partitioned indexes with V9.7 </li></ul></ul><ul><ul><li>Choose partitioning based on roll-in / roll-out granularity </li></ul></ul><ul><li>UNION ALL Views </li></ul><ul><ul><li>Define view predicates or CHECK Constraints to get branch elimination with query predicates (with constants only) </li></ul></ul><ul><ul><li>Use UNION ALL views only with well designed applications </li></ul></ul><ul><ul><ul><li>Dangers of materialization with ad hoc queries </li></ul></ul></ul><ul><ul><ul><li>Large number of branches needs time and memory to optimize </li></ul></ul></ul>
  12. 12. Best Practices – Multidimensional Clustering (MDC) <ul><li>Recommend defining MDC on the fact table </li></ul><ul><ul><li>Guaranteed clustering (Avoids the need to REORG for clustering) </li></ul></ul><ul><ul><li>I/O optimization </li></ul></ul><ul><ul><li>Compact indexes (compact, coexists with regular indexes) </li></ul></ul><ul><li>Choose dimensions based on query predicates </li></ul><ul><ul><li>Recommend the use of 1 to 4 dimensions </li></ul></ul><ul><ul><li>Need to ensure dimensions are chosen such that they do not waste storage </li></ul></ul><ul><li>Could choose a finer granularity of Table partitioning range </li></ul><ul><ul><li>For example: Table partition range by month, MDC by date </li></ul></ul>
  13. 13. Star Schema Product_id Store_id Channel_id Date_id Amount Quantity … SALES Store_id Region_id … STORE Date_id Month_id Quarter_id Year_id TIME Channel_id … CHANNEL Product_id Class_id Group_id Family_id Line_id Division_id … PRODUCT
  14. 14. Dimension Hierarchy Channel Store Month Product Class Group Family Line Division Quarter Year Retailer Sales Fact Product Dimension Time Dimension Store Dimension Channel Dimension Level 5 Level 1 Level 2 Level 3 Level 4 Level 0 Date
  15. 15. Best Practices - Schema <ul><li>Surrogate Keys </li></ul><ul><ul><li>As far as possible use application keys themselves </li></ul></ul><ul><ul><ul><li>allows predicates to be applied/transferred directly on the fact table </li></ul></ul></ul><ul><ul><ul><li>DATE is a good candidate (easier to roll-in/roll-out and for MDC ) </li></ul></ul></ul><ul><li>Star Schema / Snowflakes </li></ul><ul><ul><li>Separate tables for each dimension hierarchy (snowflake) may result in a large number of joins </li></ul></ul><ul><ul><li>Flattened dimensions may contain a lot of redundancy (space) </li></ul></ul><ul><li>Define Columns NOT NULL when appropriate </li></ul><ul><ul><li>Many optimizations that are done based on NOT NULL </li></ul></ul><ul><li>Define Uniqueness when appropriate </li></ul><ul><ul><li>Primary Keys / Unique Constraints / Unique Indexes </li></ul></ul>
  16. 16. Agenda <ul><li>SESSION 1 </li></ul><ul><li>Best Practices – Database Design </li></ul><ul><li>Best Practices – Application Design </li></ul><ul><li>Best Practices – Configuration and Operations </li></ul><ul><li>SESSION 2 </li></ul><ul><li>Best Practices – Performance Layer </li></ul>
  17. 17. Application Considerations - Expressions <ul><li>Use constants instead of expressions in the query </li></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>SELECT … WHERE DateCol <= CURRENT DATE – 5 </li></ul></ul></ul><ul><ul><ul><li>Use VALUES(CURRENT DATE – 5) to get the constant first and use it in the query </li></ul></ul></ul><ul><li>Avoid expressions on indexed columns </li></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>SELECT … WHERE DATECOL – 2 DAYS > ‘2009-10-22’ </li></ul></ul></ul><ul><ul><ul><li>SELECT … WHERE DATECOL > ‘2009-10-22’ + 2 DAYS </li></ul></ul></ul><ul><li>Similar recommendation with cast functions </li></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>SELECT … WHERE INT(CHARCOL) = 2009 </li></ul></ul></ul><ul><ul><ul><li>SELECT … WHERE CHARCOL = ‘2009’ </li></ul></ul></ul><ul><ul><ul><li>Note you may lose Errors/Warnings </li></ul></ul></ul>
  18. 18. Application Considerations – Table Partitioning / MDC <ul><li>As far as possible put local predicates directly on Table Partition or MDC dimension columns of the fact table </li></ul><ul><ul><li>SELECT ... FROM CUSTDIM C, TIMEDIM T, FACT F </li></ul></ul><ul><ul><li>WHERE and C.KEYCOL=F.CUSTKEYCOL and </li></ul></ul><ul><ul><li>T.Date = ‘2009-01-15’ and T.KEYCOL= F.TIMEKEYCOL </li></ul></ul><ul><li>Simplify if the TIMEKEYCOL is correlated to the TIME values </li></ul><ul><ul><li>(For example TIMEKEYCOL= 20090115 for the date ‘2009-01-15’) </li></ul></ul><ul><ul><li>SELECT ... FROM CUSTDIM C, FACT F </li></ul></ul><ul><ul><li>WHERE and C.KEYCOL=F.CUSTKEYCOL and </li></ul></ul><ul><ul><li>F.TIMEKEYCOL = 20090115 </li></ul></ul>
  19. 19. Application Considerations – Table Partitioning / MDC <ul><li>Another example … consider </li></ul><ul><ul><li>SELECT ... FROM CUSTDIM C, TIMEDIM T, FACT F </li></ul></ul><ul><ul><li>WHERE and C.KEYCOL=F.CUSTKEYCOL and </li></ul></ul><ul><ul><li>T.YEAR = 2009 and T.KEYCOL= F.TIMEKEYCOL </li></ul></ul><ul><li>First get the values for MINKEY and MAXKEY </li></ul><ul><li>SELECT MIN(KEYCOL) FROM TIMEDIM WHERE YEAR=2009 </li></ul><ul><li>SELECT MAX(KEYCOL) FROM TIMEDIM WHERE YEAR=2009 </li></ul><ul><li>Then write the SQL as follows </li></ul><ul><ul><li>SELECT ... FROM CUSTDIM C, TIMEDIM T, FACT F </li></ul></ul><ul><ul><li>WHERE and C.KEYCOL=F.CUSTKEYCOL and </li></ul></ul><ul><ul><li>T.YEAR = 2009 and T.KEYCOL= F.TIMEKEYCOL AND </li></ul></ul><ul><ul><li>F.TIMEKEYCOL >= MINKEY AND </li></ul></ul><ul><ul><li>F.TIMEKEYCOL <= MAXKEY </li></ul></ul>
  20. 20. Application Considerations – General Recommendations <ul><li>Avoid repetitions of complex expressions </li></ul><ul><li>Use Global Temporary Tables to split a query if it contains more than about 15 tables and compile time is an issue </li></ul>
  21. 21. Agenda <ul><li>SESSION 1 </li></ul><ul><li>Best Practices – Database Design </li></ul><ul><li>Best Practices – Application Design </li></ul><ul><li>Best Practices – Configuration and Operations </li></ul><ul><li>SESSION 2 </li></ul><ul><li>Best Practices – Performance Layer </li></ul>
  22. 22. Best Practices – Configuration and Operations <ul><li>Configuration </li></ul><ul><ul><li>Database Configuration </li></ul></ul><ul><ul><li>DBMS Configuration </li></ul></ul><ul><ul><li>Registry Settings </li></ul></ul><ul><li>Operations </li></ul><ul><ul><li>Collecting Statistics </li></ul></ul>
  23. 23. Configuration <ul><li>Optimization Level 5 </li></ul><ul><li>Avoid multiple bufferpools of the same page size </li></ul><ul><li>Configuration thumb rules </li></ul><ul><ul><li>BUFFPOOL ~= SHEAPTHRES </li></ul></ul><ul><ul><li>SORTHEAP ~= SHEAPTHRES/(# of concurrent SORT, HSJN) </li></ul></ul>
  24. 24. Registry Variables <ul><li>DB2_ANTIJOIN=EXTEND </li></ul><ul><ul><ul><li>If slow queries have NOT EXISTS, NOT IN predicates </li></ul></ul></ul>
  25. 25. Registry Variables <ul><li>DB2_REDUCED_OPTIMIZATION=YES </li></ul><ul><ul><li>Set if compile time is an issue </li></ul></ul><ul><li>IBM Service may recommend a more complex setting for example: </li></ul><ul><ul><li>DB2_REDUCED_OPTIMIZATION= 10 ,15,20 , 00011000…. </li></ul></ul><ul><ul><ul><li>First Part : DB2_REDUCED_OPTIMIZATION= A,B,C </li></ul></ul></ul><ul><ul><ul><ul><li>IF more than C joins, then &quot;quick greedy&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ELSE IF more than B joins, then use “greedy” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ELSE IF more than A joins, use reduced “dynamic” strategy. </li></ul></ul></ul></ul><ul><ul><ul><li>Second Part not documented (Mainly intended for setting by service) </li></ul></ul></ul>
  27. 27. Collecting Statistics <ul><li>The DB2 Query Optimizer relies on reasonably accurate statistics to get a good query plans </li></ul><ul><li>User runs RUNSTATS when data changes (part of ETL) </li></ul><ul><li>Statistics Fabrication (unreliable) </li></ul><ul><ul><li>DB2 keeps UPDATE / DELETE / INSERT counters </li></ul></ul><ul><ul><li>Fabrication limited to a few statistics – Not enough </li></ul></ul><ul><li>Automatic Statistics </li></ul><ul><ul><li>Automatically collects statistics on tables in need </li></ul></ul><ul><ul><li>Runs in the background as a low priority job </li></ul></ul><ul><li>Real Time Statistics </li></ul><ul><ul><li>Collects statistics on-the-fly </li></ul></ul>
  28. 28. AUTO RUNSTATS <ul><li>Set Under Automatic Table Maintenance hierarchy </li></ul><ul><ul><li>AUTO_RUNSTATS cannot be ON unless AUTO_TBL_MAINT is ON </li></ul></ul><ul><li>Automatic maintenance (AUTO_MAINT) = ON </li></ul><ul><li>Automatic database backup (AUTO_DB_BACKUP) = OFF </li></ul><ul><li>Automatic table maintenance (AUTO_TBL_MAINT) = ON </li></ul><ul><li>Automatic runstats (AUTO_RUNSTATS) = ON </li></ul><ul><li>Automatic statement statistics (AUTO_STMT_STATS) = OFF </li></ul><ul><li>Automatic statistics profiling (AUTO_STATS_PROF) = OFF </li></ul><ul><li>Automatic profile updates (AUTO_PROF_UPD) = OFF </li></ul><ul><li>Automatic reorganization (AUTO_REORG) = OFF </li></ul>
  29. 29. REAL TIME STATISTICS <ul><li>Set Under Automatic Table Maintenance hierarchy </li></ul><ul><ul><li>Real Time Statistics cannot be ON unless AUTO RUNSTATS is ON </li></ul></ul><ul><ul><li>AUTO_RUNSTATS cannot be ON unless AUTO_TBL_MAINT is ON </li></ul></ul><ul><li>Automatic maintenance (AUTO_MAINT) = ON </li></ul><ul><li>Automatic database backup (AUTO_DB_BACKUP) = OFF </li></ul><ul><li>Automatic table maintenance (AUTO_TBL_MAINT) = ON </li></ul><ul><li>Automatic runstats (AUTO_RUNSTATS) = ON </li></ul><ul><li>Automatic statement statistics (AUTO_STMT_STATS) = ON </li></ul><ul><li>Automatic statistics profiling (AUTO_STATS_PROF) = OFF </li></ul><ul><li>Automatic profile updates (AUTO_PROF_UPD) = OFF </li></ul><ul><li>Automatic reorganization (AUTO_REORG) = OFF </li></ul>
  30. 30. Best Practices – RUNSTATS <ul><li>Distribution Statistics </li></ul><ul><ul><li>Collect large Quantile Statistics for Date columns </li></ul></ul><ul><ul><li>Collect distribution statistics on columns used in predicates </li></ul></ul><ul><li>Index Statistics </li></ul><ul><ul><li>Do not collect DETAILED INDEX statistics . Use SAMPLED DETAILED INDEX statistics instead </li></ul></ul><ul><li>Avoid statistics on columns you know will never be used in predicates or GROUP BY columns </li></ul><ul><li>Use TABLESAMPLE option for very large tables and statistical views </li></ul><ul><li>Use RUNSTATS Profiles to store customized invocations </li></ul><ul><li>RUNSTATS with ATTACH ? </li></ul><ul><li>COMMIT immediately after RUNSTATS of each table </li></ul>
  31. 31. Collecting Statistics Automatic RUNSTATS Real Time Statistics SAMPLED DETAILED INDEX TABLESAMPLE Selective column statistic specification Use RUNSTATS PROFILES
  32. 32. Summary <ul><li>Tips and best practices to improve data warehouse query performance have been discussed. </li></ul><ul><ul><li>Database Design </li></ul></ul><ul><ul><li>Application Design </li></ul></ul><ul><ul><li>Configuration and Operations </li></ul></ul><ul><li>These include key considerations related to : </li></ul><ul><ul><li>Parallelism </li></ul></ul><ul><ul><li>Partitioning </li></ul></ul><ul><ul><li>Schema </li></ul></ul><ul><ul><li>Application queries </li></ul></ul><ul><ul><li>Configuration </li></ul></ul><ul><li>Session 2 will cover the Performance Layer </li></ul>
  33. 33. Disclaimer <ul><ul><li>© Copyright IBM Corporation [current year]. All rights reserved. </li></ul></ul><ul><ul><li>U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. </li></ul></ul><ul><ul><li>THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.  WHILE EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN ADDITION, THIS INFORMATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM WITHOUT NOTICE.  IBM SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION. NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, NOR SHALL HAVE THE EFFECT OF, CREATING ANY WARRANTIES OR REPRESENTATIONS FROM IBM (OR ITS SUPPLIERS OR LICENSORS), OR ALTERING THE TERMS AND CONDITIONS OF ANY AGREEMENT OR LICENSE GOVERNING THE USE OF IBM PRODUCTS AND/OR SOFTWARE. </li></ul></ul><ul><ul><li>Please update paragraph below for the particular product or family brand trademarks you mention such as WebSphere, DB2, Maximo, Clearcase, Lotus, etc </li></ul></ul><ul><li>IBM, the IBM logo,, [IBM Brand, if trademarked], and [IBM Product, if trademarked] are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at </li></ul><ul><li>If you have mentioned trademarks that are not from IBM, please update and add the following lines: </li></ul><ul><li>[Insert any special 3rd party trademark names/attributions here] </li></ul><ul><li>Other company, product, or service names may be trademarks or service marks of others. </li></ul>