BABZI
BABZI

Reputation: 119

slow process during saving model in database

i want save a list of model in database table but i very slow when i use save() method for each item it took near 20min is that a best way to save objects to table

Modles.py

class Part(models.Model):
    block           = models.CharField(max_length= 2, null= True)
    phase           = models.CharField(max_length= 3, null= True)
    department      = models.CharField(max_length= 20, null= True)
    type            = models.CharField(max_length= 10, null= True)
    mark            = models.CharField(max_length= 20, null= True)


class Task(models.Model):
    name                = models.CharField(max_length= 20)


class ProjectTask(models.Model):
    project         = models.ForeignKey('Project', on_delete= models.CASCADE)
    task            = models.ForeignKey("Task", on_delete=models.CASCADE)
    weight_percent  = models.FloatField()

class PartTask(models.Model):
    part            = models.ForeignKey('Part', on_delete= models.CASCADE)
    project_task    = models.ForeignKey('ProjectTask', on_delete= models.CASCADE)
    progress        = models.FloatField(null=True)

views.py

def import_part_task(_project_id):
    project_id = _project_id
    project_task    = ProjectTask.objects.all().filter(project= int(project_id[0]))
    part_list       = Part.objects.all()
    part_task_list  = []

    for part in part_list:
        for task in project_task:
            part_task = PartTask()
            part_task.part =part
            part_task.project_task = task
            part_task_list.append(part_task)

   #This ACTION TAKES  VERY LOG TIME
    for part_task in part_task_list:
        PartTask.save(part_task)
    

Upvotes: 2

Views: 168

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477265

That makes perfect sense, since saving the database means that you each time query the database. This takes significant time.

You can however boost performance by inserting with bulk_create(..) [Django-doc]:

def import_part_task(_project_id):
    project_id = _project_id
    project_task = ProjectTask.objects.filter(project= int(project_id[0]))
    part_list = Part.objects.all()
    part_task_list = [
        PartTask(part=part, project_task=task)
        for part in part_list
        for task in project_task
    ]
    PartTask.objects.bulk_create(part_task_list)

By inserting in bulk, Django will create a query to insert a large amount of objects with a single query, instead of each time making a query for each individual PartTask object. The amount of "round trips" to the database is thus reduced significantly.

Upvotes: 2

Related Questions