# 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:** 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:** [Fevereiro 24, 2015, 4:48pm UTC](https://meta.discourse.org/t/improve-performance-of-topic-load-order-by-random/25619/1 "2015-02-24T16:48:29Z")

</div>

Continuing the discussion from [Slow sql queries](https://meta.discourse.org/t/slow-sql-queries/16604/10):

> [@sam](#):
>
> order by random over the entire result set is a bad strategy. we should change this to maybe just select N random rows or something

##The Problem  
Clicking between topics is slow - for a few reasons - one of which is the generation of suggested topics.

For me I currently consider this a bug because suggested topics currently adds a ~640ms additional query delay to the initial display of a topic which is not critical to that topics display.

##What’s the impact?  
For me with 40K topics and 430K posts on a 2GB Digital Ocean instance I’m seeing **almost 500ms added to each topic load** just from the “random” query for suggested topics.

##Problem Detail  
Currently the function `TopicQuery::list_suggested_for` generates a list of `random` topics to augment the `unread` and `new` topics listed at the bottom of the each topic page.

The `random` element of this uses `ORDER BY RANDOM` in the SQL query, this is an easy but slow way of getting a random list of rows from the database.

Here is the function:

> <https://github.com/discourse/discourse/blob/738f2cb72c295c63c3581ae69d990344d9b947a8/lib/topic_query.rb#L55-L63>

##The Suggestions Queries  
As an example here are the numbers for a slightly faster than typical load - perhaps due to being cached.

###New Topic Suggestions  
So new topic suggestions query is sorted as follows:

```SQL
ORDER BY CASE WHEN topics.category_id = 79 THEN 0 ELSE 1 END, topics.bumped_at DESC LIMIT 5 

```

**This “new” query takes 120.1 ms**

> **:straight\_ruler: Screen capture of "new" query times (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/e/a/ea383975d179c0d838e78a0e2318f0bb16b7b883.png)

###Random Suggestions

```SQL
ORDER BY CASE WHEN topics.category_id = 79 THEN 0 ELSE 1 END, RANDOM() LIMIT 5 

```

**This “random” query takes 479.3 ms** - this was a quite fast example.

> **:straight\_ruler: Screen capture of "random" query times (click to expand)**
>
> ![](https://global.discourse-cdn.com/meta/original/3X/4/1/4190ca8c381ec951d91d72ce18432c9b42bc3531.png)

##Solution / Alternative to `ORDER BY RANDOM`  
There are several alternatives to `ORDER BY RANDOM`, one of which is this:

- [ORDER BY RAND() - ~jk](http://jan.kneschke.de/projects/mysql/order-by-rand/)

Linked from here:

> <https://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast>

---

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