Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Using a SELECTIVITY clause to influence the optimizer

Paul Yip (ypaul@ca.ibm.com), DB2 Partner Enablement, IBM Toronto
Paul Yip is a consultant from the IBM Toronto Labs where the core of DB2 for distributed platforms is developed. His primary role is to help IBM business partners migrate from competitive RDBMS platforms to DB2 and accelerate their time to market. He has written several articles for DB2 Developer Domain and recently co-authored the book DB2 SQL Procedural Language for Linux, UNIX, and Windows . He can be reached at ypaul@ca.ibm.com.

Summary:  This article explains how you can influence the DB2 SQL optimizer by providing the expected selectivity using a selectivity clause.

Date:  04 Dec 2003
Level:  Introductory

Activity:  8075 views
Comments:  

Acknowledgements:

  • John Hornibrook
  • Samir Kapoor

Introduction

This article is written for DB2® Universal DatabaseTM version 8.1 for Linux®, UNIX®, and Windows®.

Since I participate actively in the art of Oracle conversions to DB2, I am frequently asked if DB2 supports SQL hints like Oracle. The short answer is no.

Here is a longer answer:

At IBM®, we have a different philosophy regarding SQL optimization. If DB2 UDB does not choose the optimal access plan and the poor access plan is not due to a limitation inherent in the query, we consider it a defect in the product and prefer to fix the problem at the source so that all DB2 users may benefit as well. Hence, you should find that there is less need for hints in DB2 to begin with. For other cases, where limitations inherent in the query make proper access plan selection difficult, you can influence the DB2 SQL optimizer by providing additional selectivity information.

For example, consider the following SQL statement:


SELECT * FROM T1 where col1 >= ?

If the values of col1 in table T1 ranged from 0 to 100, the value provided through the parameter marker can greatly vary the actual selectivity of the predicate. That is, if the values stored in col1 were evenly distributed between 0 and 100, the number of rows which meet the requirements of the WHERE clause would vary greatly if the parameter marker value were 10 instead of 90.

However, you can influence the optimizer by providing the expected selectivity using a selectivity clause. It can be useful in situations where you want to encourage (or discourage) DB2 to use certain indexes. Using DFT_QUERYOPT=5 (the default setting) seems to work most predictably with this feature. To enable selectivity clauses, you must first set the registry variable DB2_SELECTIVITY=YES and restart the instance.


db2set DB2_SELECTIVITY=YES
	    db2 force application all   (to kick off all connected users)
	    db2stop
	    db2start

The SELECTIVITY clause can only be used with basic predicates (as defined in the SQL reference), not predicates such as LIKE or BETWEEN. A lower selectivity value (very small number) will tell DB2 that the predicate will qualify fewer rows (and encourage use of indexes defined on that column). A higher selectivity value (close to 1) will mean the opposite. Example:


SELECT c1, c2, c3, FROM T1, T2, T3
	    WHERE T1.x = T2.x AND
	    T2.y=T3.y AND
	    T1.x >= ? selectivity 0.00001 AND
	    T2.y gt; ? selectivity 0.5 AND
	    T3.z = ? selectivity 0.2 AND
	    T3.w = ?

Example of SELECTIVITY in action

Notes:

  • The EMPLOYEE and DEPARTMENT tables are provided in the SAMPLE database provided with DB2.
  • The index DEPTNOIDX on column DEPTNO of table department was created to illustrate this example.

Tip:

Use of parameter markers is allowed in DB2’s Visual Explain utility.

Here is the original query:


SELECT * FROM EMPLOYEE e, DEPARTMENT d 
	    WHERE e.workdept = d.deptno 
	    AND d.deptno = ?


Here is the same query modified with different selectivity clauses:


SELECT * FROM EMPLOYEE e, DEPARTMENT d 
	    WHERE e.workdept = d.deptno 
	    AND d.deptno = ? selectivity 0.9
	    
	    
	    
	    SELECT * FROM EMPLOYEE e, DEPARTMENT d 
	    WHERE e.workdept = d.deptno 
	    AND d.deptno = ? selectivity 0.25


Now, compare the output of visual explain. Notice how we were able to influenced use of index.



Figure 1. With selectivity of 0.9 (lower selectivity)
Image1




Figure 2. With selectivity of 0.25 (higher selectivity)
Image2

Use of the selectivity clause should be considered a last resort. Before using it:

  • Experiment with different SQL optimization classes. The default optimization class is controlled by the DFT_QUERYOPT parameter in the database configuration file.
  • Attempt to resolve any performance problems by ensuring that proper database statistics have been collected. The more detailed the statistics, the better the optimizer can perform. (See RUNSTATS in the DB2 Command Reference).
  • If the poor access plan is the result of rapidly changing characteristics of the table (i.e. grows very quickly such that statistics get out of date quickly), try marking the table as VOLATILE using the ALTER TABLE command.
  • Try explaining the query using literal values instead of parameter markers in your predicates. If you are getting different access plans when using parameter markers, it will help you understand the nature of the performance problem better. You may find that using literals in your application will yield a better plan (and therefore better performance) at the cost of SQL compilation overhead.
  • Try using DB2’s index advisor (db2advis) to see if there are any useful indexes which you may have overlooked.


Resources

Learn

Get products and technologies

  • Build your next development project with IBM trial software, available for download directly from developerWorks.

Discuss

About the author

Paul Yip is a consultant from the IBM Toronto Labs where the core of DB2 for distributed platforms is developed. His primary role is to help IBM business partners migrate from competitive RDBMS platforms to DB2 and accelerate their time to market. He has written several articles for DB2 Developer Domain and recently co-authored the book DB2 SQL Procedural Language for Linux, UNIX, and Windows . He can be reached at ypaul@ca.ibm.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management
ArticleID=223508
ArticleTitle=Using a SELECTIVITY clause to influence the optimizer
publish-date=12042003

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers