如何将分区数据库与 RoR 结合使用?
我想用一个具体的例子来问这个问题,因为我正在寻找具体的答案。 :) 假设我有一组按 user_id 分片的 MySQL 数据库。例如,所有 ID 为 1-10000 的用户都将进入数据库 D1,ID 为 10001 - 20000 的用户将进入数据库 D2,依此类推。我的 RoR 应用程序中有一个模型“User”。根据所需信息的 user_id,该模型应查询适当的数据库并返回结果。任何 RoR 专家都可以告诉我如何实现这一点吗?
一个相关的问题是,假设我在运行 MySQL 的同一个机器上创建了 N 个数据库 D1、D2 ... DN,并在这些数据库前面创建了一个 memcached 缓存层。以这种方式构建数据库会导致性能不佳吗? (我担心DB层和memcache层会不会有很多cache miss。)
I want to ask this question with a specific example as I am looking for a concrete answer. :) Let's say I have a set of MySQL databases sharded on user_id. For example, all users who have ids 1-10000 will go into database D1, user ids with 10001 - 20000 will go into database D2 so on.. I have a model "User" in my RoR application. Depending upon the user_id for which information required this model should query appropriate database and return the results back. Can any RoR expert tell how to make it possible?
A related question is that, let's say I created N databases D1, D2 ... DN on the same box where MySQL running and a memcached cache layer infront of these databases. Does structuring database in this way result in poor performance? (I am worried will there be many cache misses at DB layer and memcache layer.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要的是: http://partitioned.rubyforge.org/
I think that what you need is: http://partitioned.rubyforge.org/
看一下 DataFabric gem,它为 Active Record 添加了数据库分片支持。它可以让您执行以下操作:
Take a look at the DataFabric gem which adds database sharding support to Active Record. It lets you do things like:
您正在寻找的通常称为分片。维基百科上有一篇关于它的非常全面的文章,您绝对应该阅读并阅读高可扩展性博客上的一篇好文章。
当涉及到 Rails 分片时,我推荐 data Fabric gem,它支持应用程序级数据库分片以及主/从复制。
我希望这有帮助!
What you're looking for is commonly called sharding. There's a pretty comprehensive article on wikipedia about it, which you should definitely read and a good article on the high scalability blog.
When it comes to sharding with rails I'd recommend the data fabric gem which supports application level database sharding as well as master/slave replication.
I hope this helps!