Comment 5 for bug 734642

Robert Collins (lifeless) wrote :

This has the best plan and performance I've been able to put together: 372 startup and completion estimate, 10ms actual runtime.

 explain analyze 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 and potmsgset.id<>8171049)
SELECT DISTINCT ON (COALESCE(msgstr0, -1), COALESCE(msgstr1, -1), COALESCE(msgstr2, -1), COALESCE(msgstr3, -1), COALESCE(msgstr4, -1), COALESCE(msgstr5, -1)) TranslationMessage.id, TranslationMessage.COMMENT, TranslationMessage.date_created,
                                   TranslationMessage.date_reviewed,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 join pots on pots.id=translationmessage.potmsgset and translationmessage.language in (132)
 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;
                                                                                                                                                                 QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (cost=371.72..372.20 rows=27 width=76) (actual time=1.075..1.121 rows=4 loops=1)
   CTE pots
     -> Nested Loop (cost=0.00..101.46 rows=18 width=4) (actual time=0.064..0.475 rows=16 loops=1)
           -> Nested Loop (cost=0.00..96.39 rows=18 width=8) (actual time=0.051..0.287 rows=17 loops=1)
                 -> Index Scan using potmsgset_primemsgid_idx on potmsgset (cost=0.00..18.56 rows=5 width=4) (actual time=0.028..0.062 rows=8 loops=1)
                       Index Cond: (msgid_singular = 347523)
                       Filter: (id <> 8171049)
                 -> Index Scan using translationtemplateitem__potmsgset__idx on translationtemplateitem (cost=0.00..15.52 rows=4 width=8) (actual time=0.013..0.025 rows=2 loops=8)
                       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.010 rows=1 loops=17)
                 Index Cond: (suggestivepotemplate.potemplate = translationtemplateitem.potemplate)
   -> Unique (cost=270.26..270.74 rows=27 width=76) (actual time=1.074..1.119 rows=4 loops=1)
         -> Sort (cost=270.26..270.33 rows=27 width=76) (actual time=1.073..1.087 rows=18 loops=1)
               Sort Key: (COALESCE(translationmessage.msgstr0, (-1))), (COALESCE(translationmessage.msgstr1, (-1))), (COALESCE(translationmessage.msgstr2, (-1))), (COALESCE(translationmessage.msgstr3, (-1))), (COALESCE(translationmessage.msgstr4, (-1))), (COALESCE(translationmessage.msgstr5, (-1))), translationmessage.date_created
               Sort Method: quicksort Memory: 27kB
               -> Nested Loop (cost=0.00..269.62 rows=27 width=76) (actual time=0.099..0.764 rows=18 loops=1)
                     -> CTE Scan on pots (cost=0.00..0.36 rows=18 width=4) (actual time=0.070..0.503 rows=16 loops=1)
                     -> Index Scan using translationmessage__potmsgset__language__idx on translationmessage (cost=0.00..14.93 rows=2 width=76) (actual time=0.011..0.012 rows=1 loops=16)
                           Index Cond: ((translationmessage.potmsgset = pots.id) AND (translationmessage.language = 132))
 Total runtime: 1.390 ms
(20 rows)

Time: 10.611 ms