Looking more at the data yes it goes very often to ELSE Clause. And therefore reaching the MAX_CACHED_RES.
In there anyway to increase that value ?
Basically, I have several tables containing millions of rows and let say 5 columns. Those five columns, depending of their combination give me a 6th value. We have complex patterns to match and using simple LIKE / EQUAL and so on wouldn't be enough. This can be applied to N number of table so we refactored this process into a function that we can use in the SELECT statement, by giving only the 5 values each time.
I wouldn't mind using a table and mapping it through a join if it were for my own use. But the final query has to be readable and usable for almost-non-initiated SQL user... So using a function with encapsulated case when seemed to be a good idea and so far worked nicely.
But we might consider changing it if we have no other choice...
Rewrite the function in pl/perl and compare performance
Hierarchy of CASE statements allowing you to reduce the number of possibilities in exchange for manually pre-processing the batches on a significantly less complicated condition probably using only 1 or 2 columns instead of all five.
I'm not familiar with the caching constraint or the data so its hard to make more specific suggestions.