本文介绍了Rails:如何在主/从数据库之间拆分写/读查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站的阅读量非常大.比写流量要重得多.

My website has a very heavy read traffic. A lot heavier than write traffic.

为了提高网站的性能,我考虑过使用 master/slave 数据库配置.

To improve the performance of my website I have thought of going with master/slave database configuration.

octupus gem似乎提供了我想要的东西,但是由于我的应用程序很大,所以我无法通过数百万个源代码行来更改查询分布(将读取查询发送到从属服务器并将查询写入到主服务器).

The octupus gem seems to provide what I want, but since my app is huge I can't go though a millions of source code line to change the query distribution(sending read query to slave server and write query to master server).

MySQL Proxy似乎是解决此问题的好方法,但由于它是Alpha版,所以我不想使用它.

MySQL Proxy seems to be a great way to resolve this issue but since it is in alpha version I don't want to use it.

所以我的问题是在主/从服务器上拆分读写查询的最佳方法是什么?

So my question is what is the best way to split read/write queries across master/slave server?

是否可以在不使用rails的情况下拆分读写查询?

Is it possible to split read/write query without using any gems in rails?

推荐答案

有了octopus gem,您不必更改很多代码就可以将写查询转到主数据库服务器,将读取查询转到从数据库.

With octopus gem, you don't have to change much of your code to make write queries go to the master db server and read queries go to the slaves.

这是一个简单的配置文件,如此处所述.

It's a simple configuration file, as stated here.

我过去曾经尝试过,但效果很好.对我来说,唯一的问题是,当从服务器关闭时,它不会将查询重定向"到主数据库服务器,正如我问的.

I've tried this in the past and It worked quite well. The only problem for me is that when the slave is down, it doesn't "redirect" the queries to the master db server, as I asked here.

但是,如果要配置每个单独的查询目标,则将需要一些工作.

But, if you want to configure each individual query destination, it will take some work.

我建议您开始映射更频繁的查询以及响应时间更长的查询.了解了这些查询后,您可以单独优化它们.这可能已经解决了部分问题.

I would suggest you to start mapping your more frequent queries and those that are taking longer to respond. Knowing those queries, you can optimize them individually. This may already solve part of the problem.

如果在那之后您仍然需要主从复制,请使用章鱼宝石仅更改少数几个复杂的查询的行为.

If you still need master-slave replication after that, use the octopus gem to change the behaviour only those few complicated queries.

这篇关于Rails:如何在主/从数据库之间拆分写/读查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!