You are on page 1of 3

2/27/2014

CDB Software, Inc.

CDB Global // CDB North America // CDB International Home News & Events Webcasts Press Releases White Papers & Articles About CDB CDB North America CDB International CDB Global Products & Solutions

Packages
Real Time DB2 Data Management Solution Real Time DB2 Database Maintenance Solution DB2 Index Management Solution DB2 Image Copy Solution DB2 Partition Management Solution DB2 Data Propagation Solution Product List Support Policies & Guidelines Product Compatibility Onsite Training News & Notifications Product Documentation Technical Bulletins PTFs & Libraries Online Training Videos My Profile Partners Become a Partner Meet Our Partners Contact Us Login

White Papers & Articles


4th Q uarte r 2011
http://www.cdbsoftware.com/whitepapers/cmnews-quarter4-2011.php 1/3

2/27/2014

CDB Software, Inc.

Which is better? BETWEEN versus <= AND >=


As all good DB2 developers know, there is frequently more than one way to write a SQL statement to return the same results. But the performance that is another issue. In this quarters tip, we take a look at BETWEEN and the equivalent combination of <= AND >=. The BETWEEN predicate is easier to understand and code than the equivalent combination of less than or equal to (<=) and the greater than or equal to (>=). In past releases, in many cases it was more efficient, too. But today, in most cases, the DB2 Optimizer recognizes the two formulations as equivalent and there usually is no performance benefit one way or the other. Performance reasons aside, one BETWEEN predicate is simpler to code and maintain than multiple <= and >= predicates. For this reason, I tend to favor using BETWEEN. but not always. Consider the scenario of comparing a host variable to two columns. Usually BETWEEN is used to compare one column to two values, here shown using host variables: WHERE COLUMN1 BETWEEN :HOST-VAR1 AND :HOST-VAR2 However, it is possible to use BETWEEN to compare one value to two columns, for example: WHERE :HOST-VAR BETWEEN COLUMN1 AND COLUMN2 This statement should be changed to: WHERE :HOST_VAR >= COLUMN1 AND :HOST-VAR <= COLUMN2 The reason for this exception is that a BETWEEN formulation comparing a host variable to two columns is a Stage 2 predicate, whereas the >= AND <= formulation is Stage 1. And we all know that Stage 1 outperforms Stage 2, right? Remember too, that SQL is flexible and often the same results can be achieved using different SQL formulations. Sometimes one SQL statement will dramatically outperform a functionally equivalent SQL statement just because it is indexable and the other is not. For example, consider this SQL statement: SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME FROM EMP WHERE MIDINIT NOT BETWEEN 'A' AND 'G'; It is not indexable because it uses the NOT BETWEEN predicate. However, if we understand the data in the table and the desired results, perhaps we can reformulate the SQL to use indexable predicates, such as: SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME FROM EMP WHERE MIDINIT >= 'H'; Or we could code MIDINIT BETWEEN 'H' AND 'Z' in place of MIDINIT >= 'H'. Of course, for either of these solutions to work correctly we would need to know that MIDINIT never contained values that collate lower than the value 'A'. So, as usual, there is no one size fits all answer to the question!
http://www.cdbsoftware.com/whitepapers/cmnews-quarter4-2011.php 2/3

2/27/2014

CDB Software, Inc.

Contribution by Craig Mullins

Craig S. Mullins, president and principal consultant of Mullins Consulting, Inc., is a data management strategist, researcher, and consultant. He has nearly three decades of experience in all facets of database systems development and has worked with mainframe DB2 since V1. You may know Craig from his popular books: DB2 Developers Guide (with over 1500 pages of in-depth technical information on DB2 for z/OS) and Database Administration: The Complete Guide to Practices and Procedures (the industrys only comprehensive guide to heterogeneous database administration). Craig can be reached via his website at www.craigsmullins.com. return to white papers Press Releases White Papers & Articles Press Contacts About CDB Home News & Events About CDB Products & Solutions Support Partners Contact Us Login Copyright 2012-2014 CDB Software All Rights Reserved. | Legal Disclaimer
CDB Software, Inc.

Toll-Free : 800-627-6561 Phone : 713-588-1778 Fax: 800-627-4232 info@cdbsoftware.com

http://www.cdbsoftware.com/whitepapers/cmnews-quarter4-2011.php

3/3

You might also like