The Tale of a PR Review and the Evolution of a Database Query



  1. A filter applied to each published course as the template looped through the queryset. The feedback: filter: bad. It calls the database twice for each PublishedCourse: bad. (I’m paraphrasing, obviously. My team is kinder than that).
  2. I moved the logic into the view and sent each published course into the template via the context already annotated. But my logic didn’t change much, so it wasn’t actually any better — I hit the database the same number of times. The feedback: Do a single database query to pull the relevant data (actually 2 database queries, since there are two relevant sets of objects that need to be collected for the user, but that’s better than two queries per PublishedCourse), and organize it into dictionaries to be queried.
  3. Pull one set of a data into a dict and the other into a list, per the above feedback, and query it from there. The feedback: Since I was querying the dataset in a list for existence of an object, use a set instead. I also had some weird variable naming going on — my team was rightfully confused and called me out on it.
  4. At this point I was basically there. The final piece of feedback I received was this: “When you do, the ORM lazily does a SELECT on the Course. When you do published_course.course_id, it doesn't need to do this lookup, because it has already cached the course_id for each PublishedCourse object. Main point: ORM eagerly downloads an object’s attributes (including related object’s IDs), but lazily downloads object’s related objects.”

Final Thoughts

Senior Software Engineer |

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrienne Domingus

Adrienne Domingus

Senior Software Engineer |

More from Medium

Your code is disposable, for your own good

How Doing Queries In Views Can Degrade The Performance Of Your Application

Coding is Storytelling

UNIT TESTING in python: