Kwissle

My real-time quiz battle game Kwissle.com

Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Photos

Photoalbum, both old and new.

Twitter

Follow me on Twitter

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 
Web development

UPPER vs. ILIKE


19th of April 2010

I have a Zope web app that uses hand coded SQL (PostgreSQL) statements. Similar to the old PHP. My only excuse for not using an ORM was that this project started in 2005 and at the time SQLAlchemy seemed like a nerdy mess with more undocumented quirks than it was worth losing hair over.

Anyway, several statements use ILIKE to get around the problem of making things case insensitive. Something like the Django ORM uses UPPER to get around it. So I wonder how much the ILIKE statement slows down compared to UPPER and the indexed equal operator. Obviously, neither ILIKE or UPPER can use an index.

Long story short, here are the numbers for selecting on about 10,000 index records:

 # ONE
 EXPLAIN ANALYZE SELECT ... FROM ... WHERE name = 'Abbaye';
 Average: 0.14 milliseconds

 # TWO
 EXPLAIN ANALYZE SELECT ... FROM ... WHERE  UPPER(name::text) = UPPER('Abbaye');
 Average: 18.89 milliseconds

 # THREE
 EXPLAIN ANALYZE SELECT ... FROM ... WHERE  name ILIKE 'Abbaye';
 Average: 24.63 milliseconds

UPPER vs. ILIKE

First of all, the conclusion is to use UPPER instead of ILIKE if you don't need to do regular expressions. Secondly, if at all possible try to use the equal operator first and foremost and only reside on the case insensitive one if you really need to.

Lastly, in PostgreSQL is there a quick and easy to use drop in alternative to make an equality operatorish thing for varchars that are case insensitive?



Comment

Ivo van der Wijk - 19th April 2010  [«« Reply to this]
FYI UPPER can use an index if you create the index to be uppercase, i.e.

CREATE INDEX foo on bar (upper(col));

after which a "select * from bar where upper(col) = "PETER"; should be alot faster.
Peter Bengtsson - 21st April 2010   [«« Reply to this]
Really?! I sort of not surprised. PostgreSQL rocks! I'll throw that in if and when I replace my ILIKE statements (that don't use wildcards) with UPPER ones.
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.