عمليات الربط والتحميل الكسول مقابل المتعجل
تجنّب استعلامات N+1 عند اجتياز العلاقات.
عمليات الربط والتحميل الكسول مقابل المتعجل درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Querying Across Tables
Sometimes you filter parents by their children's data. A join lets one query span two related tables at once. 🔀
Write a join
Call join() on a query to pull a related model in, then filter on either side as one combined query.
q = User.query.join(Post).filter(Post.title == "Hi")Filter on the Joined Table
After a join, conditions on the other model are fair game, so you find parents by what their children contain.
active = User.query.join(Post).filter(Post.public).all()Lazy Loading Is Default
By default relationships are lazy: the children load only the moment you actually touch that attribute.
user = User.query.first()
user.postsThe N+1 Problem
Looping over parents and reading children lazily fires one query per parent. This N+1 pattern quietly kills performance. 😬
for u in User.query.all():
print(len(u.posts))Eager Loading Fixes It
Eager loading grabs the related rows up front, so the whole graph loads in just a query or two.
joinedload in One Query
Use joinedload to fetch parents and children together with a single SQL join.
from sqlalchemy.orm import joinedload
User.query.options(joinedload(User.posts)).all()selectinload for Collections
selectinload loads children in a second batched query, which often scales better for one-to-many lists.
from sqlalchemy.orm import selectinload
User.query.options(selectinload(User.posts)).all()Set Strategy on the Model
You can also pick a default with the relationship's lazy option, like select, joined, or selectin.
posts = db.relationship("Post", lazy="selectin")Join Does Not Eager Load
A plain join filters across tables but does not preload the relationship. For that you still add an eager option.
Choose Loading on Purpose
Lazy is fine for one row; reach for eager loading whenever you loop over many parents and read their children.
Quick Check
You loop over 100 users and read each user's posts. What problem can lazy loading cause?
Recap: Joins and Loading
You can now join tables, spot N+1, and fix it with joinedload or selectinload. Efficient relational reads, done! 🏁
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «عمليات الربط والتحميل الكسول مقابل المتعجل» مجاني؟
نعم — نص درس «عمليات الربط والتحميل الكسول مقابل المتعجل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «عمليات الربط والتحميل الكسول مقابل المتعجل»؟
تجنّب استعلامات N+1 عند اجتياز العلاقات. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «عمليات الربط والتحميل الكسول مقابل المتعجل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- جلب الصفوف باستخدام query وget
- تصفية النتائج وترتيبها وتحديد عددها
- تعريف العلاقات والمفاتيح الخارجية
- عمليات الربط والتحميل الكسول مقابل المتعجل