在模型上发布保存信号所需的时间太长

时间:2019-01-24 19:54:38

标签: django python-3.x django-models django-signals

我有一个名为partnershipArm的模型,当在没有发布后保存信号的情况下创建新模型时,它的运行速度非常快。实际上,当我只用20个成员填充数据集时,它运行良好,但是现在我有1168个成员,并且每次都超时。我如何使其运行更快。

Models.py

class PartnershipArm(models.Model):
name = models.CharField(max_length = 128)
slug = models.SlugField(unique = True, null=True, blank=True)
partnershipRecords = models.ManyToManyField(Member, through = 'Partnership') 

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('partnership-arms')

class Partnership(models.Model):

YEAR = []
for r in range((datetime.datetime.now().year), (datetime.datetime.now().year+10)):
    YEAR.append((r,r))
MONTHS = (
    ('January', 'January'),
    ('February', 'February'),
    ('March', 'March'),
    ('April', 'April'),
    ('May', 'May'),
    ('June', 'June'),
    ('July', 'July'),
    ('August', 'August'),
    ('September', 'September'),
    ('October', 'October'),
    ('November', 'November'),
    ('December', 'December'),
)
member = models.ForeignKey(Member, on_delete=models.CASCADE)
partnershipArm = models.ForeignKey(PartnershipArm, on_delete=models.CASCADE, db_index = True)
year = models.IntegerField( choices=YEAR, default=datetime.datetime.now().year, db_index = True)
month = models.CharField(max_length = 50, choices = MONTHS, null=True, blank=True, db_index = True)
week1 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week2 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week3 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week4 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
total = models.DecimalField(max_digits=10, decimal_places=2, default = 0)


def __str__(self):
    return "{0}_{1}_{2}_{3}".format(self.member, self.partnershipArm, self.year, self.month)

def get_absolute_url(self):
    return reverse('partnership-arms')

Signals.py

def post_save_partnershipArm_create_receiver(sender, instance=None, created=False,**kwargs):
members = Member.objects.all()
YEARS = []
for r in range((datetime.datetime.now().year), (datetime.datetime.now().year+10)):
    YEARS.append((r))
MONTHS = ['January','February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

if created:
    for member in members:
        for year in YEARS:
            for month, counter in enumerate(MONTHS, start = 0):
                realMonth = MONTHS[month]
                partnership = Partnership(member = member, partnershipArm = instance, year = year, month = realMonth)
                partnership.save()

1 个答案:

答案 0 :(得分:0)

您可以尝试使用bulk_create()批量保存Partnership对象。那应该减少应该加快速度的数据库插入操作的数量。

from itertools import islice

def post_save_partnershipArm_create_receiver(sender, instance=None, created=False,**kwargs):

    ...

    if created:
        partnerships = []  # To temporarily hold the Partnership instances
        batch_size = 100  # The number of Partnership instances to save at once

        # Create the Partnerships but don't save them yet
        for member in members:
            for year in YEARS:
                for month, counter in enumerate(MONTHS, start = 0):
                    realMonth = MONTHS[month]
                    partnership = Partnership(member = member, partnershipArm = instance, year = year, month = realMonth)
                    partnerships.append(partnership)

        # Save the Partnerships in batches
        while True:
            batch = list(islice(partnerships, batch_size))
            if not batch:
                break
            Partnership.objects.bulk_create(batch, batch_size)

您可能需要使用batch_size,因为使用更高的数字可能会获得更好的性能。

bulk_create()确实有一些限制,如docs中所述。