I have the following loop:
for row in rows:
print row.value('customer', 'name')
# print [customer for customer in customers if customer['name'] == row.value('customer', 'name')]
When I uncomment the second print, the loop runs much more slowly. Is there a way I can write this to make it perform faster?
My overall problem is that I used to do a separate SQL query for each row to get the matching customer for that row. Now I’m trying to grab all the customers up front in an effort to improve performance.
One row of customers looks like this, FYI:
{'name': u'JOHN SMITH', 'created_at': datetime.datetime(2010, 12, 1, 14, 49, 57), 'updated_at': datetime.datetime(2011, 3, 3, 16, 41, 1), 'customer_number': u'C102340', 'phone': u'', 'social_security_number': u'2352352', 'do_not_mail': None, 'id': 4154L, 'deceased': None}
“It runs much more slowly” because you are looking at the entire customer-list for each row.
Either make a dictionary of customers and use a lookup:
or modify your SQL query to return relevant customer information with each row (something like
row INNER JOIN customer ON row.customer_id==customer.id)