Skip to content

Commit

Permalink
Switch to bulk object creation
Browse files Browse the repository at this point in the history
Massively improves performance by creating objects in bulk
  • Loading branch information
aryanpingle committed Feb 19, 2023
1 parent 27e0f4a commit 663206d
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions swd/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,10 @@ def add_new_students(request):
created = False
idx = 1
header = {}
# Create a list of objects to be created (For bulk creation)
creation_list = []
# creation_threshold -> Arbitrary maximum number of objects to be stored in memory before creating in bulk
creation_threshold = 150
for sheet in workbook.sheets():
for row in sheet.get_rows():
if idx == 1:
Expand Down Expand Up @@ -2495,15 +2499,26 @@ def add_new_students(request):
parentName=str(row[header['fname']].value)[:50],
parentPhone=str(row[header['parent mobno']].value)[:20],
parentEmail=str(row[header['parent mail']].value)[:50])
obj.save()
# Add to creation list
creation_list.append(obj)

if len(creation_list) == creation_threshold:
# Create these student objects in bulk
Student.objects.bulk_create(creation_list)
# print(f">> CREATED IN BULK (at {count_created+1})")
creation_list.clear()

created = True
if created:
count_created = count_created + 1
else:
count = count + 1
except Exception:
message_str + studentID + " failed"


# Create these student objects in bulk
Student.objects.bulk_create(creation_list)

message_str = str(count_created) + " new students added," + "\n" + str(count) + " students updated."
else:
message_str = "No File Uploaded."
Expand Down Expand Up @@ -2681,6 +2696,10 @@ def update_hostel(request):
count = 0
idx = 1
header = {}
# Create a list of objects to be created (For bulk creation)
creation_list = []
# creation_threshold -> Arbitrary maximum number of objects to be stored in memory before creating in bulk
creation_threshold = 150
for sheet in workbook.sheets():
for row in sheet.get_rows():
if idx == 1:
Expand Down Expand Up @@ -2739,12 +2758,29 @@ def update_hostel(request):
room = str(row[header['Room']].value)
else:
room = ''
HostelPS.objects.create(student=student, hostel=new_hostel, room=room, acadstudent=acadstudent, status=status, psStation="")

# If a student is there in the sheet but not in the database, ignore
if(student == None):
continue

hostelps = HostelPS(student=student, hostel=new_hostel, room=room, acadstudent=acadstudent, status=status, psStation="")
creation_list.append(hostelps)

if len(creation_list) == creation_threshold:
# Create these hostelps objects in bulk
HostelPS.objects.bulk_create(creation_list)
# print(f">> CREATED IN BULK (at {count_created+1})")
creation_list.clear()

count = count + 1
if message_str is not '':
messages.add_message(request,
message_tag,
message_str)

# Create these student objects in bulk
HostelPS.objects.bulk_create(creation_list)

message_str = str(count) + " Updated students' hostel"
else:
message_str = "No File Uploaded."
Expand Down

0 comments on commit 663206d

Please sign in to comment.