# A lot of records running slow on Dreamhost - any suggestions

**URL:** <https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674>\
**Category:** Deployment\
**Created:** [February 20, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674 "2008-02-20T15:57:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 20, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/1 "2008-02-20T15:57:50Z")

</div>

Hi guys,

I’m quite new to Ruby on Rails and I am deploying my first app.  
I have over 50,000 records and each record has a true or false active  
state. On the homepage I find 50 of the active records and order them  
randomly.

```auto
def index
    @records = Record.find_all_by_active(true, :order => 'rand()',
:limit => 50)
end
```

The problem is that my app is running pretty slow and I’m assuming it’s  
because there’s a lot of records to index through.

I have heard about different types of caching but I’m not sure if this  
is applicable here as I want the records that are displayed to change on  
each visit.

I’m running on Dreamhost & Apache (using Fast CGI).

Would appreciate any suggestions or links.

Thanks

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 20, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/2 "2008-02-20T17:32:48Z")

</div>

Did you create an index on the ‘active’ boolean column? This would help  
a  
lot if it’s a database performance bottleneck. You could also use page  
or  
fragment caching to speed things up further but make sure it has a  
proper  
index.

Raul

----- Original Message -----  
From: “Lee M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 20, 2008 6:57 AM  
Subject: [Rails-deploy] A lot of records running slow on Dreamhost - any  
suggestions

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 12:33am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/3 "2008-02-21T00:33:45Z")

</div>

On 2/21/08, Lee M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @records = Record.find\_all\_by\_active(true, :order =\> ‘rand()’,  
> :limit =\> 50)

you might want to double check, but I recall something about the  
rand() function of mySQL being a slow pig. What is the performance  
like without the rand()

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 1:11am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/4 "2008-02-21T01:11:54Z")

</div>

@Anthony - Good catch. I forgot about that.

But with the LIMIT of 50 it shouldn’t be that big of a deal. Maybe the  
generated SQL is not what we think it is? I guess you could also try  
replacing this with a find\_by\_sql statement and see if it performs any  
better.

@records = Record.find\_by\_sql(SELECT \* FROM records WHERE active=1 ORDER  
BY  
rand() LIMIT 50")

Raul

----- Original Message -----  
From: “Anthony R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 20, 2008 3:32 PM  
Subject: [Rails-deploy] Re: A lot of records running slow on Dreamhost -  
any  
suggestions

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 1:34am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/5 "2008-02-21T01:34:10Z")

</div>

Ugh! You sir, are correct! What a drag…

Again, great catch.

Raul

----- Original Message -----  
From: “Anthony R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 20, 2008 4:30 PM  
Subject: [Rails-deploy] Re: A lot of records running slow on Dreamhost -  
any  
suggestions

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 1:31am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/6 "2008-02-21T01:31:15Z")

</div>

But the rand() would need to be performed on the entire dataset before  
the limit is applied otherwise it wouldn’t be random!

Anthony

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 1:37am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/7 "2008-02-21T01:37:07Z")

</div>

Problem is that it has to do the rand first, then the limit afterwards.

I just tried this on a table with around 4 million records:  
select \* from galleries order by rand() limit 10;

I stopped it after 3 minutes.

This took .001 seconds on the same table:  
select \* from galleries order by id limit 10;

On an unindexed field, it took 3.5 seconds:  
select \* from galleries order by thumb\_exists limit 10;

Order by rand() really sucks. We try not to use it at all.

- Ericson S.  
[http://www.funadvice.com](http://www.funadvice.com)

On Wed, Feb 20, 2008 at 7:30 PM, Anthony R. \<

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 1:52am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/8 "2008-02-21T01:52:23Z")

</div>

I saw this workaround on the web: SELECT \* FROM table WHERE RAND()\>0.9  
ORDER BY RAND()

Here’s the site link: [SQL to Select a random row from a database table](http://www.petefreitag.com/item/466.cfm)

Raul

----- Original Message -----  
From: Ericson S.  
To: removed\_email\_address@domain.invalid  
Sent: Wednesday, February 20, 2008 4:36 PM  
Subject: [Rails-deploy] Re: A lot of records running slow on Dreamhost

- any suggestions

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 6:33am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/9 "2008-02-21T06:33:41Z")

</div>

On 21.2.2008, at 4.05, Anthony R. wrote:

> depending on the business requirements for doing a random select  
> anyway, you may be able to create “randomvalue” column on the table  
> and then do a select against that. Have a background thread (or a  
> stored procedure) that then updates all the rows with a random value  
> every 10 minutes (or every hour?). That way the time consuming part is  
> not customer facing.
> 
> This depends on why you need a random selection in the first place  
> ofcourse!

Here’s Jan Kneschke’s take on the subject:

> **[ORDER BY RAND()](http://jan.kneschke.de/projects/mysql/order-by-rand/)**
>
> If you read the MySQL manual you might have seen the ORDER BY RAND() to randomize the the rows and using the LIMIT 1 to just take one of the rows. SELECT name FROM random ORDER BY RAND() LIMIT 1; This example works fine and is fast if you only ...

//jarkko

–  
Jarkko L.

> **[jlaine.net](https://jlaine.net/)**
>
> Your freedom to carry a gun messes with my freedom to walk around without fear. Your freedom to “stand your ground” with force is at odds with my …

> **[Home](https://dotherightthing.com/)**
>
> do the right thing

[http://www.railsecommerce.com](http://www.railsecommerce.com)  
[http://odesign.fi](http://odesign.fi)

---

<div class="post-metadata">

**Author:** ![fonziguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fonziguy](https://www.ruby-forum.com/u/fonziguy)\
**Post date:** [February 21, 2008, 3:06am UTC](https://www.ruby-forum.com/t/a-lot-of-records-running-slow-on-dreamhost-any-suggestions/129674/10 "2008-02-21T03:06:12Z")

</div>

depending on the business requirements for doing a random select  
anyway, you may be able to create “randomvalue” column on the table  
and then do a select against that. Have a background thread (or a  
stored procedure) that then updates all the rows with a random value  
every 10 minutes (or every hour?). That way the time consuming part is  
not customer facing.

This depends on why you need a random selection in the first place  
ofcourse!

Cheers,

Anthony R.
