sql - Using index on `LIKE :varname || '%'` in firebird -
i have query
select distinct fkdocument pntm_documents_ft_index word 'sometext%' plan sort ((pntm_documents_ft_index index (ix_pntm_documents_ft_index)))
and works okay.
but when try use concatenated string like, firebird does not use indicies:
select distinct fkdocument pntm_documents_ft_index word 'sometext' || '%' plan sort ((pntm_documents_ft_index natural))
how force use indicies?
the short answer, ain commented, use starting [with] instead of like if don't need pattern, want prefix search. so:
where word starting 'sometext' -- no %! or
where word starting :param as far know firebird like 'sometext%'. use index when available, , don't need escape presence of pattern symbols. downside can't use pattern symbols.
now why firebird doesn't use index when use
where word :param || '%' -- (or :param) matter or
where word 'sometext' || '%' the first case explained: statement preparation done separately execution. firebird needs take account possibility parameter value starts _ or - worse - %, , can't use index that.
as second case, should possible optimize equivalent of like 'sometext%', firebird considers not plain literal not optimizable. specific example possible decide should optimizable, specific exception (usually 1 doesn't concatenate literals this, of time 1 or more 'black' boxes columns, functions, case statements etc involved).
Comments
Post a Comment