简体   繁体   English

单个Django模型中的多个数据库表

[英]Multiple db tables from a single Django model

Let's say there's some kind of car rent/sell aggregator. 假设有某种租车/售车聚合器。 It works with many service providers (that provide cars to the customers) and the customers themselves. 它与许多服务提供商(向客户提供汽车)和客户本身合作。

Django way to do models for this kind of system would be something like this: Django为这种系统建立模型的方式如下:

class Vendor(models.Model):
    name = models.CharField()

class Car(models.Model):
    vendor = models.ForeignKey(Vendor,
                               blank=False,
                               null=False,
                               related_name='cars',
                               on_delete=models.CASCADE)
    license_plate = models.CharField(max_length=10, blank=False, null=False)

Now, we would proceed to add customer model, probably with m2m field pointing to Cars through the table with dates of rent or whatever. 现在,我们将继续添加客户模型,其中可能有m2m字段通过带有租用日期或其他日期的表格指向“汽车”。 All Vendors would have access only to their Cars even though all the cars are in one table, they would share one table for customers, etc. 即使所有汽车都在一张桌子中,所有供应商也只能访问他们的汽车,他们将为客户共享一张桌子,等等。

Assuming hypothetical scenario of there being a few vendors, say, a dozen, but each of them having a lot of cars - in the millions, my questions are: 假设有几个供应商,例如十几个,但是每个供应商都有很多汽车,在数百万的情况下,我的问题是:

Would there be any benefit in attempting to split the Cars model into multiple tables (despite django's principle one table - one model, maybe there are benefits on the DB side)? 尝试将Cars模型拆分为多个表会不会有任何好处(尽管django的原则是一张表-一个模型,也许在DB方面会有好处)?

I'm talking about splitting by vendor - ie each vendor having their own car table. 我说的是按供应商划分-即每个供应商都有自己的车位表。

I kinda think that if each car had some description, like 我有点想如果每辆车都有一些描述,例如

desc = models.CharField(max_length=500, blank=True)

then splitting could maybe simplify indexing? 那么拆分可能会简化索引编制? Dunno. 不知道。 Would be grateful if someone could clarify this. 如果有人可以澄清这一点,将不胜感激。

Anyway, even if there's no real benefit in this, let's say I decided I really need to do this - glue multiple tables to one Django model. 无论如何,即使这样做没有真正的好处,也可以说我决定确实需要这样做-将多个表粘贴到一个Django模型上。 Is this even possible? 这有可能吗? My thinking is to try SQLAlchemy maybe and see where this goes. 我的想法是尝试使用SQLAlchemy并查看结果。

I'd love to see any insights or ideas how you would approach such a problem. 我很乐意看到关于如何解决此类问题的任何见解或想法。 Or links, if there are articles on similar stuff. 或链接(如果有类似内容的文章)。

Depends what you're optimizing for. 取决于您要优化的内容。 If you want fast access to some unique fields, like the car VIN, for example, an index is helpful. 例如,如果您想快速访问一些独特的字段,例如汽车VIN,则索引会有所帮助。 But, generally speaking, the database will handle the performance optimizations of your queries. 但是,通常来说,数据库将处理查询的性能优化。

If your table gets really large (billions of records), you could look into what Instragram did with database sharding . 如果您的表真的很大(数十亿条记录),您可以研究一下Instragram对数据库分片所做的事情 That was a cool example of how to split a table across multiple database instances (using PostgreSQL, but the same would work with any relational database). 那是一个如何在多个数据库实例之间拆分表的很棒的示例(使用PostgreSQL,但对任何关系数据库都适用)。

DB table partitioning needs a good design and proper queries. 数据库表分区需要良好的设计和适当的查询。 There is a python app Architect which works with django. 有一个与django一起使用的python app Architect

Schemaless DB is another approach for massive data, Uber is known to be using it. 无模式数据库是处理海量数据的另一种方法,众所周知,Uber正在使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM