Coding for effective indexes: Avoid the use of like patterns beginning with % or _

The percent sign (%), and the underline (_), when used in the pattern of a LIKE (OPNQRYF %WLDCRD) predicate, specify a character string that is similar to the column value of rows you want to select. They can take advantage of indexes when used to denote characters in the middle or at the end of a character string, as in the following. For example, when using SQL, specify the following:

... WHERE LASTNAME LIKE 'J%SON%'

When using the OPNQRYF command, specify the following:

... QRYSLT('LASTNAME *EQ WLDCRD(''J*SON*'')')

However, when used at the beginning of a character string, they can prevent DB2 UDB for AS/400 from using any indexes that might be defined on the LASTNAME column to limit the number of rows scanned. For example, when using SQL, specify the following:

... WHERE LASTNAME LIKE '%SON'

When using the OPNQRYF command, specify the following:

... QRYSLT('LASTNAME *EQ WLDCRD(''*SON'')')

You should therefore avoid using these symbols at the beginning of character strings, especially if you are accessing a particularly large table.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]