(Note: Even if you don't know Django, it's probably the query that needs work anyway, so you probably don't need to know Django/Python)
I have a ListView
that displays all the entries in a mapping table between the pizza
table and the topping
table. Unfortunately, I can't just use Django's helpfulness and just give it the model to use (I'm skipping why for the sake of brevity), and have to use get_queryset
instead. Unfortunately again, the load time is slow - there are almost 700 pizzas in the table and I need to use it in my sql query to order by pizza name. Here's what I'm currently doing:
class MapPizzaToppingListView(ListView):
model = MapPizzaTopping
paginate_by = 20
template_name = 'onboardingWebApp/mappizzatopping_list.html'
def get_queryset(self):
cursor = connections["default"].cursor()
cursor.execute("""SELECT map.*
FROM map_pizza_topping as map
INNER JOIN pizza
ON (map.pizza_id = pizza.id)
ORDER BY pizza.name""")
queryset = dictfetchall(cursor)
for row in queryset:
pizza = Pizza.objects.get(id=row['pizza_id'])
topping = Topping.objects.get(id=row['topping_id'])
row['pizza_name'] = "%s (%s)" % (pizza.name, pizza.merchant.name)
row['topping_name'] = topping.name
return queryset
It takes a few seconds to load now, but we're going to be adding tons more pizzas eventually, so this really needs to be sped up. Unfortunately, my MySQL knowledge, though decent, isn't really to a point where I know how to make it better. What can I do to improve it?
Some more information that could be useful - the map_pizza_topping
table just has a pizza_id
column, a topping_id
column, and a quantity
column. The pizza
table just has name
, price
, customer_id
, and merchant_id
columns, and the topping
table just has name
and is_dairy
columns.