[Solved] Using function based index (oracle) to speed up count(X)

EverSQL Database Performance Knowledge Base

Using function based index (oracle) to speed up count(X)

Database type:

I've a table Film:

CREATE TABLE film (
film_id NUMBER(5) NOT NULL,
title varchar2(255));

And I wanted to make the query, which counts how many titles start with the same word and only displays ones with more than 20, faster using a function based index. The query:

SELECT FW_SEPARATOR.FIRST_WORD AS "First Word", COUNT(FW_SEPARATOR.FIRST_WORD) AS "Count"
FROM (SELECT regexp_replace(FILM.TITLE, '(\w+).*$','\1') AS FIRST_WORD FROM FILM) FW_SEPARATOR
GROUP BY FW_SEPARATOR.FIRST_WORD
HAVING COUNT(FW_SEPARATOR.FIRST_WORD) >= 20;

The thing is, I created this function based index:

CREATE INDEX FIRST_WORD_INDEX ON FILM(regexp_replace(TITLE, '(\w+).*$','\1'));

But it didn't speed anything up...

I was wondering if anyone could help me with this :)

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 Subqueries (query line: 5): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Push Filtering Conditions Into Subqueries (modified query below): Parts of the WHERE clause can pushed from the outer query to a subquery / union clause. Applying those conditions as early as possible will allow the database to scan less data and run the query more efficiently.
The optimized query:
SELECT
        FW_SEPARATOR.FIRST_WORD AS "First Word",
        COUNT(FW_SEPARATOR.FIRST_WORD) AS "Count" 
    FROM
        (SELECT
            regexp_replace(FILM.TITLE,
            '(\w+).*$',
            '\1') AS FIRST_WORD 
        FROM
            FILM 
        HAVING
            (
                COUNT(FIRST_WORD) >= 20
            )) FW_SEPARATOR 
    GROUP BY
        FW_SEPARATOR.FIRST_WORD 
    HAVING
        COUNT(FW_SEPARATOR.FIRST_WORD) >= 20

Related Articles



* original question posted on StackOverflow here.