[Solved] Postgresql 13, why aren\'t these indexes working at all? I\'m using pg_trgm correctly?

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the 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: 19): 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 `last_name` is indexed, the index won’t be used as it’s wrapped with the function `lower`. 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.
  2. Avoid Calling Functions With Indexed Columns (query line: 19): 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 `first_name` is indexed, the index won’t be used as it’s wrapped with the function `lower`. 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.
  3. Avoid Calling Functions With Indexed Columns (query line: 31): 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 `code` is indexed, the index won’t be used as it’s wrapped with the function `lower`. 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.
  4. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  5. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  6. Use Equality Operator Over LIKE (modified query below): Equality operators (such as '\u003d') are usually better optimized and more readable. Prefer the equality operator when searching for a constant value such as `lushijo`.
  7. Use UNION ALL instead of UNION (query line: 22): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
CREATE INDEX players_idx_team_id ON "players" ("team_id");
The optimized query:
SELECT
        players_id,
        players_first_name,
        players_last_name,
        players_code 
    FROM
        ((SELECT
            "players"."id" AS players_id,
            "players"."first_name" AS players_first_name,
            "players"."last_name" AS players_last_name,
            "players"."code" AS players_code 
        FROM
            "players" 
        WHERE
            (
                "players".team_id = 3
            ) 
            AND (
                lower(replace("players".last_name || "players".first_name, ' ', '')) LIKE '%lushijo%'
            ) LIMIT 15) 
    UNION
    DISTINCT (SELECT
        "players"."id" AS players_id,
        "players"."first_name" AS players_first_name,
        "players"."last_name" AS players_last_name,
        "players"."code" AS players_code 
    FROM
        "players" 
    WHERE
        ("players".team_id = 3) 
        AND (lower("players".code) = 'lushijo') LIMIT 15)
) AS union1 LIMIT 15

Related Articles



* original question posted on StackOverflow here.