V
V
Valeriu Vodnicear2019-10-21 18:58:22
PostgreSQL
Valeriu Vodnicear, 2019-10-21 18:58:22

How to optimize the fetch from the database?

Can someone help me optimize this query:

SELECT q.id
FROM questions.question q
WHERE
  NOT EXISTS (SELECT qt.question_id FROM questions.question_translate_it qt WHERE q.id = qt.question_id)
  AND
  NOT EXISTS (SELECT true FROM questions.translate_questions tq WHERE q.id = tq.question_id AND tq.language_id = 8)
LIMIT 1;

it works but long
indexes:
  • q.id - primary
  • qt.question_id - unique
  • tq.question_id, tq.language_id - unique

Here is the query execution plan:
5dadd59079c98470597194.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-10-21
@Alex_Wells

SELECT q.id
FROM questions.question q
LEFT JOIN questions.question_translate_it qt ON q.id = qt.question_id
LEFT JOIN questions.translate_questions tq ON q.id = tq.question_id
  AND tq.language_id = 8
WHERE qt.question_id IS NULL
WHERE tq.question_id IS NULL;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question