1

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.CharField(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
  • Actually, it is equivalent to store in the same table. You just need to use the query to get those rows you need. You can't benefit much from doing in that way. Commented Jan 18, 2011 at 21:37

1 Answer 1

2
  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.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that the performances drops with this table as it is filled with new records. Now there are more than 10 millions of records and it will be tens of millions more. The system communicates with various devices and logs data to this table. I'm considering to split this table for each device to have it's own. DB is MySQL, fine tuning is not an option since it's hosted on shared hosting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.