SlideShare a Scribd company logo
A Related Matter: 
Optimizing your webapp by using django-debug-toolbar, 
select_related(), and prefetch_related() 
Christopher Adams 
DjangoCon 2014 
https://github.com/adamsc64/a-­‐related-­‐matter
Christopher Adams 
• Software Engineer at Venmo 
• Twitter/Github: @adamsc64 
• I’m not Chris Adams (@acdha), who works at 
Library of Congress 
• Neither of us are “The Gentleman” Chris Adams 
(90’s-era Professional Wrestler)
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
Django is great 
But Django is really a set of tools
Tools are great 
But tools can be used in good or bad ways
The Django ORM: 
A set of tools
Manage your own 
expectations for tools 
• Many people approach a new tool with broad set 
of expectations as to what the think it will do for 
them. 
• This may have little correlation with what the 
project actually has implemented.
As amazing as it 
would be if they did…
Unicorns don’t exist
The Django ORM: 
An abstraction layer
Abstraction layers 
• Great because they take us away from the 
messy details 
• Risky because they take us away from the 
messy details
Don’t forget 
You’re far from the ground
The QuerySet API
QuerySets are Lazy
QuerySets are 
Immutable
Lazy: Does not evaluate 
until it needs to
Immutable: Never 
itself changes
Each a new QuerySet, none 
hit the database 
• queryset 
= 
Model.objects.all() 
• queryset 
= 
queryset.filter(...) 
• queryset 
= 
queryset.values(...)
Hits the database 
(QuerySet is “evaluated”): 
• queryset 
= 
list(queryset) 
• queryset 
= 
queryset[:] 
• for 
model_object 
in 
queryset: 
do_something(...)
Our app: a blog
Models 
class Blog(models.Model):! 
submitter = models.ForeignKey('auth.User')! 
! 
class Post(models.Model):! 
blog = models.ForeignKey('blog.Blog', related_name="posts")! 
likers = models.ManyToManyField('auth.User')! 
! 
class PostComment(models.Model):! 
submitter = models.ForeignKey('auth.User')! 
post = models.ForeignKey('blog.Post', related_name="comments")!
List View 
def blog_list(request):! 
blogs = Blog.objects.all()! 
return render(request, "blog/blog_list.html", {! 
"blogs": blogs,! 
})!
List Template
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
Detail View 
def blog_detail(request, blog_id):! 
blog = get_object_or_404(Blog, id=blog_id)! 
posts = Post.objects.filter(blog=blog)! 
return render(request, "blog/blog_detail.html", {! 
"blog": blog,! 
"posts": posts,! 
})!
Detail Template
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
SQL Queries?
If you can’t measure 
it…
…you’d never know if 
there are problems.
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
First view: 
The blog list page
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
select_related() 
• select_related works by creating an SQL join and 
including the fields of the related object in the 
SELECT statement. 
• For this reason, select_related gets the related 
objects in the same database query. 
• However, to avoid the much larger result set that 
would result from joining across a ‘many’ 
relationship, select_related is limited to single-valued 
relationships - foreign key and one-to-one.
List View 
def blog_list(request):! 
blogs = Blog.objects.all()! 
blogs = blogs.select_related("submitter")! 
return render(request, "blog/blog_list.html", {! 
"blogs": blogs,! 
})!
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
Second view: 
The blog detail page
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
prefetch_related() 
• prefetch_related does a separate lookup for 
each relationship, and does the ’joining’ in 
Python. 
• This allows it to prefetch many-to-many and 
many-to-one objects … in addition to the 
foreign key and one-to-one relationships. 
• It also supports prefetching of GenericRelation 
and GenericForeignKey.
Detail View 
def blog_detail(request, blog_id):! 
blog = get_object_or_404(Blog, id=blog_id)! 
posts = Post.objects.filter(blog=blog)! 
posts = posts.prefetch_related(! 
“comments__submitter", "likers",! 
)! 
return render(request, "blog/blog_detail.html", {! 
"blog": blog,! 
"posts": posts,! 
})!
A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()
Summary 
• The QuerySet API methods select_related() and 
prefetch_related() automate some best practices 
to avoid extra queries in views/templates. 
• Use select_related() for one-to-many or one-to-one 
relations. 
• Use prefetch_related() for many-to-many or 
many-to-one (e.g. reverse foreign key) relations.
Thanks! 
Christopher Adams (@adamsc64) 
https://github.com/adamsc64/a-related-matter

