0

I have a query below, may i please know what needs to be done to reduce execution time from 18sec

the query

SELECT DISTINCT ON (player_name) server_name, player_anme, logged_at, joined_at 
FROM player_cache 
WHERE server_name ILIKE '{query[0]}' 
AND logged_at > (now() - interval '{history} hour') 
ORDER BY player_name,logged_at desc;

The execution plan

Unique (cost=14326.21..14326.25 rows=7 width=82) (actual time=18907.017..18907.049 rows=32 loops=1)
  -> Sort (cost=14326.21..14326.23 rows=7 width=82) (actual time=18907.015..18907.023 rows=81 loops=1)
    Sort Key: player_name, logged_at DESC
    Sort Method: quicksort Memory: 36kB
  -> Index Scan using idx_logged_at on player_cache (cost=0.56..14326.11 rows=7 width=82) (actual time=5.605..18906.463 rows=81 loops=1)
    Index Cond: (logged_at > (now() - '30:00:00'::interval))
    Filter: ((server_name)::text ~~* 'Server-1'::text)
    Rows Removed by Filter: 79825
Planning Time: 0.665 ms
Execution Time: 18907.108 ms
3
  • 1
    Edit the question, if you got something to add. Don't use comments for that. Especially, if it's code you want to add, that's totally unreadable in comments. Commented Aug 22, 2021 at 16:48
  • An index on your server_name column could help. Also it seems that you don't have any pattern matching in your expression for ILIKE. In this answer you'll find some possibilities, if you really need ILIKE. Commented Aug 22, 2021 at 17:01
  • What is EXPLAIN (ANALYZE, BUFFERS) for the query? Turn track_io_timing on before running that if you can. Commented Aug 22, 2021 at 21:01

1 Answer 1

2

You can create a trigram index to speed up the ILIKE condition:

CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE INDEX ON player_cache USING gin (server_name gin_trgm_ops);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.