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强制优化器的使用技的介绍,望你能有所收获。

【编辑推荐】

  1. 对DB2数据库编目概念正确理解
  2. DB2 物化查询表以及其他常用表的介绍
  3. DB2数据库SQL编码优化与性能描述
  4. DB2数据库开发与PostgreSQL开发中的异同点
  5. DB2用户密码修改与备份恢复的正确应用

     
责任编辑:佚名 来源: 比特网
相关推荐

2010-08-02 11:01:29

DB2 Resotre

2010-08-18 11:36:40

DB2简史

2010-08-18 15:01:08

DB2 9安装方法

2010-08-19 13:36:11

DB2数据类型

2010-07-30 17:33:26

2010-08-12 15:16:50

DB2在线备份

2010-08-20 09:38:53

DB2 常见问题解答

2010-08-03 08:46:23

DB2 9.5高可用性

2010-08-09 15:31:03

2010-05-26 15:58:52

MySQL远程连接

2010-05-10 13:01:03

OracleDBA面试

2010-05-25 09:58:43

MySQL数据库

2010-05-21 09:40:57

MySQL出错代码列表

2010-09-28 09:42:16

2010-07-23 18:39:52

SQL Server游

2014-10-30 13:38:55

编程算法程序员

2019-12-10 15:30:27

SaaSIaaS云计算

2010-04-21 17:19:29

Oracle创建

2017-05-17 14:46:22

容器DockerLinux

2010-05-26 13:14:22

MySQL错误解决方案
点赞
收藏

51CTO技术栈公众号