this is substantially faster than the double nested query:
with pots as (SELECT POTMsgSet.id
FROM POTMsgSet
JOIN TranslationTemplateItem ON TranslationTemplateItem.potmsgset = POTMsgSet.id
JOIN SuggestivePOTemplate ON TranslationTemplateItem.potemplate = SuggestivePOTemplate.potemplate
WHERE msgid_singular = 347523)
SELECT TranslationMessage.COMMENT, TranslationMessage.date_created, TranslationMessage.date_reviewed, TranslationMessage.id, TranslationMessage.is_current_ubuntu, TranslationMessage.is_current_upstream, TranslationMessage.LANGUAGE, TranslationMessage.msgstr0, TranslationMessage.msgstr1, TranslationMessage.msgstr2, TranslationMessage.msgstr3, TranslationMessage.msgstr4, TranslationMessage.msgstr5, TranslationMessage.origin, TranslationMessage.potemplate, TranslationMessage.potmsgset, TranslationMessage.reviewer, TranslationMessage.submitter, TranslationMessage.validation_status, TranslationMessage.was_obsolete_in_last_import
FROM TranslationMessage
WHERE TranslationMessage.id IN
(SELECT DISTINCT ON (COALESCE(msgstr0, -1), COALESCE(msgstr1, -1), COALESCE(msgstr2, -1), COALESCE(msgstr3, -1), COALESCE(msgstr4, -1), COALESCE(msgstr5, -1)) TranslationMessage.id
FROM TranslationMessage
WHERE (TranslationMessage.LANGUAGE IN (132))
AND TranslationMessage.potmsgset <> 8171049
AND potmsgset IN (select id from pots)
ORDER BY COALESCE(msgstr0, -1), COALESCE(msgstr1, -1), COALESCE(msgstr2, -1), COALESCE(msgstr3, -1), COALESCE(msgstr4, -1), COALESCE(msgstr5, -1), date_created DESC)LIMIT 2001
Limit (cost=372.84..697.30 rows=27 width=76) (actual time=0.814..0.872 rows=4 loops=1)
CTE pots
-> Nested Loop (cost=0.00..101.45 rows=18 width=4) (actual time=0.057..0.513 rows=24 loops=1)
-> Nested Loop (cost=0.00..96.38 rows=18 width=8) (actual time=0.044..0.268 rows=25 loops=1)
-> Index Scan using potmsgset_primemsgid_idx on potmsgset (cost=0.00..18.54 rows=5 width=4) (actual time=0.021..0.047 rows=9 loops=1) Index Cond: (msgid_singular = 347523)
-> Index Scan using translationtemplateitem__potmsgset__idx on translationtemplateitem (cost=0.00..15.52 rows=4 width=8) (actual time=0.015..0.022 rows=3 loops=9) Index Cond: (translationtemplateitem.potmsgset = potmsgset.id)
-> Index Scan using suggestivepotemplate_pkey on suggestivepotemplate (cost=0.00..0.27 rows=1 width=4) (actual time=0.008..0.009 rows=1 loops=25) Index Cond: (suggestivepotemplate.potemplate = translationtemplateitem.potemplate)
-> Nested Loop (cost=271.39..595.86 rows=27 width=76) (actual time=0.812..0.868 rows=4 loops=1)
-> HashAggregate (cost=271.39..271.66 rows=27 width=4) (actual time=0.787..0.789 rows=4 loops=1)
-> Unique (cost=270.58..271.05 rows=27 width=36) (actual time=0.768..0.779 rows=4 loops=1) -> Sort (cost=270.58..270.65 rows=27 width=36) (actual time=0.767..0.768 rows=6 loops=1) Sort Key: (COALESCE(public.translationmessage.msgstr0, (-1))), (COALESCE(public.translationmessage.msgstr1, (-1))), (COALESCE(public.translationmessage.msgstr2, (-1))), (COALESCE(public.translationmessage.msgstr3, (-1))), (COALESCE(public.translationmessage.msgstr4, (-1))), (COALESCE(public.translationmessage.msgstr5, (-1))), public.translationmessage.date_created Sort Method: quicksort Memory: 25kB -> Nested Loop (cost=0.40..269.94 rows=27 width=36) (actual time=0.631..0.728 rows=6 loops=1) -> HashAggregate (cost=0.40..0.58 rows=18 width=4) (actual time=0.580..0.583 rows=6 loops=1) -> CTE Scan on pots (cost=0.00..0.36 rows=18 width=4) (actual time=0.062..0.550 rows=24 loops=1) -> Index Scan using translationmessage__potmsgset__language__idx on translationmessage (cost=0.00..14.94 rows=2 width=40) (actual time=0.020..0.021 rows=1 loops=6) Index Cond: ((public.translationmessage.potmsgset = pots.id) AND (public.translationmessage.language = 132)) Filter: (public.translationmessage.potmsgset <> 8171049)
-> Index Scan using translationmessage_pkey on translationmessage (cost=0.00..11.99 rows=1 width=76) (actual time=0.016..0.017 rows=1 loops=4) Index Cond: (public.translationmessage.id = public.translationmessage.id)
Total runtime: 1.126 ms
(25 rows)
this is substantially faster than the double nested query: lateItem ON TranslationTemp lateItem. potmsgset = POTMsgSet.id plate ON TranslationTemp lateItem. potemplate = SuggestivePOTem plate.potemplat e age.COMMENT, TranslationMess age.date_ created,
TranslationMess age.date_ reviewed,
TranslationMess age.id,
TranslationMess age.is_ current_ ubuntu,
TranslationMess age.is_ current_ upstream,
TranslationMess age.LANGUAGE, TranslationMess age.msgstr0,
TranslationMes sage.msgstr1,
TranslationMes sage.msgstr2,
TranslationMes sage.msgstr3,
TranslationMes sage.msgstr4,
TranslationMes sage.msgstr5,
TranslationMes sage.origin,
TranslationMes sage.potemplate ,
TranslationMes sage.potmsgset,
TranslationMes sage.reviewer,
TranslationMes sage.submitter,
TranslationMes sage.validation _status,
TranslationMes sage.was_ obsolete_ in_last_ import age.id IN age.id sage.LANGUAGE IN (132)) age.potmsgset <> 8171049
with pots as (SELECT POTMsgSet.id
FROM POTMsgSet
JOIN TranslationTemp
JOIN SuggestivePOTem
WHERE msgid_singular = 347523)
SELECT TranslationMess
FROM TranslationMessage
WHERE TranslationMess
(SELECT DISTINCT ON (COALESCE(msgstr0, -1), COALESCE(msgstr1, -1), COALESCE(msgstr2, -1), COALESCE(msgstr3, -1), COALESCE(msgstr4, -1), COALESCE(msgstr5, -1)) TranslationMess
FROM TranslationMessage
WHERE (TranslationMes
AND TranslationMess
AND potmsgset IN (select id from pots)
ORDER BY COALESCE(msgstr0, -1), COALESCE(msgstr1, -1), COALESCE(msgstr2, -1), COALESCE(msgstr3, -1), COALESCE(msgstr4, -1), COALESCE(msgstr5, -1), date_created DESC)LIMIT 2001
Limit (cost=372. 84..697. 30 rows=27 width=76) (actual time=0.814..0.872 rows=4 loops=1) primemsgid_ idx on potmsgset (cost=0.00..18.54 rows=5 width=4) (actual time=0.021..0.047 rows=9 loops=1)
Index Cond: (msgid_singular = 347523) lateitem_ _potmsgset_ _idx on translationtemp lateitem (cost=0.00..15.52 rows=4 width=8) (actual time=0.015..0.022 rows=3 loops=9)
Index Cond: (translationtem plateitem. potmsgset = potmsgset.id) plate_pkey on suggestivepotem plate (cost=0.00..0.27 rows=1 width=4) (actual time=0.008..0.009 rows=1 loops=25)
Index Cond: (suggestivepote mplate. potemplate = translationtemp lateitem. potemplate) 39..595. 86 rows=27 width=76) (actual time=0.812..0.868 rows=4 loops=1) 39..271. 66 rows=27 width=4) (actual time=0.787..0.789 rows=4 loops=1) 58..271. 05 rows=27 width=36) (actual time=0.768..0.779 rows=4 loops=1)
-> Sort (cost=270. 58..270. 65 rows=27 width=36) (actual time=0.767..0.768 rows=6 loops=1)
Sort Key: (COALESCE( public. translationmess age.msgstr0, (-1))), (COALESCE( public. translationmess age.msgstr1, (-1))), (COALESCE( public. translationmess age.msgstr2, (-1))), (COALESCE( public. translationmess age.msgstr3, (-1))), (COALESCE( public. translationmess age.msgstr4, (-1))), (COALESCE( public. translationmess age.msgstr5, (-1))), public. translationmess age.date_ created
Sort Method: quicksort Memory: 25kB
- > Nested Loop (cost=0.40..269.94 rows=27 width=36) (actual time=0.631..0.728 rows=6 loops=1)
-> HashAggregate (cost=0.40..0.58 rows=18 width=4) (actual time=0.580..0.583 rows=6 loops=1)
-> CTE Scan on pots (cost=0.00..0.36 rows=18 width=4) (actual time=0.062..0.550 rows=24 loops=1)
-> Index Scan using translationmess age__potmsgset_ _language_ _idx on translationmessage (cost=0.00..14.94 rows=2 width=40) (actual time=0.020..0.021 rows=1 loops=6)
Index Cond: ((public. translationmess age.potmsgset = pots.id) AND (public. translationmess age.language = 132))
Filter: (public. translationmess age.potmsgset <> 8171049) age_pkey on translationmessage (cost=0.00..11.99 rows=1 width=76) (actual time=0.016..0.017 rows=1 loops=4)
Index Cond: (public. translationmess age.id = public. translationmess age.id)
CTE pots
-> Nested Loop (cost=0.00..101.45 rows=18 width=4) (actual time=0.057..0.513 rows=24 loops=1)
-> Nested Loop (cost=0.00..96.38 rows=18 width=8) (actual time=0.044..0.268 rows=25 loops=1)
-> Index Scan using potmsgset_
-> Index Scan using translationtemp
-> Index Scan using suggestivepotem
-> Nested Loop (cost=271.
-> HashAggregate (cost=271.
-> Unique (cost=270.
-> Index Scan using translationmess
Total runtime: 1.126 ms
(25 rows)
Time: 11.106 ms