More Related Content

Viewers also liked

Don't do this
Don't do thisDon't do this
Don't do this
Richard Jones
 
A Crush on Design Thinking
A Crush on Design ThinkingA Crush on Design Thinking
A Crush on Design Thinking
Matteo Burgassi
 
Enterprise architectsview 2015-apr
Enterprise architectsview 2015-aprEnterprise architectsview 2015-apr
Enterprise architectsview 2015-apr
MongoDB
 
How to use graphs to identify credit card thieves?
How to use graphs to identify credit card thieves?How to use graphs to identify credit card thieves?
How to use graphs to identify credit card thieves?
Linkurious
 
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
Neo4j
 
Exploring the Great Olympian Graph
Exploring the Great Olympian GraphExploring the Great Olympian Graph
Exploring the Great Olympian Graph
Neo4j
 
Presentation on Large Scale Data Management
Presentation on Large Scale Data ManagementPresentation on Large Scale Data Management
Presentation on Large Scale Data Management
Chris Bunch
 
Web valley talk - usability, visualization and mobile app development
Web valley talk - usability, visualization and mobile app developmentWeb valley talk - usability, visualization and mobile app development
Web valley talk - usability, visualization and mobile app development
Eamonn Maguire
 
CQRS & EVS with MongoDb
CQRS & EVS with MongoDbCQRS & EVS with MongoDb
CQRS & EVS with MongoDb
Lluis Fernandez
 
How to establish a sustainable solution for data lineage
How to establish a sustainable solution for data lineageHow to establish a sustainable solution for data lineage
How to establish a sustainable solution for data lineage
Leigh Hill
 
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
Neo4j
 
Km4City: how to make smart and resilient your city, beginner document
Km4City: how to make smart and resilient your city, beginner documentKm4City: how to make smart and resilient your city, beginner document
Km4City: how to make smart and resilient your city, beginner document
Paolo Nesi
 
The Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
The Five Graphs of Government: How Federal Agencies can Utilize Graph TechnologyThe Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
The Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
Greta Workman
 
Graphically understand and interactively explore your Data Lineage
Graphically understand and interactively explore your Data LineageGraphically understand and interactively explore your Data Lineage
Graphically understand and interactively explore your Data Lineage
Mohammad Ahmed
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Evandro Venancio
 
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
Chris Richardson
 
Samza at LinkedIn: Taking Stream Processing to the Next Level
Samza at LinkedIn: Taking Stream Processing to the Next LevelSamza at LinkedIn: Taking Stream Processing to the Next Level
Samza at LinkedIn: Taking Stream Processing to the Next Level
Martin Kleppmann
 
Km4city Smart City Ecosystem Urban Platform
Km4city Smart City Ecosystem Urban PlatformKm4city Smart City Ecosystem Urban Platform
Km4city Smart City Ecosystem Urban Platform
Paolo Nesi
 
Data Modeling with Neo4j
Data Modeling with Neo4jData Modeling with Neo4j
Data Modeling with Neo4j
Neo4j
 
Visualization of Publication Impact
Visualization of Publication ImpactVisualization of Publication Impact
Visualization of Publication Impact
Eamonn Maguire
 

Viewers also liked (20)

Don't do this
Don't do thisDon't do this
Don't do this
 
A Crush on Design Thinking
A Crush on Design ThinkingA Crush on Design Thinking
A Crush on Design Thinking
 
