right now I have the following query in a stored procedure.
select * from table A where A.indexed_char_column LIKE :param1
:param1 can contain any type of wildcharacters, but sometimes it might not. ‘%abcdef%’ usually wants a full table scan and ‘abcdef’ should do a index range scan. So I would like the SQL engine to use two different execution plans depending on this parameter.
Is there anyway I can make this behaviour possible? I would like answers for both Oracle 11gr2 and SQL Server 2005. I’m thinking if I could include a dummy parameter in the query (like a comment or something) that makes the SQL engine think of the two queries, that are actually identical, not to be identical.
select /* use table scan */ * from table A where A.indexed_char_column LIKE :param1
select /* use index */ * from table A where A.indexed_char_column LIKE :param1
But I don’t know how to accomplish this? Any other suggestions? Should I just use two stored procedures?
If you have the ability to change the hint/comment according to whether the value contains wildcards or not, why not change the statement instead?