Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between INSTR and LIKE in Oracle?

Can some one tell me the difference between INSTR and LIKE in Oracle?

Which one is faster in Oracle10g?

like image 943
Naveenraja Subramaniam Avatar asked Dec 27 '11 16:12

Naveenraja Subramaniam


People also ask

Which is faster like or Instr?

They are both case-insensitive, and generally they perform full-table scans, a general no-no when dealing with high-performance MySQL. In which case, the LIKE with only a suffix wildcard is much faster. massive improvement and so obvious thinking about it.

What is difference between Instr and Substr in Oracle?

Answers. The substr functions allows you to extract a substring from a string. The instr function returns the location of a substring in a string.

What is use of Instr in Oracle?

Purpose. The INSTR functions search string for substring . The function returns an integer indicating the position of the character in string that is the first character of this occurrence. INSTR calculates strings using characters as defined by the input character set.

What is substr () Instr () functions?

INSTR() helps to find if a substring is present in the original string. If a string is present, it gives the position of the substring in the original string from the indexed position. The number of occurrences of the substring is also returned as output.


4 Answers

That depends on the data and on the pattern. If you use like 'a%', then Oracle can use a BTree indexes to look up the matches because it can search the btree with the start of the pattern and then consider only the subtree.

This doesn't work for LIKE '%a' but you can work around this by creating a calculated column which reverses all values from the column you want to search (so you get the pattern above).

If you use hashed indexes, there is little that Oracle can do but scan the whole index. It might sill be faster when there are only few different values.

I'm not sure whether INSTR can ever use an index because it has no fixed anchor.

So like with all performance questions:

  1. Fill the database with some realistic test data and run some tests.
  2. Always write your code in a way that it can be optimized easily later, when you know about the bottlenecks
  3. Never guess what might be slow. You'll be wrong 90% of the time. Always measure.
like image 72
Aaron Digulla Avatar answered Oct 19 '22 22:10

Aaron Digulla


  • INSTR searches for a string inside another string, with options for direction, starting positions etc
  • LIKE is simple pattern matching and standard SQL

Neither will be "faster" consistently because they aren't comparable: it depends what you need to do, your data, how you search etc

like image 25
gbn Avatar answered Oct 20 '22 00:10

gbn


INSTR is a oracle function since Oracle 8i for finding a substring, and LIKE is an SQL condition mainly used for matching string with wildcards.

I expect INSTR to be a bit faster since it's less complex but I didn't measure it.

like image 43
Michał Šrajer Avatar answered Oct 20 '22 00:10

Michał Šrajer


This page says that INSTR is faster:

http://oracle.veryoo.com/2013/03/performance-comparation-between-like.html

EDIT: Archive link since the above is no longer available: http://web.archive.org/web/20160301212009/http://oracle.veryoo.com/2013/03/performance-comparation-between-like.html

Also on SO for MySql this answer says that INSTR is faster.

like image 31
Roland Avatar answered Oct 19 '22 23:10

Roland