duckduckgo/community-platform — pull request #235 by jbarrett (2014-10-16 12:49:07+00:00)
Title: Target prefetches for common grouped comments queries.

Intent is to simplify common queries in forum view.

Before (DBIC debug output, not psql query log):

```sql
SELECT COUNT( * )
FROM (
SELECT "me"."id"
FROM "comment" "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
GROUP BY "me"."id"
) "me"
SELECT "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."moderated", "me"."parent[582/1898]
SELECT COUNT( * )
FROM "comment" "comments_count"
WHERE "comments_count"."context" = "me"."context" AND "comments_count"."context_id" = "me"."context_id"
), "user"."id", "user"."username", "user"."public", "user"."email_notification_content", "user"."admin", "user"."ghosted", "user"."ignore", "user"."email", "user"."userpage", "user"."data", "user"."notes", "user"."profile_media_id", "user"."created", "user"."updated"
, "user"."roles", "user"."flags", "token_language_translation_vote"."id", "token_language_translation_vote"."users_id", "token_language_translation_vote"."token_language_translation_id", "token_language_translation_vote"."created", "token_language_translation_vote"."upd
ated", "user_2"."id", "user_2"."username", "user_2"."public", "user_2"."email_notification_content", "user_2"."admin", "user_2"."ghosted", "user_2"."ignore", "user_2"."email", "user_2"."userpage", "user_2"."data", "user_2"."notes", "user_2"."profile_media_id", "user_2".
"created", "user_2"."updated", "user_2"."roles", "user_2"."flags", "token_language_translation"."id", "token_language_translation"."msgstr0", "token_language_translation"."msgstr1", "token_language_translation"."msgstr2", "token_language_translation"."msgstr3", "token_l
anguage_translation"."msgstr4", "token_language_translation"."msgstr5", "token_language_translation"."data", "token_language_translation"."notes", "token_language_translation"."token_language_id", "token_language_translation"."check_result", "token_language_translation"
."check_timestamp", "token_language_translation"."check_users_id", "token_language_translation"."username", "token_language_translation"."created", "token_language_translation"."updated", "user_3"."id", "user_3"."username", "user_3"."public", "user_3"."email_notificatio
n_content", "user_3"."admin", "user_3"."ghosted", "user_3"."ignore", "user_3"."email", "user_3"."userpage", "user_3"."data", "user_3"."notes", "user_3"."profile_media_id", "user_3"."created", "user_3"."updated", "user_3"."roles", "user_3"."flags", "token_language"."id",
"token_language"."token_id", "token_language"."token_domain_language_id", "token_language"."notes", "token_language"."msgstr0", "token_language"."msgstr1", "token_language"."msgstr2", "token_language"."msgstr3", "token_language"."msgstr4", "token_language"."msgstr5", "
token_language"."translator_users_id", "token_language"."data", "token_language"."created", "token_language"."updated", "token_language"."fuzzy", "token_domain_language"."id", "token_domain_language"."language_id", "token_domain_language"."token_domain_id", "token_domai
n_language"."sticky_notes", "token_domain_language"."created", "token_domain_language"."updated", "token_domain"."id", "token_domain"."key", "token_domain"."name", "token_domain"."description", "token_domain"."data", "token_domain"."sorting", "token_domain"."source_lang
uage_id", "token_domain"."sticky_notes", "token_domain"."notes", "token_domain"."created", "token_domain"."updated", "language"."id", "language"."name_in_english", "language"."name_in_local", "language"."lang_in_local", "language"."locale", "language"."alternative_local
es", "language"."country_id", "language"."sticky_notes", "language"."nplurals", "language"."plural", "language"."rtl", "language"."data", "language"."notes", "language"."created", "language"."updated", "language"."parent_id", "token"."id", "token"."msgid", "token"."msgi
d_plural", "token"."msgctxt", "token"."type", "token"."data", "token"."notes", "token"."token_domain_id", "token"."created", "token"."updated", "token"."retired", "token_domain_2"."id", "token_domain_2"."key", "token_domain_2"."name", "token_domain_2"."description", "to
ken_domain_2"."data", "token_domain_2"."sorting", "token_domain_2"."source_language_id", "token_domain_2"."sticky_notes", "token_domain_2"."notes", "token_domain_2"."created", "token_domain_2"."updated", "thread"."id", "thread"."users_id", "thread"."forum", "thread"."co
mment_id", "thread"."key", "thread"."title", "thread"."data", "thread"."sticky", "thread"."readonly", "thread"."done", "thread"."deleted", "thread"."created", "thread"."updated", "thread"."ghosted", "thread"."checked", "thread"."seen_live", "thread"."old_url", "user_4".
"id", "user_4"."username", "user_4"."public", "user_4"."email_notification_content", "user_4"."admin", "user_4"."ghosted", "user_4"."ignore", "user_4"."email", "user_4"."userpage", "user_4"."data", "user_4"."notes", "user_4"."profile_media_id", "user_4"."created", "user
_4"."updated", "user_4"."roles", "user_4"."flags", "comment"."id", "comment"."users_id", "comment"."content", "comment"."context", "comment"."context_id", "comment"."deleted", "comment"."readonly", "comment"."is_html", "comment"."data", "comment"."created", "comment"."u
pdated", "comment"."ghosted", "comment"."checked", "comment"."seen_live", "comment"."moderated", "comment"."parent_id", "token_2"."id", "token_2"."msgid", "token_2"."msgid_plural", "token_2"."msgctxt", "token_2"."type", "token_2"."data", "token_2"."notes", "token_2"."to
ken_domain_id", "token_2"."created", "token_2"."updated", "token_2"."retired", "token_domain_3"."id", "token_domain_3"."key", "token_domain_3"."name", "token_domain_3"."description", "token_domain_3"."data", "token_domain_3"."sorting", "token_domain_3"."source_language_
id", "token_domain_3"."sticky_notes", "token_domain_3"."notes", "token_domain_3"."created", "token_domain_3"."updated", "token_domain_language_2"."id", "token_domain_language_2"."language_id", "token_domain_language_2"."token_domain_id", "token_domain_language_2"."stick
y_notes", "token_domain_language_2"."created", "token_domain_language_2"."updated", "token_domain_4"."id", "token_domain_4"."key", "token_domain_4"."name", "token_domain_4"."description", "token_domain_4"."data", "token_domain_4"."sorting", "token_domain_4"."source_lang
uage_id", "token_domain_4"."sticky_notes", "token_domain_4"."notes", "token_domain_4"."created", "token_domain_4"."updated", "language_2"."id", "language_2"."name_in_english", "language_2"."name_in_local", "language_2"."lang_in_local", "language_2"."locale", "language_2
"."alternative_locales", "language_2"."country_id", "language_2"."sticky_notes", "language_2"."nplurals", "language_2"."plural", "language_2"."rtl", "language_2"."data", "language_2"."notes", "language_2"."created", "language_2"."updated", "language_2"."parent_id", "tok
en_language_2"."id", "token_language_2"."token_id", "token_language_2"."token_domain_language_id", "token_language_2"."notes", "token_language_2"."msgstr0", "token_language_2"."msgstr1", "token_language_2"."msgstr2", "token_language_2"."msgstr3", "token_language_2"."msg
str4", "token_language_2"."msgstr5", "token_language_2"."translator_users_id", "token_language_2"."data", "token_language_2"."created", "token_language_2"."updated", "token_language_2"."fuzzy", "token_language_translations"."id", "token_language_translations"."msgstr0",
"token_language_translations"."msgstr1", "token_language_translations"."msgstr2", "token_language_translations"."msgstr3", "token_language_translations"."msgstr4", "token_language_translations"."msgstr5", "token_language_translations"."data", "token_language_translatio
ns"."notes", "token_language_translations"."token_language_id", "token_language_translations"."check_result", "token_language_translations"."check_timestamp", "token_language_translations"."check_users_id", "token_language_translations"."username", "token_language_trans
lations"."created", "token_language_translations"."updated", "user_5"."id", "user_5"."username", "user_5"."public", "user_5"."email_notification_content", "user_5"."admin", "user_5"."ghosted", "user_5"."ignore", "user_5"."email", "user_5"."userpage", "user_5"."data", "u
ser_5"."notes", "user_5"."profile_media_id", "user_5"."created", "user_5"."updated", "user_5"."roles", "user_5"."flags", "token_domain_language_3"."id", "token_domain_language_3"."language_id", "token_domain_language_3"."token_domain_id", "token_domain_language_3"."stic
ky_notes", "token_domain_language_3"."created", "token_domain_language_3"."updated", "token_domain_5"."id", "token_domain_5"."key", "token_domain_5"."name", "token_domain_5"."description", "token_domain_5"."data", "token_domain_5"."sorting", "token_domain_5"."source_lan
guage_id", "token_domain_5"."sticky_notes", "token_domain_5"."notes", "token_domain_5"."created", "token_domain_5"."updated", "language_3"."id", "language_3"."name_in_english", "language_3"."name_in_local", "language_3"."lang_in_local", "language_3"."locale", "language_
3"."alternative_locales", "language_3"."country_id", "language_3"."sticky_notes", "language_3"."nplurals", "language_3"."plural", "language_3"."rtl", "language_3"."data", "language_3"."notes", "language_3"."created", "language_3"."updated", "language_3"."parent_id", "to
ken_3"."id", "token_3"."msgid", "token_3"."msgid_plural", "token_3"."msgctxt", "token_3"."type", "token_3"."data", "token_3"."notes", "token_3"."token_domain_id", "token_3"."created", "token_3"."updated", "token_3"."retired", "token_domain_6"."id", "token_domain_6"."key
", "token_domain_6"."name", "token_domain_6"."description", "token_domain_6"."data", "token_domain_6"."sorting", "token_domain_6"."source_language_id", "token_domain_6"."sticky_notes", "token_domain_6"."notes", "token_domain_6"."created", "token_domain_6"."updated", "us
er_blog"."id", "user_blog"."users_id", "user_blog"."translation_of_id", "user_blog"."title", "user_blog"."uri", "user_blog"."teaser", "user_blog"."content", "user_blog"."topics", "user_blog"."company_blog", "user_blog"."raw_html", "user_blog"."live", "user_blog"."seen_l
ive", "user_blog"."language_id", "user_blog"."data", "user_blog"."fixed_date", "user_blog"."created", "user_blog"."updated", "user_6"."id", "user_6"."username", "user_6"."public", "user_6"."email_notification_content", "user_6"."admin", "user_6"."ghosted", "user_6"."ign
ore", "user_6"."email", "user_6"."userpage", "user_6"."data", "user_6"."notes", "user_6"."profile_media_id", "user_6"."created", "user_6"."updated", "user_6"."roles", "user_6"."flags"
FROM (
SELECT "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."moderated", "me"."parent_id",
(
SELECT COUNT( * )
FROM "comment" "comments_count"
WHERE "comments_count"."context" = "me"."context" AND "comments_count"."context_id" = "me"."context_id"
)
FROM "comment" "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
GROUP BY "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."moderated", "me"."parent_id"

ORDER BY "me"."updated" DESC
LIMIT '15'
) "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "token_language_translation_vote" "token_language_translation_vote"
ON "me"."context" = 'DDGC::DB::Result::Token::Language::Translation::Vote' AND "token_language_translation_vote"."id" = "me"."context_id"
LEFT JOIN "users" "user_2"
ON "user_2"."id" = "token_language_translation_vote"."users_id"
LEFT JOIN "token_language_translation" "token_language_translation"
ON "token_language_translation"."id" = "token_language_translation_vote"."token_language_translation_id"
LEFT JOIN "users" "user_3"
ON "user_3"."username" = "token_language_translation"."username"
LEFT JOIN "token_language" "token_language"
ON "token_language"."id" = "token_language_translation"."token_language_id"
LEFT JOIN "token_domain_language" "token_domain_language"
ON "token_domain_language"."id" = "token_language"."token_domain_language_id"
LEFT JOIN "token_domain" "token_domain"
ON "token_domain"."id" = "token_domain_language"."token_domain_id"
LEFT JOIN "language" "language"
ON "language"."id" = "token_domain_language"."language_id"
LEFT JOIN "token" "token"
ON "token"."id" = "token_language"."token_id"
LEFT JOIN "token_domain" "token_domain_2"
ON "token_domain_2"."id" = "token"."token_domain_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
LEFT JOIN "users" "user_4"
ON "user_4"."id" = "thread"."users_id"
LEFT JOIN "comment" "comment"
ON "comment"."id" = "thread"."comment_id"
LEFT JOIN "token" "token_2"
ON "me"."context" = 'DDGC::DB::Result::Token' AND "token_2"."id" = "me"."context_id"
LEFT JOIN "token_domain" "token_domain_3"
ON "token_domain_3"."id" = "token_2"."token_domain_id"
LEFT JOIN "token_domain_language" "token_domain_language_2"
ON "me"."context" = 'DDGC::DB::Result::Token::Domain::Language' AND "token_domain_language_2"."id" = "me"."context_id"
LEFT JOIN "token_domain" "token_domain_4"
ON "token_domain_4"."id" = "token_domain_language_2"."token_domain_id"
LEFT JOIN "language" "language_2"
ON "language_2"."id" = "token_domain_language_2"."language_id"
LEFT JOIN "token_language" "token_language_2"
ON "me"."context" = 'DDGC::DB::Result::Token::Language' AND "token_language_2"."id" = "me"."context_id"
LEFT JOIN "token_language_translation" "token_language_translations"
ON "token_language_translations"."token_language_id" = "token_language_2"."id"
LEFT JOIN "users" "user_5"
ON "user_5"."username" = "token_language_translations"."username"
LEFT JOIN "token_domain_language" "token_domain_language_3"
ON "token_domain_language_3"."id" = "token_language_2"."token_domain_language_id"
LEFT JOIN "token_domain" "token_domain_5"
ON "token_domain_5"."id" = "token_domain_language_3"."token_domain_id"
LEFT JOIN "language" "language_3"
ON "language_3"."id" = "token_domain_language_3"."language_id"
LEFT JOIN "token" "token_3"
ON "token_3"."id" = "token_language_2"."token_id"
LEFT JOIN "token_domain" "token_domain_6"
ON "token_domain_6"."id" = "token_3"."token_domain_id"
LEFT JOIN "user_blog" "user_blog"
ON "me"."context" = 'DDGC::DB::Result::User::Blog' AND "user_blog"."id" = "me"."context_id"
LEFT JOIN "users" "user_6"
ON "user_6"."id" = "user_blog"."users_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
ORDER BY "me"."updated" DESC
```

