[Solved] Why does column = upper(column) work in Oracle but not in MySQL?
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Why does column = upper(column) work in Oracle but not in MySQL?

Database type:

In mySQL I have to use WHERE BINARY UPPER(col1) = col1 but in Oracle it works with no problem. Can someone explain to me why this is? I am new to mySQL.

So looking at it with the following statement

select upper(col1),col1
from mytable
where upper(col1) = col1;

Result in Oracle:

UPPER(COL1)          COL1               
-------------------- --------------------
JÜRGEN               JÜRGEN               
RENÉ                 RENÉ                 
CARL                 CARL 

Result in MySQL

UPPER(COL1)          COL1               
-------------------- --------------------
JÜRGEN               JÜRGEN               
JÜRGEN               Jürgen               
RENÉ                 René                 
RENÉ                 RENÉ                 
CARL                 Carl                 
CARL                 CARL 

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Calling Functions With Indexed Columns (query line: 7): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `col1` is indexed, the index won’t be used as it’s wrapped with the function `upper`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
The optimized query:
SELECT
        upper(mytable.col1),
        mytable.col1 
    FROM
        mytable 
    WHERE
        upper(mytable.col1) = mytable.col1

Related Articles



* original question posted on StackOverflow here.