1

I'm trying to optimizing an Oracle Query that looks like this:

select * from upcTable where upc like '%567%'

The query is fast for small search terms like 567, however it can get as long as 15 digits in which it can take ~1.5 seconds. Is there anyway to speed this up?

I've tried doing something like

select * from
(select * from rldb.productmaster where upc like '%567%')
where upc like '%380%'

select * from rldb.productmaster where upc like '%567380%'

In this case, the number of terms if about the same, but the first example is a bit faster. However when using the same technique for 15 digits, it's a bit faster but still too slow.

1
  • 1
    There's no reason why '%567%' would be faster than '%567380%' to return all rows (as it has to scan the same volume of source data). It might seem faster because the short the string to match the more chance it has of finding matches so it will return the first matches earlier. If you were going through a telephone book looking for people with a 'J' in their name, you'd get a list of 100 quicker than if you were searching for ones with a 'JU' in their name. Commented Jun 16, 2011 at 0:27

2 Answers 2

5

If you index the column like this:

create index upcIndex on upcTable (upc, pk);

(where pk is the primary key of the table)

then this query can perform an INDEX FAST FULL SCAN on that index:

select pk from upcTable where upc like '%567380%';

If you really need all the columns (select *) then you could try this:

select * from upcTable where pk in
(select pk from upcTable where upc like '%567380%');
Sign up to request clarification or add additional context in comments.

Comments

1

you won't be using any indexes doing a '%123%' (leading %). If you can search using '123%' then you can take advantage of an index.

2 Comments

This is true but I have to use the two %. The user won't know the UPC completely, so this function is meant to add an "autocomplete" The item UPC might by 55000 but in the database it is stored as 00055000
without any other restrictions, you're basically letting the user search for any string of any length in the field, so the db will need to perform a full scan to determine matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.