After

```sql
SELECT COUNT( * )
FROM (
SELECT "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."parent_id"
FROM "comment" "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
GROUP BY "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."parent_id"
) "me"
SELECT "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."parent_id", ( [495/1928]
SELECT COUNT( * )
FROM "comment" "comments_count"
WHERE "comments_count"."context" = "me"."context" AND "comments_count"."context_id" = "me"."context_id"
), "user"."id", "user"."username", "user"."public", "user"."email_notification_content", "user"."admin", "user"."ghosted", "user"."ignore", "user"."email", "user"."userpage", "user"."data", "user"."notes", "user"."profile_media_id", "user"."created", "user"."updated"
, "user"."roles", "user"."flags", "thread"."id", "thread"."users_id", "thread"."forum", "thread"."comment_id", "thread"."key", "thread"."title", "thread"."data", "thread"."sticky", "thread"."readonly", "thread"."done", "thread"."deleted", "thread"."created", "thread"."u
pdated", "thread"."ghosted", "thread"."checked", "thread"."seen_live", "thread"."old_url", "user_2"."id", "user_2"."username", "user_2"."public", "user_2"."email_notification_content", "user_2"."admin", "user_2"."ghosted", "user_2"."ignore", "user_2"."email", "user_2"."
userpage", "user_2"."data", "user_2"."notes", "user_2"."profile_media_id", "user_2"."created", "user_2"."updated", "user_2"."roles", "user_2"."flags"
FROM (
SELECT "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."parent_id", (
SELECT COUNT( * )
FROM "comment" "comments_count"
WHERE "comments_count"."context" = "me"."context" AND "comments_count"."context_id" = "me"."context_id"
)
FROM "comment" "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
GROUP BY "me"."id", "me"."users_id", "me"."content", "me"."context", "me"."context_id", "me"."deleted", "me"."readonly", "me"."is_html", "me"."data", "me"."created", "me"."updated", "me"."ghosted", "me"."checked", "me"."seen_live", "me"."parent_id"
ORDER BY "me"."updated" DESC
LIMIT '15'
) "me"
JOIN "users" "user"
ON "user"."id" = "me"."users_id"
LEFT JOIN "thread" "thread"
ON "me"."context" = 'DDGC::DB::Result::Thread' AND "thread"."id" = "me"."context_id"
LEFT JOIN "users" "user_2"
ON "user_2"."id" = "thread"."users_id"
WHERE ( "me"."ghosted" = '0' OR ( "me"."ghosted" = '1' AND "me"."users_id" = '1' ) ) AND "me"."context" IN ( 'DDGC::DB::Result::Thread' ) AND "me"."id" IN (
SELECT "comment_context"."latest_comment_id"
FROM (
SELECT ( latest ) .id AS latest_comment_id, (
FIRST
) .id AS first_comment_id, (
FIRST
) .context AS comments_context, (
FIRST
) .context_id AS comments_context_id
FROM (
SELECT (
SELECT latest
FROM comment latest
WHERE latest.context = c.context AND latest.context_id = c.context_id
ORDER BY created DESC
LIMIT 1
) AS latest, (
SELECT
FIRST
FROM comment
FIRST
WHERE first.context = c.context AND first.context_id = c.context_id
ORDER BY created ASC
LIMIT 1
)
FIRST
FROM comment c
GROUP BY context, context_id
) latest_comment
) "comment_context"
) AND "thread"."forum" = '1'
ORDER BY "me"."updated" DESC

```