Performance: Database query optimization
This affects how fast data loads from the database, impacting page load speed and responsiveness.
from sqlalchemy.orm import joinedload users = User.query.options(joinedload(User.posts)).all()
users = User.query.all() for user in users: posts = Post.query.filter_by(user_id=user.id).all()
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| N+1 Query Pattern | Minimal | Minimal | High due to delayed data | [X] Bad |
| Eager Loading with Joined Query | Minimal | Minimal | Low due to fast data fetch | [OK] Good |
limit() is called on the query object before all().limit(10) before all(). Others misuse method order or methods.users = User.query.filter(User.age > 30).limit(5).all()
users = User.query.filter(User.name == 'Alice').all().limit(3)
limit() must be called before all() because all() executes the query and returns a list.limit() on a list (result of all()) causes an AttributeError.with_entities(User.username) fetches only usernames, reducing data load.all().Pass the test and this pattern moves unknown