# Improve performance of Topic load - ORDER BY RANDOM

**URL:** <https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619>\
**Category:** Bug\
**Tags:** slow-sql\
**Created:** [Fevereiro 24, 2015, 4:48pm UTC](https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619 "2015-02-24T16:48:29Z")\
**Posts on this page:** 1\
**Showing post:** 15

<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:** [Fevereiro 28, 2015, 1:36am UTC](https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619/15 "2015-02-28T01:36:49Z")

</div>

> [@DeanMarkTaylor](#):
>
> Is there any chance of the topics.bumped\_at query being cached?

Out of interest in improving the `topics.bumped_at` `list_suggested_for` query I tested adding an index.

###Without Index

> **Executed in ~112ms (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/5/f/5f56af13568d1061d2d1c4b73dd20977b21a8030.png)

> **Executed in ~141.3ms (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/d/b/dbbc33f0ee35ecb734a3db6b31073f00aaba453c.JPG)

###With Index

> **Executed in ~13.2ms (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/4/5/456c0d7946961efb4ebc6c6dd9acbc6bd9462c84.JPG)

> **Executed in ~18.7ms (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/5/c/5cdb1b4a9224a6f16d5994e5be269207d0a4c9fc.JPG)

###index tested

```SQL
CREATE INDEX idx_topic_load_suggested_for
    ON topics
    USING btree
    (deleted_at, visible, archetype COLLATE pg_catalog."default", created_at);

```

###Tested table / index sizes  
`topics` row count: 66,373  
`topics` table size: 29MB  
`idx_topic_load_suggested_for` index size: 3,488KB

###Thoughts and questions  
To me it seems far better to scan only 3.5MB of data instead of 29MB per topic page load considering how frequently it occurs.

This was just a quick pass at what I thought was a matching index for the query - I’m sure someone with more knowledge of the details could do better.

**Is this an index the Discourse Team can get behind and perhaps include?**  
Or is there a more cached solution that’s needed?  
Perhaps just a good stop-gap?

---

_[View the full topic](https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619)._
