Skip to Main Content
  • Questions
  • The fastest way to compare string to string

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Ivan.

Asked: August 04, 2010 - 3:07 am UTC

Last updated: August 05, 2010 - 8:32 am UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

Hi Tom!

I don't have any sample data for my question because it's too broad. I've tried to find similar questions but it's somewhat hard to formulate it as a search query.

So, which way to compare strings in Oracle (comparing string to string) is preferable - using equality test (=) or using LIKE?
I.e. "a = 'qwerty'" or "a like 'qwerty'"?
Which way will implement faster - and why? Or maybe there aren't any difference?

Also, if it works differently depending on the version of Oracle, those differences are also interesting.

Thanks.

and Tom said...

if you use literals as you did in your problem statement - the optimizer realizes that column like 'literal without percent or underscore' is really equals and does that, consider:

ops$tkyte%ORA11GR2> create table t as select * from all_users;

Table created.

ops$tkyte%ORA11GR2>
ops$tkyte%ORA11GR2> set autotrace traceonly explain
ops$tkyte%ORA11GR2> select * from t where username = 'abc' or username like 'def';

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    39 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    39 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("USERNAME"='abc' OR "USERNAME"='def')


see the username like 'def' was rewritten. However, if you have the CHANCE of having % or _ in there - via a bind variable for example:

ops$tkyte%ORA11GR2> variable x varchar2(20)
ops$tkyte%ORA11GR2> variable y varchar2(20)
ops$tkyte%ORA11GR2> set autotrace traceonly explain
ops$tkyte%ORA11GR2> select * from t where username = :x or username like :y;

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    33 |  1287 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |    33 |  1287 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("USERNAME" LIKE :Y OR "USERNAME"=:X)


then we have to assume you really need the extra processing that LIKE entails and we'll do the LIKE processing.


If you mean equals - use equals.

If you mean like - use like.


that should be the ONLY deciding factor. Which one do you NEED to use, which one did you MEAN to use, which one makes the most sense.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.