SlideShare a Scribd company logo
1 of 30
Download to read offline
Joseph Victor Zammit
Thinking in SQL with Django
Django Day Copenhagen 2022
@jvzammit
Why SQL?
Technology
"built to last"
in 1973:
Lindy's Restaurant at Broadway and 52nd Street in NYC https://en.wikipedia.org/wiki/Lindy_e
ff
ect
Tools used to inspect SQL:
1. Built-in: MyModel.objects.explain()
2. Built-in: print(MyModel.objects.all().query)
3. django-extensions: shell_plus —print-sql
4. Django debug toolbar
Additional requirement:
As a customer I want to see:
- a (V) next to vegetarian pizza, and
- a (VG) next to vegan pizza.
Classifying a Pizza as (VG) or (V):
A Pizza is vegan (VG) if all its Toppings are vegan.
A Pizza is vegetarian (V) if all its Toppings are vegan or vegetarian.
Topping: Approach 1
Topping: Approach 2
N+1 or 1+N ?
prefetch_related FTW!
Nice! But how to ensure we do not lose this "gain"?
assertNumQueries FTW!
Is this a case of "Premature …" ? 🤔
Left Outer Joins .. 😱
Shouldn’t we be able to get the "max" topping rating with one query* on the "through" table if we know the Pizza id?
* I mean, one query without LEFT OUTER JOINS 😅
How can we use this aggregate query within our main query to fetch Pizzas?
How do we add max_rating to our Pizzas queryset?
* And avoid the LEFT OUTER JOIN :))
Does Django provide a Subquery-friendly way in which to get max?
0 = VG
1 = V
2 = neither VG nor V
Bye bye HashAggregate!
The constructs Django provides
What did we just do?
• Relational modeling taking querying into consideration
• Model managers to achieve DRY in querying
• Django ORM: queryset prefetch_related and select_related
• Django ORM: queryset conditional expressions (Case..when)
• Django ORM: queryset aggregation
• Django ORM: Subquery and OuterRef classes
• Django ORM: querying the M:N "through" table directly
• Unit tests: assertNumQueries + allowing us to tweak SQL while still testing logic
Together all these
• control the SQL generated by the ORM (RawSQL as a last resort)
• allow DRY
• allow unit testing all business logic within Django project
Conclusion: know your SQL
Tak!
Thank you audience!
Thank you organisers!
Joseph Victor Zammit
@jvzammit

More Related Content

Similar to Thinking in SQL with Django

Behat Workshop at WeLovePHP
Behat Workshop at WeLovePHPBehat Workshop at WeLovePHP
Behat Workshop at WeLovePHP
Marcos Quesada
 
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
Fred Sauer
 

Similar to Thinking in SQL with Django (20)

Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
GWTcon 2015 - Beyond GWT 3.0 Panic
GWTcon 2015 - Beyond GWT 3.0 PanicGWTcon 2015 - Beyond GWT 3.0 Panic
GWTcon 2015 - Beyond GWT 3.0 Panic
 
Behat Workshop at WeLovePHP
Behat Workshop at WeLovePHPBehat Workshop at WeLovePHP
Behat Workshop at WeLovePHP
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
Verdades e mentiras sobre teste de software no mundo magico do agile
Verdades e mentiras sobre teste de software no mundo magico do agileVerdades e mentiras sobre teste de software no mundo magico do agile
Verdades e mentiras sobre teste de software no mundo magico do agile
 
PSPDFKit on Android
PSPDFKit on AndroidPSPDFKit on Android
PSPDFKit on Android
 
Webinar: End to End Security & Operations with Chainguard and Weave GitOps
Webinar: End to End Security & Operations with Chainguard and Weave GitOpsWebinar: End to End Security & Operations with Chainguard and Weave GitOps
Webinar: End to End Security & Operations with Chainguard and Weave GitOps
 
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
 
Mutation testing pixels camp 2019
Mutation testing   pixels camp 2019Mutation testing   pixels camp 2019
Mutation testing pixels camp 2019
 
des mutants dans le code.pdf
des mutants dans le code.pdfdes mutants dans le code.pdf
des mutants dans le code.pdf
 
Adding Modularity Afterward with Embedded OSGi
Adding Modularity Afterward with Embedded OSGiAdding Modularity Afterward with Embedded OSGi
Adding Modularity Afterward with Embedded OSGi
 
Whats New In Groovy 1.6?
Whats New In Groovy 1.6?Whats New In Groovy 1.6?
Whats New In Groovy 1.6?
 
Introduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debuggerIntroduction of Tools for providing rich user experience in debugger
Introduction of Tools for providing rich user experience in debugger
 
How to write Testable Javascript
How to write Testable JavascriptHow to write Testable Javascript
How to write Testable Javascript
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
 
Create A/B tests with the Google Tag Manager
Create A/B tests with the Google Tag ManagerCreate A/B tests with the Google Tag Manager
Create A/B tests with the Google Tag Manager
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
Continuous delivery is more than dev ops
Continuous delivery is more than dev opsContinuous delivery is more than dev ops
Continuous delivery is more than dev ops
 
MockServer-driven testing
MockServer-driven testingMockServer-driven testing
MockServer-driven testing
 

Recently uploaded

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

Thinking in SQL with Django

  • 1. Joseph Victor Zammit Thinking in SQL with Django Django Day Copenhagen 2022 @jvzammit
  • 4. Lindy's Restaurant at Broadway and 52nd Street in NYC https://en.wikipedia.org/wiki/Lindy_e ff ect
  • 5.
  • 6. Tools used to inspect SQL: 1. Built-in: MyModel.objects.explain() 2. Built-in: print(MyModel.objects.all().query) 3. django-extensions: shell_plus —print-sql 4. Django debug toolbar
  • 7. Additional requirement: As a customer I want to see: - a (V) next to vegetarian pizza, and - a (VG) next to vegan pizza. Classifying a Pizza as (VG) or (V): A Pizza is vegan (VG) if all its Toppings are vegan. A Pizza is vegetarian (V) if all its Toppings are vegan or vegetarian.
  • 9.
  • 12. Nice! But how to ensure we do not lose this "gain"?
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. Is this a case of "Premature …" ? 🤔 Left Outer Joins .. 😱
  • 19. Shouldn’t we be able to get the "max" topping rating with one query* on the "through" table if we know the Pizza id? * I mean, one query without LEFT OUTER JOINS 😅
  • 20. How can we use this aggregate query within our main query to fetch Pizzas? How do we add max_rating to our Pizzas queryset? * And avoid the LEFT OUTER JOIN :))
  • 21. Does Django provide a Subquery-friendly way in which to get max?
  • 22. 0 = VG 1 = V 2 = neither VG nor V
  • 23.
  • 24.
  • 26.
  • 27. The constructs Django provides What did we just do? • Relational modeling taking querying into consideration • Model managers to achieve DRY in querying • Django ORM: queryset prefetch_related and select_related • Django ORM: queryset conditional expressions (Case..when) • Django ORM: queryset aggregation • Django ORM: Subquery and OuterRef classes • Django ORM: querying the M:N "through" table directly • Unit tests: assertNumQueries + allowing us to tweak SQL while still testing logic Together all these • control the SQL generated by the ORM (RawSQL as a last resort) • allow DRY • allow unit testing all business logic within Django project
  • 29.
  • 30. Tak! Thank you audience! Thank you organisers! Joseph Victor Zammit @jvzammit