[Solved] Oracle REGEXP confusion
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Oracle REGEXP confusion

Database type:

Question: Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.

Solution I tried but failed (giving incorrect output):

SELECT DISTINCT city FROM station
WHERE NOT REGEXP_LIKE(city, '^(a|e|i|o|u).*(a|e|i|o|u)$','i');

Solution which worked:

SELECT DISTINCT city FROM station
WHERE REGEXP_LIKE(city, ^[^aeiou].*[^aeiou]$,'i');

Please can anyone explain why the first solution is failing.

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: 6): 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 `city` is indexed, the index won’t be used as it’s wrapped with the function `REGEXP_LIKE`. 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
        DISTINCT station.city 
    FROM
        station 
    WHERE
        NOT REGEXP_LIKE(station.city, '^(a|e|i|o|u).*(a|e|i|o|u)$', 'i')

Related Articles



* original question posted on StackOverflow here.