Enterprise architectsview 2015-apr
Enterprise architectsview 2015-aprEnterprise architectsview 2015-apr
Enterprise architectsview 2015-apr
 
How to use graphs to identify credit card thieves?
How to use graphs to identify credit card thieves?How to use graphs to identify credit card thieves?
How to use graphs to identify credit card thieves?
 
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
GraphConnect Europe 2016 - Creating an Innovative Task Management Engine - Mi...
 
Exploring the Great Olympian Graph
Exploring the Great Olympian GraphExploring the Great Olympian Graph
Exploring the Great Olympian Graph
 
Presentation on Large Scale Data Management
Presentation on Large Scale Data ManagementPresentation on Large Scale Data Management
Presentation on Large Scale Data Management
 
Web valley talk - usability, visualization and mobile app development
Web valley talk - usability, visualization and mobile app developmentWeb valley talk - usability, visualization and mobile app development
Web valley talk - usability, visualization and mobile app development
 
CQRS & EVS with MongoDb
CQRS & EVS with MongoDbCQRS & EVS with MongoDb
CQRS & EVS with MongoDb
 
How to establish a sustainable solution for data lineage
How to establish a sustainable solution for data lineageHow to establish a sustainable solution for data lineage
How to establish a sustainable solution for data lineage
 
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
How to Search, Explore and Visualize Neo4j with Linkurious - Jean Villedieu @...
 
Km4City: how to make smart and resilient your city, beginner document
Km4City: how to make smart and resilient your city, beginner documentKm4City: how to make smart and resilient your city, beginner document
Km4City: how to make smart and resilient your city, beginner document
 
The Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
The Five Graphs of Government: How Federal Agencies can Utilize Graph TechnologyThe Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
The Five Graphs of Government: How Federal Agencies can Utilize Graph Technology
 
Graphically understand and interactively explore your Data Lineage
Graphically understand and interactively explore your Data LineageGraphically understand and interactively explore your Data Lineage
Graphically understand and interactively explore your Data Lineage
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
Decompose that WAR? A pattern language for microservices (@QCON @QCONSP)
 
Samza at LinkedIn: Taking Stream Processing to the Next Level
Samza at LinkedIn: Taking Stream Processing to the Next LevelSamza at LinkedIn: Taking Stream Processing to the Next Level
Samza at LinkedIn: Taking Stream Processing to the Next Level
 
Km4city Smart City Ecosystem Urban Platform
Km4city Smart City Ecosystem Urban PlatformKm4city Smart City Ecosystem Urban Platform
Km4city Smart City Ecosystem Urban Platform
 
Data Modeling with Neo4j
Data Modeling with Neo4jData Modeling with Neo4j
Data Modeling with Neo4j
 
Visualization of Publication Impact
Visualization of Publication ImpactVisualization of Publication Impact
Visualization of Publication Impact
 

Similar to A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()

Art & music vs Google App Engine
Art & music vs Google App EngineArt & music vs Google App Engine
Art & music vs Google App Engine
thomas alisi
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
Salvatore Fazio
 
Django Overview
Django OverviewDjango Overview
Django Overview
Brian Tol
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
Mark Rackley
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScript
Joost Elfering
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
James Johnson
 
Riding the Edge with Ember.js
Riding the Edge with Ember.jsRiding the Edge with Ember.js
Riding the Edge with Ember.js
aortbals
 
Build a game with javascript (april 2017)
Build a game with javascript (april 2017)Build a game with javascript (april 2017)
Build a game with javascript (april 2017)
Thinkful
 
NinjaScript 2010-10-14
NinjaScript 2010-10-14NinjaScript 2010-10-14
NinjaScript 2010-10-14
lrdesign
 
Django at Scale
Django at ScaleDjango at Scale
Django at Scale
bretthoerner
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
Salesforce Developers
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
Anton Serdyuk
 
SPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have knownSPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have known
Mark Rackley
 
Google App Engine - exploiting limitations
Google App Engine - exploiting limitationsGoogle App Engine - exploiting limitations
Google App Engine - exploiting limitations
Tomáš Holas
 
Hibernate Tutorial for beginners
Hibernate Tutorial for beginnersHibernate Tutorial for beginners
Hibernate Tutorial for beginners
rajkamal560066
 
Data Migrations in the App Engine Datastore
Data Migrations in the App Engine DatastoreData Migrations in the App Engine Datastore
Data Migrations in the App Engine Datastore
Ryan Morlok
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
Doris Chen
 
fuser interface-development-using-jquery
fuser interface-development-using-jqueryfuser interface-development-using-jquery
fuser interface-development-using-jquery
Kostas Mavridis
 
Django Pro ORM
Django Pro ORMDjango Pro ORM
Django Pro ORM
Alex Gaynor
 
JavaScript!
JavaScript!JavaScript!
JavaScript!
RTigger
 

Similar to A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related() (20)

Art & music vs Google App Engine
Art & music vs Google App EngineArt & music vs Google App Engine
Art & music vs Google App Engine
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
 
Django Overview
Django OverviewDjango Overview
Django Overview
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScript
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Riding the Edge with Ember.js
Riding the Edge with Ember.jsRiding the Edge with Ember.js
Riding the Edge with Ember.js
 
Build a game with javascript (april 2017)
Build a game with javascript (april 2017)Build a game with javascript (april 2017)
Build a game with javascript (april 2017)
 
NinjaScript 2010-10-14
NinjaScript 2010-10-14NinjaScript 2010-10-14
NinjaScript 2010-10-14
 
Django at Scale
Django at ScaleDjango at Scale
Django at Scale
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
 
SPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have knownSPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have known
 
Google App Engine - exploiting limitations
Google App Engine - exploiting limitationsGoogle App Engine - exploiting limitations
Google App Engine - exploiting limitations
 
Hibernate Tutorial for beginners
Hibernate Tutorial for beginnersHibernate Tutorial for beginners
Hibernate Tutorial for beginners
 
Data Migrations in the App Engine Datastore
Data Migrations in the App Engine DatastoreData Migrations in the App Engine Datastore
Data Migrations in the App Engine Datastore
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
fuser interface-development-using-jquery
fuser interface-development-using-jqueryfuser interface-development-using-jquery
fuser interface-development-using-jquery
 
Django Pro ORM
Django Pro ORMDjango Pro ORM
Django Pro ORM
 
JavaScript!
JavaScript!JavaScript!
JavaScript!
 

Recently uploaded

Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
bellared2
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
maigasapphire
 
Acumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptxAcumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptx
BrainSell Technologies
 
Mastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for SuccessMastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for Success
David Wilson
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
alexjohnson7307
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
DianaGray10
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
ldtexsolbl
 
Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10
ankush9927
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
alexjohnson7307
 
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
Priyanka Aash
 
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
Priyanka Aash
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
bhumivarma35300
 
Step-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From ScratchStep-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From Scratch
softsuave
 
Uncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in LibrariesUncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in Libraries
Brian Pichman
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
Priyanka Aash
 

Recently uploaded (20)

Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
Russian Girls Call Navi Mumbai 🎈🔥9920725232 🔥💋🎈 Provide Best And Top Girl Ser...
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
 
Acumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptxAcumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptx
 
Mastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for SuccessMastering OnlyFans Clone App Development: Key Strategies for Success
Mastering OnlyFans Clone App Development: Key Strategies for Success
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
 
Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
 
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
(CISOPlatform Summit & SACON 2024) Digital Personal Data Protection Act.pdf
 
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
 
Step-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From ScratchStep-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From Scratch
 
Uncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in LibrariesUncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in Libraries
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
 

