Master Django Performance

Explore deadly ruins while learning to make Django apps blazing fast. Veteran Python developer Andrew Brookins guides you through dangers in this book on database performance tuning.

Buy Now Learn More

Available in PDF, EPUB, MOBI.

The cover image of the book

About the Book

Want to be an expert Django developer? Master performance. An expert can jump into any Django project, profile slow pages and APIs, and tune the underlying database queries.

Caching can only take you so far. You need to go deeper, to the source. And most performance problems in Django start in the same place. The database.

But database performance tuning in Django involves a dizzying number of ORM API methods. And all this rests on top of the already complex machinery of SQL queries and indexes.

To fully master database performance with Django, reading through API documentation is not enough. You need to become an expert. Here are some examples of what an expert knows that this book will teach you:

  • How to use profiling and measurement tools like New Relic, Django Debug Toolbar, and database query plans to see exactly how badly a query performs in production

  • How to eyeball a query and know exactly the right index to speed it up (a covering index with INCLUDE? a partial index? a GIN index? You’re going to tell me by the end of the book because YOU will be the expert)

  • How to use advanced techniques like server-side cursors with iterator(), custom Func() expressions, and keyset pagination with row comparisons to churn through millions of rows of data with ease

You will learn all this and more through over 160 pages, 60 examples, and 30 quiz questions. By the end, you won’t ever think of a Django ORM query the same again: it will exist simultaneously in your mind as an operational metric, ORM syntax, generated SQL, and a database execution plan. Because you will be an expert.

I can teach this because I have over ten years of experience working with Django and relational databases, both as an application developer and a platform engineer building databases as a service for Compose.com and IBM Cloud.

And did I mention that you’re going to have fun? Yes, you read that right. This book is chock full of awesome fantasy art including a map of each chapter’s core concepts. Why fantasy art? Because I play tabletop role-playing games and I require books to be fun.

So, gaze upon the Sacrificial Cliff of Profiling. Get lost in the Labyrinth of Indexing. Ransack the Crypt of Querying. Then get back to kicking ass and saving the world from unbounded queries!

A man holding a torch whose flame takes the shape of a Phoenix

Audience

Django and Python Experience

This book is written for Django developers who use a relational database and want to push their database-related code as fast as it can go. You should have some experience with Python and Django.

We'll dive into the QuerySet API and look at how methods like only(), defer(), and iterator() translate into SQL statements and consume Python process memory. We also explore ways to use these API methods and others more effectively from templates, views, and management commands.

Relational Databases and SQL

The focus of this book is on relational databases like PostgreSQL, MySQL, and SQLite. These databases are accessible through the core models and migrations systems provided with Django.

You should have experience with relational databases and SQL. However, the reason this book exists is to level up your knowledge of these two topics, so we will spend a good amount of time discussing them. Over the next 100+ pages, we will talk often about databases and SQL, discussing how to build efficient database indexes, uses of different types of index, partial indexes, covering indexes, table clustering, types of JOIN statements, and more.

Structure of the Book

Learning Tools

This book contains technical content like conceptual overviews, explanations of performance problems, and annotated source code examples using Python and SQL code.

This content is the meat of the book: everything you need to know to become an expert in database performance with Django.

But that’s not all! One of the most effective learning tools verified by scientific research is spaced repetition, a memorization technique. That is why this book comes with quiz questions at the end of each chapter that you can turn into index cards or add to your spaced repetition app. Quizzing yourself is extremely effective at helping you recall the material!

A sample of code from the book
A sample of code from the book

The Fantastic

Technical books can be dry. You're flipping the pages, and you forget what the chapter is even about.

Why not spice things up a bit?

That's why this book mixes in a a fantasy adventure in which you explore the Temple of Django Database Performance. As you read, you will encounter different areas of the Temple and its grounds:

  • The Sacrificial Cliffs of Profiling

  • The Labyrinth of Indexing

  • The Crypt of Querying

