混合 PostgreSQL 和 MongoDB(作为 Django 后端)
出于性能原因,我正在考虑将网站的后端从 Postgres 转移到 Mongo,但网站的关键部分依赖 GeoDjango 模型来计算现实世界中对象之间的距离(等等)。
让大部分站点在 Mongo 上运行但那些关键区域使用 Postgres 进行存储是否可行?这是否痛苦和/或容易出错?我是否缺少全 Mongo 解决方案?
如果您能为我解答这些问题,我将不胜感激。
I'm thinking about shifting my site's backend to Mongo from Postgres for performance reasons, but key parts of the site rely on the GeoDjango models to calculate distances between objects in the real world (and so on).
Would it be feasible to have most of the site running on Mongo but those key areas using Postgres for storage? Is this painful and / or error-prone? Is there an all-Mongo solution I'm missing?
Any light you can shed on these matters for me would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Django 1.2 开始,您可以在
设置中定义多个数据库连接。 py
.然后您可以使用数据库路由器 告诉 Django 要访问哪个数据库,这对您的应用程序来说是透明的。免责声明:这就是我认为它应该如何工作,我从未在 Django 中使用过 MongoDB,也没有测试过我的代码是否实际工作。 :)
settings.py
模型
然后将自定义元变量添加到您的地理表中,以覆盖其数据库。不要将此属性添加到应该进入默认数据库的模型中。
数据库路由器
并编写一个数据库路由器,将所有设置了
using
元属性的模型定向到适当的连接:Since Django 1.2, you can define multiple datbase connections in your
settings.py
. Then you can use database routers to tell Django which database to go to, transparently for your application.Disclaimer: this is how I think it should work, I have never used MongoDB in Django, nor have I tested that my code actually works. :)
settings.py
Models
Then add custom Meta variables to your geo-tables, to override their database. Don't add this attribute to models that are supposed to go to the default database.
Database router
And write a database router to direct all models that have the
using
meta attribute set, to the appropriate connection:元列表中不能有“使用”。
这是一个可行的解决方案
,将其添加到 models.py:
在您的应用程序文件夹中创建一个 router.py:router.py
的内容:
在您的设置中引用路由器:
you can't have 'using' in the Meta list.
here is a working solution
add this to models.py:
create a router.py in your apps folder:
Content of router.py:
Reference router in your settings:
我会看一下 Disqus talk from DjangoCan 2010 关于它们的缩放建筑学。他们很可能在 Postgres 上运行最大的 Django 网站。他们提供了简单的代码片段,展示了如何使用 Django 内置功能启动垂直和水平缩放。
我的理解是,他们确实使用 MongoDB 进行一些分析,但我认为该演讲中没有讨论这一点。
I would take a look at the Disqus talk from DjangoCan 2010 about their scaling architecture. They run quite possibly the largest Django website on top of Postgres. They present simple code snippets showing how to start both vertical and horizontal scaling using features built into Django.
My understanding is that they do use MongoDB for some of their analytics thought I don't think it's discussed in that talk.