Optimize Queries with only() and defer() When working with Django ORM, it's important to be mindful of the data you retrieve from the database. Fetching all fields when only a few are required can lead to unnecessary overhead and slower performance. ❌ User.objects.all() → Retrieves all fields ✅ User.objects.only('id', 'name') → Retrieves only the specified fields By limiting the data you load, you can significantly improve query efficiency and overall application performance. Key takeaway: Less data fetched = Faster queries ⚡ #Django #Python #WebDevelopment #PerformanceOptimization
Django ORM Optimization: Use only() and defer()
More Relevant Posts
-
I was debugging a Django service last week and hit a classic problem memory growing silently across requests, no obvious culprit. The usual suspects (tracemalloc, memory_profiler, objgraph) are great tools. But I wanted something I could drop on any function in 30 seconds and get a readable answer from. Also, honestly I wanted to understand what's happening at the GC and tracemalloc abstraction layer in Python. The best way I know to understand something is to build on top of it. So I built MemGuard over a weekend. What it does: Drop @memguard() on any function and after every call you get: Net memory retained (the actual leak signal) Peak vs net ratio — catches memory churn even when net looks clean Per-type gc object count delta tells you what is accumulating, not just how much Cross-call trend detection if net grows every call, it flags it Allocation hotspots via tracemalloc exact file and line Zero dependencies. Pure stdlib gc, tracemalloc, threading. @memguard() def process_batch(records): That's it. It also works as a context manager if you want to profile a block rather than a function. Biggest thing I learned building this: Python's gc and tracemalloc expose far more than most people use day to day. The object-reference graph alone tells a story that byte counts miss entirely. Repo: https://lnkd.in/gdjkHvfb Would love feedback from anyone who's dealt with Python memory issues in production. #Python #Django #SoftwareEngineering #OpenSource #BackendDevelopment #MemoryManagement
To view or add a comment, sign in
-
-
Day 5 of #50DaysOfDjango ◆URLs & Routing in Django Today I learned how Django connects URLs to views This is the starting point of every user request. Response Flow: URL urls.py → View urls.py acts like a traffic controller Static URL: /about/ Dynamic URL: /user/5/ (passes data) Example: Python path('user/<int:id>/', views.user_profile) Challenge: Create 3 URLs + 1 dynamic route and connect them to views Now I understand how Django handles requests behind the scenes
To view or add a comment, sign in
-
-
💡 𝗧𝗶𝗽 𝗼𝗳 𝘁𝗵𝗲 𝗗𝗮𝘆 — 𝗗𝗷𝗮𝗻𝗴𝗼 𝗗𝗶𝗱 𝘆𝗼𝘂 𝗸𝗻𝗼𝘄? Django’s "annotate()" lets you add 𝗰𝗮𝗹𝗰𝘂𝗹𝗮𝘁𝗲𝗱 𝗳𝗶𝗲𝗹𝗱𝘀 𝗱𝗶𝗿𝗲𝗰𝘁𝗹𝘆 𝗶𝗻 𝘆𝗼𝘂𝗿 𝗾𝘂𝗲𝗿𝘆𝘀𝗲𝘁𝘀. Instead of processing data in Python after fetching it, you can compute values at the 𝗱𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝗹𝗲𝘃𝗲𝗹. 🔧 𝗖𝗼𝗺𝗺𝗼𝗻 𝘂𝘀𝗲 𝗰𝗮𝘀𝗲𝘀: - Counting related objects ("Count") - Calculating averages ("Avg") - Adding computed fields to API responses This reduces data processing in your app and leverages the power of your database. Smarter queries = faster apps. #Django #Python #BackendDevelopment #WebDevelopment #DatabaseOptimization #PerformanceOptimization #SoftwareEngineering #CodingTips #FullstackDeveloper
To view or add a comment, sign in
-
-
Most Python developers use Flask, FastAPI, or Django… But many still overlook one fundamental concept: HTTP methods. No matter which framework you choose, everything comes down to how your application handles these requests: • GET – Retrieve data • POST – Create a resource • PUT – Replace an entire resource • PATCH – Update specific fields • DELETE – Remove a resource Here’s where it gets interesting 👇 A lot of developers confuse PUT and PATCH. PUT → Replaces the entire resource PATCH → Updates only what’s necessary Why does this matter? Because choosing the right method leads to: ✔ Cleaner API design ✔ Better performance ✔ Easier maintainability Frameworks may differ in style and complexity, but the foundation remains the same: HTTP. Master these basics once, and switching between Flask, FastAPI, and Django becomes much easier. What’s one concept in backend development that took you time to fully understand? #Python #WebDevelopment #APIDesign #BackendDevelopment #Flask #FastAPI #Django #HTTPMethods
To view or add a comment, sign in
-
-
I am happy to share 🥳🥳🥳 🚀 Just shipped my first open-source Python library: pyctxlog Ever tried tracing a single request across 40 log lines and given up? That's the problem I built this for. pyctxlog is a tiny decorator that auto-tags every log line inside a function with per-call context — request id, job name, tenant, whatever you want — using contextvars so it works correctly across threads and async tasks. @log_context(fields={"job": "ingest_orders"}) def run_ingest(batch_id): log.info(f"processing {batch_id}") # every line inside is auto-tagged with job + id ✅ Sync + async auto-detected ✅ Works with Django, FastAPI, Celery, or plain functions ✅ Zero framework assumptions — truly generic ✅ Python 3.9+, MIT licensed pip install pyctxlog 🔗 https://lnkd.in/dx9HpvXt 🐙 https://lnkd.in/df4rAkR4 Feedback very welcome — this is v0.1.0 and I'd love to hear what you'd want in v0.2. #Python #OpenSource #Logging #Observability #SoftwareEngineering
To view or add a comment, sign in
-
-
One extra line in your Django queryset can turn a 20ms API into a 2s one. ⚡ I ran into this while optimizing an API that was fetching related data inside a loop. Everything worked. But response time kept increasing as data grew. The issue was the classic N+1 query problem. Django gives two powerful tools to solve it. 🔹 select_related Uses SQL JOIN Best for ForeignKey or OneToOne Fetches everything in a single query 🔹 prefetch_related Runs separate queries and combines results in Python Best for ManyToMany or reverse relationships Prevents large JOIN explosions The key difference is not syntax. It is how the data is fetched. Golden rule 👇 Single relationship → select_related Multiple relationship → prefetch_related Getting this wrong does not just slow things down. It can quietly break performance as your data grows. In my experience, fixing N+1 queries is one of the fastest ways to improve API performance in Django. Which one do you end up using more in your projects? #Django #Python #PostgreSQL #BackendDevelopment #DatabaseOptimization #SoftwareEngineering
To view or add a comment, sign in
-
-
Been building CallFlow Tracer, an open-source Python library that traces function call flows and visualizes them as interactive graphs. Just shipped v0.4.1 with some major improvements: Fixed critical security vulnerabilities (command injection, code injection in the old extension) Added Content Security Policy headers to all webviews What CallFlow Tracer does in this new version: OpenTelemetry export for production observability (Jaeger, OTLP) SLA/SLO tracking with error budgets and canary analysis Framework integrations: FastAPI, Flask, Django, SQLAlchemy Fixed imports and made it more modular and extensible. Would love feedback from anyone working on observability, profiling, or developer tooling. below is the link https://lnkd.in/drUQspvv #Python #OpenSource #DeveloperTools #Observability #OpenTelemetry #VSCode #TypeScript #SoftwareEngineering
To view or add a comment, sign in
-
5 books. 6 database trips. That's your Django app bleeding performance. Most of the time we never notice the N+1 problem — until their app slows down under real data. Here's the fix explained as a story (swipe through) 👇 𝗦𝗹𝗶𝗱𝗲 𝟭 — You have 5 books. Each has an author. Simple. 𝗦𝗹𝗶𝗱𝗲 𝟮 — Without optimization: Django makes 6 separate DB trips. One per book. Painful. 𝗦𝗹𝗶𝗱𝗲 𝟯 — select_related() fixes it with a single JOIN. 1 trip. Everything together. 𝗦𝗹𝗶𝗱𝗲 𝟰 — But JOIN breaks with tags — Book 1 repeats 3 times. Messy. 𝗦𝗹𝗶𝗱𝗲 𝟱 — prefetch_related() makes 2 smart trips. Python glues them in memory. 𝗦𝗹𝗶𝗱𝗲 𝟲 — The rule: ONE thing → select_related. MANY things → prefetch_related. That's it. Two methods. One simple rule. #Django #Python #WebDevelopment #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
Django can absolutely scale. But not if you treat it like a tutorial project. Here are 6 patterns we use in production at Horizon Dev that handle real load: → Fat models, thin views → Select_related and prefetch_related everywhere → Custom managers for complex queries → Celery for anything over 500ms → Database routers for read replicas → Cached querysets with smart invalidation Swipe through for details on each ↓ #Django #Python #BackendEngineering
To view or add a comment, sign in
-
Just shipped stup 🚀 A python package that scaffolds production-ready Python projects in seconds. 🛠️ Every time I started a new project, I was doing the same thing: init uv, pin Python, create venv, wire up requirements.txt, set up folders. 10 minutes of setup before writing a single line of actual code. ⏳ stup kills that loop. One command, full scaffold⚡ → `stup django` : Django + DRF + Celery + Redis + PostgreSQL, ready to go → `stup lang-agent` : LangGraph agent with tools, memory, Ollama config → `stup react-fastapi` : Full-stack with Docker Compose and CORS pre-wired → `stup ml` : MLflow + PyTorch + scikit-learn experiment structure → `stup cli` : PyPI-ready package with Typer + Rich, entry_points set All templates build on top of `stup uv` which handles uv init, Python pinning, and venv in one shot. Install once: `pip install stup` source code: https://lnkd.in/duGUCXVH
To view or add a comment, sign in
-
Explore related topics
- How to Optimize Query Strategies
- How Indexing Improves Query Performance
- How to Optimize Pytorch Performance
- Tips for Database Performance Optimization
- How to Improve NOSQL Database Performance
- Efficient Database Queries
- How to Optimize Cloud Database Performance
- How to Optimize Postgresql Database Performance
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development