Ruby on Rails relies on it's database for just about everything it does. Typically the database performs well but it is also one of the first things that slows down a site, no matter what type of Rails hosting it is on. There are a few techniques you can use to optimize your database.
1. Move your database to a separate server
Moving your database to a separate server will let you add resources for the database itself without having to fight Rails for those resources. This can be done with most Rails hosting providers and is a good first step if you are trying to scale up your site.
2. Turn off unnecessary database features
Most databases are configured with a bunch of extra features by default. Turning off these features can save you some resources on the server. Some common things to turn off are: remote connections, unused database engines (MyISAM vs InnoDB), or optional authentication settings like LDAP.
3. Setup a database cluster
At a certain point you will need to setup a database cluster. A database cluster is just a group of servers that all have a copy of your database running. Your website will then connect to a random server in the cluster to get it's data. So if you have a database cluster with 3 servers, each server will handle 1/3 of the database traffic. The most common database cluster setup is master/slave, which means that there is one of your servers setup as the master and the rest are considered slaves. The master server is the one who is holds the original data and the slaves have copies of that data that they refresh every few minutes.
4. Separate your data to multiple databases
Depending on your site and the date it stores, it might make sense to separate your data into a second (or third) database. This can be the same database type as your main application, like MySQL, or something different like MongoDB. Common things to separate are logging or historic data. For example with an e-commerce site it might be useful to have all of your product prices in the database but you can keep your historic prices in a separate database since they aren't used as often. Ruby on Rails has support for connecting to multiple databases so it isn't too difficult to set up.
5. Use a database caching layer
The fastest way to make your database quick is to never connect to it. This is what a database caching layer does. It sits between your Rails application and the database and will cache queries to your database for a short period of time. This means that instead of hitting your database for the same record 100 times, the caching layer will hit your database once and then serve up the cached record the other 99 times. Memcached is used for this most of the time and has great performance, but you need to take extra steps to make sure that it clears it's cache correctly.
These five optimizations should help you get started on speeding up your Rails site. You don't need to do them all at once either, just pick one or two to start with and add the others later on.
Author Resource:
I didn't address this issue but the actual rails web host you are using plays an important role in your database performance. You want a host that is easy to get started with but can also help you scale up your site as you get more and more traffic. I have seen a lot of people recommend hosting Rails on Host Gator , they are inexpensive to get started with and have great support for handling large traffic sites.