I have the following somewhat complex SELECT
statement with multiple joins, a group by and an order by:
SELECT
COUNT(*) AS count_all,
"response_variables"."id",
"response_variables"."var_name" AS "response_variables_id_response_variables_var_name"
FROM "response_variables"
INNER JOIN "responses" ON "responses"."id" = "response_variables"."response_id"
INNER JOIN "questions" ON "questions"."id" = "responses"."question_id"
WHERE "questions"."key" = 'rbmmpmvs'
GROUP BY "response_variables"."id", "response_variables"."var_name"
ORDER BY "response_variables"."var_name" ASC;
Here's the output of running EXPLAIN ANALYZE
on that:
GroupAggregate (cost=720.80..723.20 rows=120 width=9) (actual time=277.127..285.953 rows=15265 loops=1)
-> Sort (cost=720.80..721.10 rows=120 width=9) (actual time=277.120..281.391 rows=15265 loops=1)
Sort Key: response_variables.var_name, response_variables.id
Sort Method: external merge Disk: 288kB
-> Nested Loop (cost=0.00..716.66 rows=120 width=9) (actual time=0.064..21.795 rows=15265 loops=1)
-> Nested Loop (cost=0.00..657.78 rows=128 width=4) (actual time=0.050..7.919 rows=3042 loops=1)
-> Index Scan using index_questions_on_key on questions (cost=0.00..8.27 rows=1 width=4) (actual time=0.032..0.033 rows=1 loops=1)
Index Cond: ((key)::text = 'rbmmpmvs'::text)
-> Index Scan using index_responses_on_question_id on responses (cost=0.00..646.69 rows=282 width=8) (actual time=0.016..7.326 rows=3042 loops=1)
Index Cond: (question_id = questions.id)
-> Index Scan using index_response_variables_on_response_id on response_variables (cost=0.00..0.42 rows=4 width=13) (actual time=0.002..0.003 rows=5 loops=3042)
Index Cond: (response_id = responses.id)
Total runtime: 288.766 ms
(13 rows)
I've got a number of indexes on various bits and pieces, but not sure where to start to optimize the call any more (or if it's possible at all).