# 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:** 20

<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, 8:50pm UTC](https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619/20 "2015-02-28T20:50:54Z")

</div>

> [@sam](#):
>
> What I want you to test is:

###Best Results  
Yes this gives the best results:

```SQL
create index idx_topics_suggested_for
on topics (visible, created_at) 
where archetype <> 'private_message' and deleted_at is null

```

Index size: 1248KB  
Method: Uses Index scan  
Reduces the query time to: 6.7-5.8ms from 141.3-112ms

###Other Notes  
####Note 1  
In this query the index is not used because of the join reducing the result set before `bumped_at` is used / useful.

```SQL
create index idx_topics_suggested_for
on topics (bumped_at) 
where archetype <> 'private_message' and deleted_at is null and visible

```

Index size: 896KB

####Note 2  
The following index causes “recheck condition” on the `topics` table using bitmap heap scan after initially reducing the result set, causing slower total query time.

```SQL
create index idx_topics_suggested_for
on topics (created_at) 
where archetype <> 'private_message' and deleted_at is null and visible

```

Index size: 896KB

####Note 3

> [@sam](#):
>
> What I am really struggling here with is why would you be adding deleted\_at to the actual index when the only valid value for it on selection is NULL. We are never ordering on deleted\_at

Yeah that would be because I’m used to not using INDEX’s that support WHERE conditions, you don’t need to be ordering on an item for it to help with index lookup performance in those cases.

---

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