brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
orm-patterns-n-plus-1-awarenessProvides comprehensive training on ORM patterns, the N+1 problem, and strategies to mitigate it, complete with code examples and workflows.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: orm-patterns-n-plus-1-awareness description: Provides comprehensive training on ORM patterns, the N+1 problem, and strategies to mitigate it, complete with code examples and workflows. license: MIT compatibility: opencode metadata: version: "1.0.0" domain: data-and-persistence triggers: orm patterns, n+1 problem, optimization strategies, data access patterns, object relational mapping, lazy loading, eager loading role: implementation scope: implementation output-format: code related-skills: orm-best-practices, data-access-optimization archetypes: - tactical anti_triggers: - vague ideation - generalization response_profile: verbosity: low directive_strength: high abstraction_level: operational
Provides comprehensive training on ORM patterns, the N+1 problem, and strategies to mitigate it, complete with code examples and workflows.
from sqlalchemy.orm import joinedload
session.query(Parent).options(joinedload(Parent.children))
Checkpoint: Verify all necessary child entities are fetched in the initial query to avoid N+1 pitfalls.The Active Record pattern directly maps an object to a database table, making CRUD operations straightforward.
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
def save(self):
session.add(self) # Automatically inserts/updates the user in the database
session.commit()
Here’s where lazy loading can introduce performance issues. For example:
# Potential N+1 Problem—fetching orders for each user individually.
for user in session.query(User).all():
print(user.orders) # Results in a separate query for each user's orders
Adjust the query to load related entities in one go:
# Proper use of eager loading to prevent N+1
users = session.query(User).options(joinedload(User.orders)).all()
for user in users:
print(user.orders) # All orders fetched in single query
# Lazy Loading: Not fetching data until accessed. May result in N+1 problem.
orders = user.orders # Triggers a query for each access
# Eager Loading: Fetching all related data in one go.
user_with_orders = session.query(User).
options(joinedload(User.orders)).all()
# All orders are fetched in a single query
When loaded, this skill helps identify ORM patterns and mitigate common pitfalls effectively, providing crucial background for impactful data-driven application development.
Authoritative documentation links for this skill's domain. The model follows markdown links to resolve external references and inline content.