开发者

Variable table name in Django

开发者 https://www.devze.com 2023-02-05 22:04 出处:网络
Can I use variable table name for db mapped objects? For example, there are n objects of the same structure and I want to store it in different tables, for raising performance on some operations.

Can I use variable table name for db mapped objects? For example, there are n objects of the same structure and I want to store it in different tables, for raising performance on some operations.

Let's say I've got class defined as:

class Measurement(models.Model):
    slave_id = models.IntegerField()
    tag = models.C开发者_如何学JAVAharField(max_length=40)
    value = models.CharField(max_length=16)
    timestamp = models.DateTimeField()

    class Meta:
        db_table = 'measurements'

Now all objects are stored into table 'measurements'. I would like to make table name dependant on 'slave_id' value. For example, to handle data from tables 'measurements_00001', 'measurements_00002' etc...

Is it possible to achieve this using Django ORM model or the only solution is to drop to SQL level?


  1. In the vast majority of cases, this shouldn't buy you any performance advantage. Any RDBMS worth its salt should handle immense tables effortlessly.

  2. If it's needed, there could be some sharding of the table. Again, managed by the DB server; at SQL level (and ORM) it should be seen as a single table. Ideally, the discrimination should be automatically handled; if not, most RDBMS let you specify it at table definition time (or sometimes tune with ALTER TABLE)

  3. If you choose to define the sharding method, each RDBMS has it's own non-standard methods. Best not to tie your Python code to that; do the tuning once on the DB server instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消