DB2强制优化器的使用“窍门”不得不看


  本文标签:DB2强制优化器

  以下的文章主要描述的是在实际操作中DB2强制优化器的使用技巧,很多开发与数据库管理人员都在为优化器问题发牢骚  。尽管很多时候优化器问题一般都是可以通过常规手段解决的,但是在某些特殊情况下  。

  或者紧急情况(没有时间完整地分析问题)下,用户可以使用profile暂时强制优化器使用某些特定的操作  。  。  。

  下面是一个step by step的例子,简单地说明了怎样DB2强制优化器使用table scan

  1. DB21085I Instance "DB2" uses "32" bits and DB2 code release "SQL09010" with   
  2. level identifier "02010107".   
  3. Informational tokens are "DB2 v9.1.0.356", "s060629", "NT32", and Fix Pack "0".   
  4. Product is installed at "D:\PROGRA'1\IBM\SQLLIB\" with DB2 Copy Name   
  5. "DB2COPY1".  

  创建一个数据

  1. D:\TEMP\db2service.perf1>db2 create db sampel2DB20000I The CREATE DATABASE command completed successfully.   
  2. D:\TEMP\db2service.perf1>db2 connect to sampel2   
  3. Database Connection Information   
  4. Database server = DB2/NT 9.1.0   
  5. SQL authorization ID = TAOEWANG   
  6. Local database alias = SAMPEL2  

  创建优化器系统表

  1. D:\TEMP\db2service.perf1>db2 "create table systools.opt_profile (schema VARCHAR(128) not null, name varchar(128) 
    not null, profile blob (2M) not null, primary key (schema, name))"   
  2. DB20000I The SQL command completed successfully.   
  3. D:\TEMP\db2service.perf1>cd ..  

  创建用户表

  1. D:\TEMP>db2 "create table mytable (name varchar(128), id integer, salary float,phone varchar(20))"   
  2. DB20000I The SQL command completed successfully.  

  插入一些数据

  1. D:\TEMP>db2 "insert into mytable values (tao wang, 12345, 100, 123-456)"   
  2. DB20000I The SQL command completed successfully.   
  3. D:\TEMP>db2 "insert into mytable values (diablo2, 12346, 101, 123-457)"   
  4. DB20000I The SQL command completed successfully.   
  5. D:\TEMP>db2 "insert into mytable values (whiterain, 123, 102, 123-458)"   
  6. DB20000I The SQL command completed successfully.   
  7. D:\TEMP>db2 "insert into mytable values (ganquan, 1255, 104, 123-459)"   
  8. DB20000I The SQL command completed successfully.  

  DB2强制优化器的使用

  下面是一个step by step的例子,简单地说明了怎样强制优化器使用table scan

  1. DB21085I Instance "DB2" uses "32" bits and DB2 code release "SQL09010" with   
  2. level identifier "02010107".   
  3. Informational tokens are "DB2 v9.1.0.356", "s060629", "NT32", and Fix Pack "0".   
  4. Product is installed at "D:\PROGRA'1\IBM\SQLLIB\" with DB2 Copy Name   
  5. "DB2COPY1".  

  创建一个数据库

  1. D:\TEMP\db2service.perf1>db2 create db sampel2DB20000I The CREATE DATABASE command completed successfully.   
  2. D:\TEMP\db2service.perf1>db2 connect to sampel2   
  3. Database Connection Information   
  4. Database server = DB2/NT 9.1.0   
  5. SQL authorization ID = TAOEWANG   
  6. Local database alias = SAMPEL2  

  创建优化器系统表

  1. D:\TEMP\db2service.perf1>db2 "create table systools.opt_profile (schema VARCHAR(128) not null, 
    name varchar(128) not null, profile blob (2M) not null, primary key (schema, name))"   
  2. DB20000I The SQL command completed successfully.   
  3. D:\TEMP\db2service.perf1>cd ..  

  创建用户表

  1. D:\TEMP>db2 "create table mytable (name varchar(128), id integer, salary float,phone varchar(20))"   
  2. DB20000I The SQL command completed successfully.  

  插入一些数据

  1. D:\TEMP>db2 "insert into mytable values (tao wang, 12345, 100, 123-456)"   
  2. DB20000I The SQL command completed successfully.   
  3. D:\TEMP>db2 "insert into mytable values (diablo2, 12346, 101, 123-457)"   
  4. DB20000I The SQL command completed successfully.   
  5. D:\TEMP>db2 "insert into mytable values (whiterain, 123, 102, 123-458)"   
  6. DB20000I The SQL command completed successfully.   
  7. D:\TEMP>db2 "insert into mytable values (ganquan, 1255, 104, 123-459)"   
  8. DB20000I The SQL command completed successfully.  

  现在优化器用了index scan
 

  1. Original Statement:   
  2. ------------------   
  3. SELECT *   
  4. FROM TAOEWANG.MYTABLE   
  5. WHERE ID < 1000   
  6. Optimized Statement:   
  7. -------------------   
  8. SELECT Q1.NAME AS "NAME", Q1.ID AS "ID", Q1.SALARY AS "SALARY", Q1.PHONE AS   
  9. "PHONE"   
  10. FROM TAOEWANG.MYTABLE AS Q1   
  11. WHERE (Q1.ID < 1000)   
  12. Access Plan:   
  13. -----------   
  14. Total Cost: 7.56853   
  15. Query Degree: 1   
  16. Rows   
  17. RETURN   
  18. ( 1)   
  19. Cost   
  20. I/O   
  21. |   
  22. 1   
  23. FETCH   
  24. ( 2)   
  25. 7.56853   
  26. 1   
  27. /----+---\   
  28. 1 4   
  29. IXSCAN TABLE: TAOEWANG   
  30. ( 3) MYTABLE   
  31. 0.00630865   
  32. 0   
  33. |   
  34. 4   
  35. INDEX: TAOEWANG   
  36. IX1  

  以上的相关内容就是对DB2强制优化器的使用技的介绍,望你能有所收获  。