Optimize queries by selecting only required fields and avoiding N+1 problems. Use when writing queries with relations or large result sets.
Optimizes Prisma queries by selecting only required fields and preventing N+1 problems through proper relation loading. Use when writing queries with relations, building API endpoints, or optimizing slow queries with large result sets.
/plugin marketplace add djankies/claude-configs/plugin install prisma-6@claude-configsThis skill is limited to using the following tools:
references/api-optimization.mdreferences/n-plus-one-prevention.mdreferences/nested-selection.mdreferences/performance-verification.mdreferences/type-safety.mdOptimize Prisma 6 queries through selective field loading and relation batching to prevent N+1 problems and reduce data transfer.
include (prototyping, most fields) vs. select (production, API responses, performance-critical)select for precise control, nest relations with select, use _count instead of loading all records, limit relation results with takewhere clauses, orderBy fields, composite indexes for filtered relationsProblem: Fetching entire models wastes bandwidth and memory
const users = await prisma.user.findMany()
Solution: Use select to fetch only needed fields
const users = await prisma.user.findMany({
select: {
id: true,
email: true,
name: true,
},
})
Performance Impact:
Include: Adds relations to full model
const user = await prisma.user.findUnique({
where: { id: 1 },
include: {
posts: true,
profile: true,
},
})
Select: Precise control over all fields
const user = await prisma.user.findUnique({
where: { id: 1 },
select: {
id: true,
email: true,
posts: {
select: {
id: true,
title: true,
published: true,
},
},
profile: {
select: {
bio: true,
avatar: true,
},
},
},
})
When to Use:
include: Quick prototyping, need most fieldsselect: Production code, API responses, performance-critical pathsN+1 Problem: Separate query for each relation
const posts = await prisma.post.findMany()
for (const post of posts) {
const author = await prisma.user.findUnique({
where: { id: post.authorId },
})
}
Solution: Use include or select with relations
const posts = await prisma.post.findMany({
include: {
author: true,
},
})
Better: Select only needed author fields
const posts = await prisma.post.findMany({
select: {
id: true,
title: true,
content: true,
author: {
select: {
id: true,
name: true,
email: true,
},
},
},
})
Problem: Loading all relations just to count them
const user = await prisma.user.findUnique({
where: { id: 1 },
include: {
posts: true,
},
})
const postCount = user.posts.length
Solution: Use _count for efficient aggregation
const user = await prisma.user.findUnique({
where: { id: 1 },
select: {
id: true,
name: true,
_count: {
select: {
posts: true,
comments: true,
},
},
},
})
Result:
{
id: 1,
name: "Alice",
_count: {
posts: 42,
comments: 128
}
}
</core-principles>
<quick-reference>
## Quick Reference
const optimized = await prisma.model.findMany({
where: {},
select: {
field1: true,
field2: true,
relation: {
select: {
field: true,
},
take: 10,
},
_count: {
select: {
relation: true,
},
},
},
orderBy: { field: 'desc' },
take: 20,
skip: 0,
})
select for all production queriesinclude only for prototyping_count for counting relationsMUST:
select for all API responses_count for relation countsSHOULD:
takeNEVER:
include in production without field selectionFor detailed patterns and examples, see:
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.