Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

Is it better to have several different tables for similar objects, or one big table?

I have an optimisation problem for my tables :

I have Items that can have a price by one, by ten and by hundred.

Right now i have three tables for that :

class Prix_x1(models.Model):
    def __str__(self):
        return self.item.name

    prix = models.IntegerField(null=True)
    saved_at = models.DateTimeField()
    item = models.ForeignKey(Item, on_delete=models.CASCADE)


class Prix_x10(models.Model):
    def __str__(self):
        return self.item.name

    prix = models.IntegerField(null=True)
    saved_at = models.DateTimeField()
    item = models.ForeignKey(Item, on_delete=models.CASCADE)


class Prix_x100(models.Model):
    def __str__(self):
        return self.item.name

    prix = models.IntegerField(null=True)
    saved_at = models.DateTimeField()
    item = models.ForeignKey(Item, on_delete=models.CASCADE)

But often i need to check which of these prices is the lowest on a per unit basis. So i have to check each price table for each Item every time i need to do that.
So my question is : Is it a better way to optimse that ?

For exemple, is it better to have one big table Price with a field number_of_unit that can take 1, 10 or 100 as value ?

Thanks for your help !!

Upvotes: 0

Views: 32

Answers (2)

Adam R
Adam R

Reputation: 26

You could just have a spot for each price in the model:

class Prix(models.Model):
def __str__(self):
    return self.item.name

prix_x1 = models.IntegerField(null=True)
prix_x10 = models.IntegerField(null=True)
prix_x100 = models.IntegerField(null=True)

saved_at = models.DateTimeField()
item = models.ForeignKey(Item, on_delete=models.CASCADE)

Upvotes: 1

JuConte
JuConte

Reputation: 542

I think is better have one table

class Prix(models.Model):
    def __str__(self):
        return self.item.name

    prix = models.IntegerField(null=True)
    prix_x = models.IntegerField(null=True)
    saved_at = models.DateTimeField()
    item = models.ForeignKey(Item, on_delete=models.CASCADE)

save and increment prix and prix_x

Upvotes: 1

Related Questions