Flask Academy · 课时

连接与延迟加载和立即加载

遍历关系时避免 N+1 查询

第 4 / 4 课13 个步骤

连接与延迟加载和立即加载 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.posts

The 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! 🏁

免费开始

用 AI 导师学习 Python — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「连接与延迟加载和立即加载」课时是免费的吗?

是的 — 「连接与延迟加载和立即加载」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「连接与延迟加载和立即加载」这节课中我会学到什么?

遍历关系时避免 N+1 查询 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「连接与延迟加载和立即加载」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 query 和 get 获取行
  2. 筛选、排序并限制结果
  3. 定义关系与外键
  4. 连接与延迟加载和立即加载
← 返回 Flask Academy