Skip to Main Content
  • Questions
  • Performance of querying CHAR columns

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Peter.

Asked: August 22, 2017 - 1:50 pm UTC

Last updated: August 22, 2017 - 2:01 pm UTC

Version: 12c

Viewed 1000+ times

You Asked

I wasn't able to parameterize our queries because our database schema uses a lot of CHAR types on key fields, but was told to use the LIKE operator instead of =. This did allow the parameterized queries to work, but I'm concerned how this will effect performance?

and Chris said...

Ug. Chars :(

Assuming you mean you're not padding the variables with spaces and doing something like:

where char_col like :var || '%'


I'd be more concerned about correctness than performance! In general these won't return the same results:

create table t (
  x char(10)
);

insert into t values ('test');
insert into t values ('testtest');

var v varchar2(10);

exec :v := 'test';

select * from t
where  x = :v;

no rows selected

select * from t
where  x like :v || '%';

X         
----------
test      
testtest  

select * from t
where  x = cast(:v as char(10));

X         
----------
test 


You may still encounter issues with performance too though. The cardinality calculations are different for equalities (=) and like. So you may end up with different than using =. This isn't necessarily a bad thing. But it's not good either.

Assuming you can't get rid of the chars (easily), either pad your variables or cast them to the appropriate length. Then compare using =.

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

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.