اكتشاف مشكلة N+1
التعرف على مشكلة الاستعلام لكل صف الشائعة
اكتشاف مشكلة N+1 درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What N+1 Means
The N+1 problem is when your code runs one query to get a list, then one more query for each item in it. 🐢
Where It Hides
It usually hides in a loop over objects, where each pass quietly touches a related row in the database again and again.
A Tiny Example
Imagine listing books and printing each author. The loop below looks innocent but fires a fresh query on every line.
for book in Book.objects.all():
print(book.author.name)Counting the Queries
One query loads all books. Then each book.author access runs its own query, so 100 books means 101 trips to the database.
Why 1, Then N
That is the name: 1 query for the parent list, plus N extra queries, one per related object you touch.
Lazy Loading Is the Cause
Django loads related rows lazily, only when you ask. Convenient for one object, painful when you ask inside a loop.
It Scales Badly
The damage grows with your data. Ten rows feel fine, but ten thousand rows turn one page load into a flood of queries.
Templates Trigger It Too
A template that prints related data inside a for loop causes the exact same storm, even though no Python loop is visible to you.
{% for book in books %}
{{ book.author.name }}
{% endfor %}How to Spot It
Watch for a related-object access, like obj.parent, happening inside a loop. That pattern is the warning sign every time.
Make Queries Visible
You can count queries directly with connection.queries during DEBUG, which proves how many round trips a view really makes.
from django.db import connection
print(len(connection.queries))The Good News
Once you can see the problem, Django gives you two tools to collapse all those queries into just a few. You will meet them next. 🚀
Quick Check
Let us make sure the N+1 pattern is clear.
Recap
The N+1 problem is one list query plus one per related object, caused by lazy loading inside loops. Spot it by watching for related access in a loop. ✅
الأسئلة الشائعة
هل درس «اكتشاف مشكلة N+1» مجاني؟
نعم — نص درس «اكتشاف مشكلة N+1» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «اكتشاف مشكلة N+1»؟
التعرف على مشكلة الاستعلام لكل صف الشائعة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «اكتشاف مشكلة N+1»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اكتشاف مشكلة N+1
- select_related للمفاتيح الأجنبية
- prefetch_related لعلاقات M2M
- تحليل الأداء باستخدام Debug Toolbar