Django、多态性和 N+1 查询问题

发布于 2024-10-31 10:38:28 字数 376 浏览 3 评论 0原文

我正在 Django 中编写一个应用程序,我想在使用外键时利用隐式继承。就我而言,处理这个问题的唯一方法是使用 django_polymorphic 库(Django 中没有单表继承,为什么为什么?)。

我想了解该解决方案对性能的影响。进行多态查询时执行什么类型的连接?与常规查询相比,它是否必须多次访问数据库(臭名昭著的 N+1 查询问题)?文档警告说“现代 RDBM 无法有效处理所执行的查询类型”?然而,它并没有真正告诉我们这些查询是什么。任何统计数据、经验都会非常有帮助。

编辑:

是否有任何方法来检索对象列表,每个对象都是其实际类的实例,具有恒定数量的查询?我认为这就是前面提到的库所做的,但是现在我很困惑,我不再那么确定了。

I'm writing an app in Django where I'd like to make use of implicit inheritence when using ForeignKeys. As far as I'm concerned the only way to handle this nicely is to use django_polymorphic library (no single table inheritence in Django, WHY OH WHY??).

I'd like to know about the performance implications of this solution. What kind of joins are performed when doing polymorphic queries? Does it have to hit the database multiple times as compared to regular queries (the infamous N+1 queries problem)? The docs warn that "the type of queries that are performed aren't handled efficiently by the modern RDBMs"? However it doesn't really tell what those queries are. Any statistics, experiences would be really helpful.

EDIT:

Is there any way of retrieving a list of objects, each being an instance of its actual class with a constant number of queries ?? I thought this is what the aforementioned library does, however now I got confused and I'm not that certain anymore.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-11-07 10:38:28

Django-Typed-Models 是 Django-Polymorphic 的替代方案,它需要一个简单的 &解决单表继承问题的干净方法。它使用添加到模型中的“类型”属性。当您保存它时,该类将保留到“type”属性中。在查询时,该属性用于设置结果对象的类。

它按查询方式执行您所期望的操作(从查询集返回的每个对象都是向下转型的类),而不需要特殊的语法或与 Django 多态相关的可怕的代码量。并且没有额外的数据库查询。

Django-Typed-Models is an alternative to Django-Polymorphic which takes a simple & clean approach to solving the single table inheritance issue. It works off a 'type' attribute which is added to your model. When you save it, the class is persisted into the 'type' attribute. At query time, the attribute is used to set the class of the resulting object.

It does what you expect query-wise (every object returned from a queryset is the downcasted class) without needing special syntax or the scary volume of code associated with Django-Polymorphic. And no extra database queries.

你怎么敢 2024-11-07 10:38:28

好的,我进一步挖掘并发现了这段不错的段落:

https://github.com/bconstantin/django_polymorphic/blob/master/DOCS.rst#performance-considerations

很高兴这个库做了一些相当理智的事情。很高兴知道这一点。

Ok, I've digged a little bit further and found this nice passage:

https://github.com/bconstantin/django_polymorphic/blob/master/DOCS.rst#performance-considerations

So happily this library does something reasonably sane. That's good to know.

灰色世界里的红玫瑰 2024-11-07 10:38:28

在 Django 中,继承的模型在内部通过 OneToOneField 表示。如果您使用 select_lated() 在查询中,Django 将向前和向后遵循一对一的关系,以包含带有连接的引用表;因此,如果您使用 select_lated,则无需两次访问数据库。

In Django inherited models are internally represented through an OneToOneField. If you are using select_related() in a query Django will follow a one to one relation forwards and backwards to include the referenced table with a join; so you wouldn't need to hit the database twice if you are using select_related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文