-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize multibuy database queries #469
base: next
Are you sure you want to change the base?
Optimize multibuy database queries #469
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to run it locally to check things over, and verify that we have tests in place
@@ -128,15 +128,15 @@ def execute(self): | |||
self.member = Member.objects.select_for_update().get(id=self.member.id) | |||
self.member.fulfill(transaction) | |||
|
|||
# Collect all the sales of the order | |||
Sales = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sales = [] | |
sales = [] |
for i in range(item.count): | ||
s = Sale(member=self.member, product=item.product, room=self.room, price=item.product.price) | ||
s.save() | ||
Sales.append(s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sales.append(s) | |
sales.append(s) |
I'll try to review this before summer ends. Probably after the current update has been finished |
@Mast3rwaf1z Vil du ikke være en king og review den her så Kresten kan få lidt fritid? |
|
Tager lige et kig senere, var lige til forelæsning |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small things, i think improves readability of the code, and removed some redundant constructor calls?
also i believe by using _
in a for loop prevents memory being allocated to the variable. really insignificant but hey, its good to have imo
s.save() | ||
Sales.append(s) | ||
# Save all the sales | ||
Sale.objects.bulk_create(Sales) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sale.objects.bulk_create(Sales) | |
Sale.objects.bulk_create(sales) |
if str(unique_id) not in unique_product_dict: | ||
unique_product_dict[str(unique_id)] = 1 | ||
else: | ||
unique_product_dict[str(unique_id)] += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if str(unique_id) not in unique_product_dict: | |
unique_product_dict[str(unique_id)] = 1 | |
else: | |
unique_product_dict[str(unique_id)] += 1 | |
if unique_id not in unique_product_dict: | |
unique_product_dict[unique_id] = 1 | |
else: | |
unique_product_dict[unique_id] += 1 |
what's the point in converting from an int object to string all the time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can also optimize it further by just using dict comprehension:
unique_product_dict = {i:bought_ids.count(i) for i in bought_ids}
for i in unique_product_dict: | ||
product = Product.objects.get( | ||
Q(pk=i), | ||
Q(active=True), | ||
Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), | ||
Q(rooms__id=room.id) | Q(rooms=None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for i in unique_product_dict: | |
product = Product.objects.get( | |
Q(pk=i), | |
Q(active=True), | |
Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), | |
Q(rooms__id=room.id) | Q(rooms=None), | |
for key, value in unique_product_dict.items(): | |
product = Product.objects.get( | |
Q(pk=key), | |
Q(active=True), | |
Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), | |
Q(rooms__id=room.id) | Q(rooms=None), |
product = Product.objects.get( | ||
Q(pk=i), | ||
Q(active=True), | ||
Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), | ||
Q(rooms__id=room.id) | Q(rooms=None), | ||
) | ||
products.append(product) | ||
products.extend([product for x in range(unique_product_dict[str(i)])]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
products.extend([product for x in range(unique_product_dict[str(i)])]) | |
products.extend([product for _ in range(value)]) |
Fixes #357.