Agent skill
performance-optimization
Performance optimization - query analysis, N+1 detection, caching, indexing strategies
Install this agent skill to your Project
npx add-skill https://github.com/ils15/mythic-agents/tree/main/skills/performance-optimization
SKILL.md
Performance Optimization Skill
Query Optimization
N+1 Query Problem
# ❌ WRONG: N+1 queries
users = await db.query(User).all()
for user in users:
posts = await db.query(Post).where(Post.user_id == user.id).all()
# This runs 1 + N queries!
# ✅ RIGHT: Eager loading
users = await db.query(User).options(selectinload(User.posts)).all()
Indexes Strategy
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True) # Indexed automatically
email = Column(String, unique=True, index=True) # Index for WHERE
created_at = Column(DateTime, index=True) # Index for ORDER BY
# Composite index for common queries
__table_args__ = (
Index('ix_user_created_email', 'created_at', 'email'),
)
Query Optimization
# EXPLAIN to see execution plan
@router.get("/users/search")
async def search_users(q: str):
# Good query
return await db.execute(
select(User).where(User.email.ilike(f"%{q}%"))
)
Caching Strategy
class CachingService:
def __init__(self, redis_client):
self.redis = redis_client
async def get_user(self, user_id: int):
# Try cache first
cached = await self.redis.get(f"user:{user_id}")
if cached:
return json.loads(cached)
# Cache miss - fetch from DB
user = await db.query(User).filter(User.id == user_id).first()
# Cache for 1 hour
await self.redis.setex(
f"user:{user_id}",
3600,
json.dumps(user.dict())
)
return user
# Cache invalidation on update
async def update_user(user_id: int, data):
user = await db.query(User).filter(User.id == user_id).first()
user.update(data)
await db.commit()
# Invalidate cache
await self.redis.delete(f"user:{user_id}")
Monitoring Performance
# Add timing middleware
@app.middleware("http")
async def add_timing_header(request, call_next):
start = time.time()
response = await call_next(request)
duration = time.time() - start
response.headers["X-Process-Time"] = str(duration)
logger.info(f"{request.url.path} took {duration:.3f}s")
return response
Real-World Checklist
- Identified slow queries (with EXPLAIN ANALYZE)
- Indexes added on WHERE/JOIN/ORDER BY columns
- N+1 queries fixed (eager loading)
- Caching implemented for hot data
- Pagination added to list endpoints
- Batch operations where possible
- Connection pooling configured
- Monitoring/alerting in place
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
database-optimization
Optimize SQL queries, analyze indexes, review Alembic migrations, and identify N+1 problems. Provides query execution plans, index recommendations, and migration best practices for SQLAlchemy async.
orchestration-workflow
Practical step-by-step walkthrough for orchestrating features end-to-end using the multi-agent system, from planning through deployment
fastapi-async-patterns
Create async FastAPI endpoints with proper error handling, dependency injection, service/repository patterns, and type safety. Includes async database queries, pagination, filtering, and Gemini integration.
frontend-analyzer
Analyze React/Next.js components to extract typography, colors, layout, fonts, spacing systems, and design tokens. Identifies accessibility issues, responsive breakpoints, and component hierarchies.
remote-sensing-analysis
internet-search
Web research and internet search — source trust hierarchy, structured API patterns for general web (DuckDuckGo, Wikipedia, Jina Reader), tech community (Stack Overflow, Hacker News, Reddit, Dev.to), official vendor docs, academic databases (Semantic Scholar, CrossRef, arXiv), GitHub search, package registries, and remote sensing data sources. All sources are free and require no API key. Covers query construction, site-scoped search, parallel search strategy, and result synthesis.
Didn't find tool you were looking for?