DB2 for z/OS version 9 brought in a new feature called "Index on Expression". Yes, you can create indexes on frequently used expressions in your queries. For example, if you often do a SELECT FIRST_NAME WHERE UPPER(LAST_NAME) = 'SMITH', then creating an index on UPPER(LAST_NAME) will be useful.
Until V8, you can create index on just columns, not on expressions/functions etc. So, you would have created an index on LAST_NAME, but DB2 would have ignored it because when you use a function on a column, DB2 ignores index access. But starting in V9, you can create an index on UPPER(LAST_NAME) and DB2 will use this index.
Now, to the tricky part ...
I tried to create an index on UPPER(LAST_NAME), DB2 complained that I need to specify 'locale' name. After reading a little bit about 'locale names', I specified UPPER(LAST_NAME, 'En_US') and DB2 happily created the index. I also ran runstats on the table. However, when I did an explain on the query SELECT FIRST_NAME WHERE UPPER(LAST_NAME) = 'SMITH', DB2 didn't seem to pick up the newly created index. That puzzled me.
After reading a little further, I found that I need to specify the same 'locale' in the query too. After specifying the 'locale', I did an explain and DB2 picked up the index. This is the modified query:
SELECT FIRST_NAME WHERE UPPER(LAST_NAME, 'EN_US') = 'SMITH'
Keywords: DB2, z/OS, Index, Expression, Upper, Locale, function
4 comments:
Great tip! I was hitting a road block on this as well. Kuddos!
Great tip. This was tripping me up too.
I am trying this, but a runstats indicates that I do not have access to TABLE 'uppercase_index name'.
Any ideas?
"I do not have access to TABLE 'uppercase_index name'.
Any ideas?" <-- What is the error message? Can you post the error message here?
Post a Comment