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. 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 Universal Database™ for iSeries™ from using any indexes that might be defined on the LASTNAME column to limit the number of rows scanned using index scan-key positioning. Index scan-key selection, however, is allowed. For example, in the following queries index scan-key selection can be used, but index scan-key positioning cannot. In SQL:
… WHERE LASTNAME LIKE '%SON'In OPNQRYF:
… QRYSLT('LASTNAME *EQ %WLDCRD(''*SON'')')Ideally, you should avoid patterns with a % so that you can get the best performance when you perform key processing on the predicate. If possible, you should try to get a partial string to search so that index scan-key positioning can be used.
For example, if you were looking for the name "Smithers", but you only type "S%," this query returns all names starting with "S." You should adjust the query to return all names with "Smi%". By forcing the use of partial strings, you may get better performance in the long term.
Parent topic:
Coding for effective indexes