DB2 force optimizer usage tips have to be viewed

Source: Internet
Author: User

The following articles mainly describe how to use the DB2 force optimizer in practice. Many developers and database managers complain about the optimizer problem. In many cases, optimizer problems can be solved by conventional means, but in some special cases.

Or in an emergency (no time for complete problem analysis), you can use the profile to temporarily force the optimizer to use some specific operations...

The following is an example of step by step, which briefly describes how DB2 forces the optimizer to use 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".  

Create a database

 
 
  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  

Create optimizer system table

 
 
  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 ..  

Create user table

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

Insert some data

 
 
  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.  

Use of DB2 force Optimizer

The following example shows how to force the optimizer to use 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".  

Create a database

 
 
  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  

Create optimizer system table

 
 
  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 ..  

Create user table

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

Insert some data

 
 
  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.  

Now the optimizer uses 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  

The above content is an introduction to the use technology of the DB2 force optimizer. I hope you will have some gains.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.