# Consider Random Topic backfill as background task?

**URL:** https://meta.discourse.org/t/consider-random-topic-backfill-as-background-task/40210
**Category:** Development
**Created:** [2016年二月26日 03:18 UTC](https://meta.discourse.org/t/consider-random-topic-backfill-as-background-task/40210 "2016-02-26T03:18:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2016年二月26日 03:18 UTC](https://meta.discourse.org/t/consider-random-topic-backfill-as-background-task/40210/1 "2016-02-26T03:18:44Z")

</div>

Whenever the random topic list backfill is executed page load time is dramatically hit.

In this example here an additional `301.9 ms` for the single query.

If the “random topic” plays any part of a new users experience to a site…  
**…perhaps it would be best to not make the query immediately and only return / update the results via the message bus once available?**

```plaintext
Executing action: show
T+493.1 ms
Reader
301.9 ms
lib/freedom_patches/fast_pluck.rb:41:in `select_raw'
lib/freedom_patches/fast_pluck.rb:79:in `pluck'
lib/freedom_patches/fast_pluck.rb:71:in `pluck'
app/services/random_topic_selector.rb:27:in `backfill'
app/services/random_topic_selector.rb:57:in `next'
lib/topic_query.rb:657:in `random_suggested'
lib/topic_query.rb:111:in `list_suggested_for'
lib/topic_view.rb:298:in `suggested_topics'
app/serializers/topic_view_serializer.rb:90:in `details'
app/controllers/application_controller.rb:262:in `render_json_dump'
app/controllers/topics_controller.rb:580:in `block (2 levels) in perform_show_response'
app/controllers/topics_controller.rb:572:in `perform_show_response'
app/controllers/topics_controller.rb:89:in `show'
lib/middleware/anonymous_cache.rb:129:in `call'
config/initializers/100-quiet_logger.rb:10:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:26:in `call'
lib/middleware/request_tracker.rb:73:in `call'
lib/scheduler/defer.rb:85:in `process_client'
lib/middleware/unicorn_oobgc.rb:95:in `process_client'

```

```SQL
SELECT "topics"."id" FROM "topics" LEFT OUTER JOIN "categories" ON "categories"."id" = "topics"."category_id" LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = -1) WHERE (categories.id = 114 OR (categories.parent_category_id = 114 AND categories.topic_id <> topics.id)) AND (topics.archetype <> 'private_message') AND (COALESCE(categories.topic_id, 0) <> topics.id) AND "topics"."visible" = 't' AND ("topics"."id" != 67459) AND (topics.deleted_at IS NULL) AND (COALESCE(tu.notification_level,1) > 0) AND (
          NOT EXISTS (
            SELECT 1
              FROM category_users cu
             WHERE cu.user_id = -1
               AND cu.category_id = topics.category_id
               AND cu.notification_level = 0
               AND cu.category_id <> 114
               AND (tu.notification_level IS NULL OR tu.notification_level < 2)
          )) AND "topics"."closed" = 'f' AND "topics"."archived" = 'f' ORDER BY RANDOM() LIMIT 3000   

```

For information that is perhaps rarely seen on initial render (bottom of page), just may be this is a good optimisation?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2016年四月13日 00:24 UTC](https://meta.discourse.org/t/consider-random-topic-backfill-as-background-task/40210/2 "2016-04-13T00:24:40Z")

</div>

You only hit:

> <https://github.com/discourse/discourse/blob/main/app/services/random_topic_selector.rb#L61-L67>

In very extreme cases.

In general the low watermark will hit and it will be deferred per:

> <https://github.com/discourse/discourse/blob/main/app/services/random_topic_selector.rb#L69-L73>
