java - Oracle does not use function-based index for regex_replace with bind variables -


i have table in oracle 11g database non formatted text in column, shall displayed entered. anyway, user shall able search text in possible format (regarding punctuation). fulfil requirement introducing function-based index on column:

create index "my_regex_index" on "my_table" (upper( regexp_replace ("my_column",'[:punct:]',''))) 

when query index

select * my_table upper(regexp_replace(,'[:punct:]', ''))='123' 

it works fine. execution plan contains proper usage of new (and highly selective) index.

but when use bind variables regex pattern, not work anymore.

select * my_table upper(regexp_replace(,:pattern, ''))='123' 

the pattern variable contains pattern constant, anyway, optimizer refuses usage of index. somehow, oracle's bind peeking not kick in.

unfortunately, have no possibility not use literals instead of bind variables, because application uses hibernate in order generate sql. hints didn't work out neither.

i no oracle expert - there way make oracle "understand" bind variables , use index? didn't find related topic in docs, except fact can use bind variables pattern.

any highly appreciated.

although doesn't answer why bind variable stops index being used, or why hint ignored (which i've verified, oracle free ignore hint suppose - hence name), take approach , use virtual column instead:

drop index my_regex_index;  alter table my_table add my_regexp_column generated   (upper(regexp_replace(my_column, '[[:punct:]]')));  create index my_regex_index on my_table (my_regexp_column); 

then query use index, there no bind worry about, , no (constant) value pass around:

select * my_table my_regexp_column = '123';  ---------------------------------------------------------------------------------------------- | id  | operation                   | name           | rows  | bytes | cost (%cpu)| time     | ---------------------------------------------------------------------------------------------- |   0 | select statement            |                |     1 |   204 |     2   (0)| 00:00:01 | |   1 |  table access index rowid| my_table       |     1 |   204 |     2   (0)| 00:00:01 | |*  2 |   index range scan          | my_regex_index |     1 |       |     1   (0)| 00:00:01 | ---------------------------------------------------------------------------------------------- 

of course, assumes can add columns, , hibernate handle properly. don't see why not, don't use it...


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -