Comment 2 for bug 734642

Robert Collins (lifeless) wrote :

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)

Time: 11.106 ms