通过将Django模型/表分解成两个模型/表,是否具有性能优势? [英] Are there performance advantages by splitting a Django model/table into two models/tables?

查看:106
本文介绍了通过将Django模型/表分解成两个模型/表,是否具有性能优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO问题7531153 中,我提出了将Django模型分为两个方法的正确方法 - 使用Django的Multi-表继承或明确定义OneToOneField。

In SO question 7531153, I asked the proper way to split a Django model into two—either using Django's Multi-table Inheritance or explicitly defining a OneToOneField.

基于 Luke Sneeringer的<一个评论,如果两个分割模型有一个性能增益,我很好奇。

Based Luke Sneeringer's comment, I'm curious if there's a performance gain from splitting the model in two.

我正在考虑将模型分为两个原因是因为我有一些领域将始终完成,而其他领域通常将为空(直到项目关闭)。

The reason I was thinking about splitting the model in two is because I have some fields that will always be completed, while there are other fields that will typically be empty (until the project is closed).

是否通过空白字段提高性能,例如 actual_completion_date actual_project_costs ,转换为Django中的单独模型/表格?

Are there performance gains from putting typically empty fields, such as actual_completion_date and actual_project_costs, into a separate model/table in Django?

class Project(models.Model):
    project_number = models.SlugField(max_length=5, blank=False,
            primary_key=True)
    budgeted_costs = models.DecimalField(max_digits=10, decimal_places=2)
    submitted_on = models.DateField(auto_now_add=True)

class ProjectExtendedInformation(models.Model):
    project = models.OneToOneField(CapExProject, primary_key=True)
    actual_completion_date = models.DateField(blank=True, null=True)
    actual_project_costs = models.DecimalField(max_digits=10, decimal_places=2,
            blank=True, null=True)


推荐答案

对于您的情况,如果有一些信息仅在关闭时可用,我建议您单独制作一个模型。

For your case, if there's some info that's available only when it's closed, I'd indeed advise making a separate model.

连接也不错。特别是在你的情况下,如果一个表中的所有行和另一个表中的行少得多,则连接将更快。我已经在数据库上工作了很多,在大多数情况下,这是一个纯粹的猜测,来判断一个连接是否会更好或更坏。即使是全表扫描也比许多情况下使用索引更好。您需要查看EXPLAINs,如果性能出现问题,并且可能会对Db工作进行配置(我知道Oracle支持这一点)但是,在性能成为问题之前,我更喜欢更快的开发。

Joins aren't bad. Especially in your case the join will be faster if you have all rows in one table and much less rows in the other one. I've worked with databases a lot, and in most cases it's a pure guess to tell if a join will be better or worse. Even a full table scan is better than using an index in many cases. You need to look at the EXPLAINs, if performance is a concern, and profile the Db work if possible (I know Oracle supports this.) But before performance becomes an issue, I prefer quicker development.

我们在Django中有一个5M行的表。而且我们需要一个仅对1K行不会为空的列。只是改变桌子将需要半天的时间。从零开始重建也需要几个小时。我已经选择制作了一个单独的模型。

We have a table in Django with 5M rows. And we needed a column that would have been not null only for 1K rows. Just altering the table would have taken half a day. Rebuilding from scratch also takes a few hours. We've chosen to make a separate model.

我曾参加过域驱动设计讲座,其中作者解释说,这很重要,特别是在开发一个新的应用程序,分离模型,不要把所有东西都放在一个课堂。

I've been to a lecture on Domain Driven Design in which the author explained that it is important, especially in development of a new app, to separate models, to not stuff everything in one class.

假设你有一个CargoAircraft类和PassengerAircraft。把它们放在一个班里,无缝地工作是如此诱人,不是吗?但是与他们的互动(调度,预约,重量或容量计算)是完全不同的。

Let's say you have a CargoAircraft class and PassengerAircraft. It's so tempting to put them in one class and work "seamlessly", isn't it? But interactions with them (scheduling, booking, weight or capacity calculations) are completely different.

所以,把所有内容都放在一个类中你强制你自己在每一个方法中都有一堆IF子句,在管理器中增加额外的方法,更难调试,更大的数据库表。基本上你为什么花更多的时间来发展?只有两件事情:1)少加入2)较少的类名。

So, by putting everything in one class you force yourself to bunch of IF clauses in every method, to extra methods in Manager, to harder debugging, to bigger tables in the DB. Basically you make yourself spend more time developing for the sake of what? For only two things: 1) less joins 2) less class names.

如果你分开课程,事情会变得容易一些:

If you separate the classes, things go much easier:


  • ,没有丑陋的ifs,no .getattr和默认值

  • 简单调试

  • 更多可维护数据库

因此,开发更快。

这篇关于通过将Django模型/表分解成两个模型/表,是否具有性能优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