SlideShare a Scribd company logo
SOLVING PERF ISSUES IN . ORMDJANGO
SIAN LERK LAU
linkedin.com/in/sianlerk
@kiawin
A little bit about Jewel…
We’re a Singapore-based company with offices in KL and HK too.
We’re hiring!
We’re always looking for talented developers, data scientists, data engineers, devops engineers and more!
Check out our Careers page at
https://www.jewelpaymentech.com/careers.html
Image
Jewel Paymentech
© Copyright 2018 Jewel Paymentech Pte .Ltd. – Reproduction and distribution of this presentation without written permission is
prohibited.
VISION
JOIN THE TEAM
Python Developers
React Developers
Data Scientists
Data Engineers
and many more…
Scan the QR code to
visit our Careers page:
(UN)ORTHODOX
THE JOURNEY OF AN OPTIMIZATION
Jafnee Jasmee Willy CahyadiSwee Meng
BACKGROUND
(OUR STORY)
M V P
M V P
TOOLS
(TO GET US STARTED)
browser inspection tool
Solving performance issues in Django ORM
code inspection tool
code profiling tool
jazzband/django-silk
Solving performance issues in Django ORM
Solving performance issues in Django ORM
Solving performance issues in Django ORM
Solving performance issues in Django ORM
ORM
(THE DJANGO WAY)
just works
just works
django documentation
D.10K
(THE CASE STUDY)
#the10kreality
REALITY
HURTS
(if not your eyes)
setting baseline
#the10kdream
process of elimination
lazy load
1
# 10k Records found
Form.objects.count()
# How many queries made?
for form in Form.objects.all():
print(form.author.name)
# 10k Records found
Form.objects.count()
# How many queries made?
for form in Form.objects.all():
print(form.author.name)
# 1+10k queries made!
Big-O Notation
# 10k Records found
Form.objects.count()
# How many queries made?
for form in Form.objects.all():
print(form.author.name)
# 1+10k queries made!
Big-O Notation
O(n+1)
one-to-one
one-to-many
many-to-many
one-to-one
one-to-many
.select_related()
# One query made
for form in Form.objects.select_related('author'):
print(form.author.name)
O(1)
many-to-many .prefetch_related()
# Two queries made, join done at python level
for form in Form.objects.prefetch_related('categories'):
for category in form.categories.all():
print(category.name)
O(2)
model object
2
#expensive
skip model object
slim data retrieval
# 10k Records found
Form.objects.count()
# List of 10k model objects
for form in Form.objects.select_related('author'):
print(book.author.name)
fat + slow
skip model object
slim data retrieval
.values()
# List of 10k dictionaries
form_qs = Form.objects.values(
'application_id',
'author__name',
'categories__name',
)
for form in form_qs:
print(
f"{form['application_id']}, "
f"{form['author__name']}, "
f"{form['categories__name']}"
)
slim + quick
skip model object
slim data retrieval
.values_list()
# List of 10k tuples
form_qs = Form.objects.values_list(
'application_id',
'author__name',
'categories__name',
)
for application_id, author, categories in form_qs:
print(f'{application_id}, {author}, {categories}')
slim + quick
skip model object
slim data retrieval
.values_list()
# List of 10k tuples
form_qs = Form.objects.values_list(
'application_id',
flat=True,
)
for application_id in form_qs:
print(f'{application_id}')
slim + quick
prove to me
from django.db import connection
# prints 0
print(len(connection.queries))
# How many queries made?
forms = list(Form.objects.values_list('id',
'categories__name'))
# prints x?
print(len(connection.queries))
print(connection.queries[-1])
# actual query performed, with time taken
{'sql': "QUERY = 'SELECT ...', 'time': '0.009'}
DRF
(serializer)
3
#slow
manual serialization
return a dict
#eww
three minutes saved
#okay...
database
agnostic
support
4
to-many relationship
string aggregation
SELECT
form.id AS form,
...
STRING_AGG(category.name, ',') AS categories,
...
GROUP BY form
...
string aggregation
SELECT
form.id AS form,
...
STRING_AGG(category.name, ',') AS categories,
...
GROUP BY form
...
string aggregation
string aggregation
#...
setdefault
get
form_qs = Form.objects.values('id', 'categories__name')
serialized_forms = {}
for id, category in form_qs:
serialized_form = serialized_forms.setdefault(
id,
{},
)
categories = serialized_form.get('categories', [])
categories.append(category)
setdefault x get
#jobdone
disclaimer
THANK YOU
linkedin.com/in/sianlerk
@kiawin

More Related Content

PDF
A gentle intro to the Django Framework
PDF
Fast REST APIs Development with MongoDB
PPTX
PyCon APAC - Django Test Driven Development
PDF
Building Services With gRPC, Docker and Go
PDF
Scalable web application architecture
PDF
Prompt engineering for iOS developers (How LLMs and GenAI work)
PPTX
Crafting Evolvable Api Responses
PDF
Data Mining Open Ap Is
A gentle intro to the Django Framework
Fast REST APIs Development with MongoDB
PyCon APAC - Django Test Driven Development
Building Services With gRPC, Docker and Go
Scalable web application architecture
Prompt engineering for iOS developers (How LLMs and GenAI work)
Crafting Evolvable Api Responses
Data Mining Open Ap Is

Similar to Solving performance issues in Django ORM (20)

PDF
Story for a Ruby on Rails Single Engineer
PPTX
How to Be a 10x Data Scientist
PPTX
moma-django overview --> Django + MongoDB: building a custom ORM layer
PDF
From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...
PPTX
How to Build a Semantic Search System
PDF
Clojure: Simple By Design
PDF
Introduction to Django
PDF
RESTful Web API and MongoDB go for a pic nic
PDF
Intro to PySpark: Python Data Analysis at scale in the Cloud
PDF
Agile Database Development with JSON
PDF
Scaling business app development with Play and Scala
PPT
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
PPTX
IWT presentation121232112122222225556+556.pptx
PPT
Re Descubriendo A Linq
ODP
Intro To Spring Python
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
PDF
Deliver Business Value Faster with AWS Step Functions
PDF
Mashing Up The Guardian
PPTX
[GEMINI EXTERNAL DECK] Introduction to Gemini.pptx
Story for a Ruby on Rails Single Engineer
How to Be a 10x Data Scientist
moma-django overview --> Django + MongoDB: building a custom ORM layer
From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...
How to Build a Semantic Search System
Clojure: Simple By Design
Introduction to Django
RESTful Web API and MongoDB go for a pic nic
Intro to PySpark: Python Data Analysis at scale in the Cloud
Agile Database Development with JSON
Scaling business app development with Play and Scala
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
IWT presentation121232112122222225556+556.pptx
Re Descubriendo A Linq
Intro To Spring Python
Crossing the Bridge: Connecting Rails and your Front-end Framework
Deliver Business Value Faster with AWS Step Functions
Mashing Up The Guardian
[GEMINI EXTERNAL DECK] Introduction to Gemini.pptx
Ad

More from Sian Lerk Lau (7)

PDF
The journey of an (un)orthodox optimization
PDF
Velocity. Agility. Python. (Pycon APAC 2017)
PDF
DevOps - Myth or Real
PPTX
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
PDF
Python and you
PDF
Quality of life through Unit Testing
PDF
Install Archlinux in 10 Steps (Sort of) :)
The journey of an (un)orthodox optimization
Velocity. Agility. Python. (Pycon APAC 2017)
DevOps - Myth or Real
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Python and you
Quality of life through Unit Testing
Install Archlinux in 10 Steps (Sort of) :)
Ad

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
Teaching material agriculture food technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
KodekX | Application Modernization Development
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Approach and Philosophy of On baking technology
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Teaching material agriculture food technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
KodekX | Application Modernization Development
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Solving performance issues in Django ORM