A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related() and prefetch_related()

  • 1. A Related Matter: Optimizing your webapp by using django-debug-toolbar, select_related(), and prefetch_related() Christopher Adams DjangoCon 2014 https://github.com/adamsc64/a-­‐related-­‐matter
  • 2. Christopher Adams • Software Engineer at Venmo • Twitter/Github: @adamsc64 • I’m not Chris Adams (@acdha), who works at Library of Congress • Neither of us are “The Gentleman” Chris Adams (90’s-era Professional Wrestler)
  • 4. Django is great But Django is really a set of tools
  • 5. Tools are great But tools can be used in good or bad ways
  • 6. The Django ORM: A set of tools
  • 7. Manage your own expectations for tools • Many people approach a new tool with broad set of expectations as to what the think it will do for them. • This may have little correlation with what the project actually has implemented.
  • 8. As amazing as it would be if they did…
  • 10. The Django ORM: An abstraction layer
  • 11. Abstraction layers • Great because they take us away from the messy details • Risky because they take us away from the messy details
  • 12. Don’t forget You’re far from the ground
  • 16. Lazy: Does not evaluate until it needs to
  • 18. Each a new QuerySet, none hit the database • queryset = Model.objects.all() • queryset = queryset.filter(...) • queryset = queryset.values(...)
  • 19. Hits the database (QuerySet is “evaluated”): • queryset = list(queryset) • queryset = queryset[:] • for model_object in queryset: do_something(...)
  • 20. Our app: a blog
  • 21. Models class Blog(models.Model):! submitter = models.ForeignKey('auth.User')! ! class Post(models.Model):! blog = models.ForeignKey('blog.Blog', related_name="posts")! likers = models.ManyToManyField('auth.User')! ! class PostComment(models.Model):! submitter = models.ForeignKey('auth.User')! post = models.ForeignKey('blog.Post', related_name="comments")!
  • 22. List View def blog_list(request):! blogs = Blog.objects.all()! return render(request, "blog/blog_list.html", {! "blogs": blogs,! })!
  • 25. Detail View def blog_detail(request, blog_id):! blog = get_object_or_404(Blog, id=blog_id)! posts = Post.objects.filter(blog=blog)! return render(request, "blog/blog_detail.html", {! "blog": blog,! "posts": posts,! })!
  • 29. If you can’t measure it…
  • 30. …you’d never know if there are problems.
  • 32. First view: The blog list page
  • 38. select_related() • select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. • For this reason, select_related gets the related objects in the same database query. • However, to avoid the much larger result set that would result from joining across a ‘many’ relationship, select_related is limited to single-valued relationships - foreign key and one-to-one.
  • 39. List View def blog_list(request):! blogs = Blog.objects.all()! blogs = blogs.select_related("submitter")! return render(request, "blog/blog_list.html", {! "blogs": blogs,! })!
  • 43. Second view: The blog detail page
  • 51. prefetch_related() • prefetch_related does a separate lookup for each relationship, and does the ’joining’ in Python. • This allows it to prefetch many-to-many and many-to-one objects … in addition to the foreign key and one-to-one relationships. • It also supports prefetching of GenericRelation and GenericForeignKey.
  • 52. Detail View def blog_detail(request, blog_id):! blog = get_object_or_404(Blog, id=blog_id)! posts = Post.objects.filter(blog=blog)! posts = posts.prefetch_related(! “comments__submitter", "likers",! )! return render(request, "blog/blog_detail.html", {! "blog": blog,! "posts": posts,! })!
  • 54. Summary • The QuerySet API methods select_related() and prefetch_related() automate some best practices to avoid extra queries in views/templates. • Use select_related() for one-to-many or one-to-one relations. • Use prefetch_related() for many-to-many or many-to-one (e.g. reverse foreign key) relations.
  • 55. Thanks! Christopher Adams (@adamsc64) https://github.com/adamsc64/a-related-matter