Each of these areas relates to a chapter in the book. Every chapter has an illustrated fantasy map that touches on the core technical concepts within the chapter and also relates to the fantasy adventure.

And just as they might in a fantasy adventure, these areas contain Creatures, Traps, and Artifacts, each of which is illustrated with a piece of fantasy art and some text within the chapter.

These aspects of the book are not merely included for fun. They add an important learning dimension in the form of visualizations and metaphors to make the content more engaging and memorable.

An image showing three maps in the book

The Example Application and Database

Instead of abstract code examples, this book uses a Django application called Quest.

Quest has a mix of some pages rendered purely by Django (e.g., the login form and admin pages) and others rendered partly by Django and partly by React components. It has a REST API powered by Django Rest Framework that the React components consume.

The focus in this book is on code that interacts with the database. That will keep us solidly in Django models and views. At times, we will dip into template rendering and touch on client interactions with the API, but that will be the extent of our work in the front-end.

Data for the application is stored in a PostgreSQL database and accessed through Django models. Many of the SQL and ORM examples used in this book will be database-agnostic, but at times we will use PostgreSQL-specific features. At these times, we will discuss how the feature works in other databases, but you may need to look up the details for your particular database.

Perhaps most importantly, most of the code examples are unit tested and these tests appear in the examples when they help explain complexity.

Get 5 Free Django Performance Tips

ebook preview

Get 5 tips from the book delivered to your inbox. I'll send you one tip a day for five days.

You'll learn how to:

  • Read a database query plan
  • Make your index migrations fast
  • Index JSON data effectively
  • Query large amounts of data efficiently
  • Express complex queries with the ORM

These tips will jump-start your mastery of Django performance. Once you sign up, you'll get your first tip within minutes!

Webinar Preview

Check out a live webinar of content from the book, recorded with JetBrains.

What Our Readers Are Saying

The Temple of Django Database Performance is a must read for anyone working with Django. You’ll learn about profiling, query plans, indexing, and the Django ORM. Very approachable for those with only modest experience but even the experts should learn something new.

Face of Tristan Waddington

Tristan Waddington

Developer Experience Lead

[M]oney well spent. The Temple of Django Database Performance was a great read. ... I enjoyed the technical tidbits as much as I enjoyed the accompanying fantasy adventure.

The face of Modasser Billah

Modasser Billah

Senior Backend Developer

Pricing

Ebook

$29
  • Example Code
  • PDF, EPUB, and Mobi
  • Full-color Print Book

Print

$39
  • Example Code
  • Full-color Print Book
  • PDF, EPUB, and Mobi

Coaching Package

$199
  • Ebook
  • 1:1 Coaching Session
  • Detailed, Personal Learning Plan

About the Author

A photo of the author looking very tired and holding his newborn daughter, Elena

Andrew Brookins


I have over ten years of experience working with Django and relational databases. That experience spans DevOps, application development, and platform engineering.

I've worked on ecommerce sites, mobile APIs, comic book readers, particle simulation systems, learning and volunteering apps, and Databases as a Service. Today I'm a Software Engineer at Redis Labs.

The constant across all of these domains? Databases and application performance are always intertwined! Time and again, I've applied the knowledge from this book to solve performance problems in Django applications by focusing my attention on profiling, indexes, and querying techniques.

Here's what I ask of you: let me share this experience, so that someone other than my employers and me can benefit from it. I'll rest easier knowing that I've successfully passed the Torch of Performance-Tuning.

Help!

Questions about your order?

Problems accessing the files? Corrections, typos, or feature requests? Just email Andrew!

Looking for the example project?

The project is available on GitHub.

Looking for student or teacher pricing?

We love students! Email us from your educational institution address for current educational pricing.

Corrections

This section lists corrections that new editions have made to errors in older editions.

The N+1 performance problem

  • The first edition identified the N in the "N+1 performance problem" as referring to the initial query, and the "+1" as the additional queries for related records in each iteration of a loop over the results. That is backwards! N refers to each query for a related record, and +1 refers to the initial query. Thanks, Charlie!