JOINと遅延読み込み・即時読み込み
リレーションをたどる際のN+1クエリを避けます
「JOINと遅延読み込み・即時読み込み」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.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! 🏁
よくある質問
「JOINと遅延読み込み・即時読み込み」レッスンは無料ですか?
はい。「JOINと遅延読み込み・即時読み込み」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「JOINと遅延読み込み・即時読み込み」で何を学びますか?
リレーションをたどる際のN+1クエリを避けます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「JOINと遅延読み込み・即時読み込み」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- queryとgetで行を取得する
- 結果をフィルター、並べ替え、制限する
- リレーションシップと外部キーを定義する
- JOINと遅延読み込み・即時読み込み