A table I'm using has a char(19) column let's call it P. Due to circumstances in some wheres I need to check if a 10 character variable is found in P (at the end of the string in P). Thus I'm doing: where P like CONCAT('%',variableName).
The performance is accordingly bad. A possible solution is now the introduction of an automatically updated/calculated column let's call it P10 with char(10). Then I can use where P10 like variableName which is way better. (especially when it comes to indexes).
Now the question is what is performancewise the better method (I'm not finding any info on that)? -Alter TABLE MyTable add column P10 char(10) GENERATED ALWAYS AS substr(P,9,10) -Or a trigger event that fills P10 with a substr from P
If you are sure column
Pis always the full 19 chars long and the search values is always 10 chars long you can index the search expression and search using it (typing it exactly as you indexed it). For example:See running example at db<>fiddle.
Note that
substr(p, 10, 10)is typed the exact same way to the letter in the SELECT.Alternatively, if you want to search on the last chars of a string, the generic solution is to index the reversed value and then use the reverse pattern to search. This way the
%will be at the end of the search pattern and the engine will use the index naturally.