optimize-ef-core-queries

Diagnoses and rewrites slow EF Core queries to eliminate N+1 patterns and over-fetching.

7|3|Updated Sep 23, 2025
One-click install
npx skills add https://github.com/islamu-ngo/Event --skill optimize-ef-core-queries-islamu-ngo
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: optimize-ef-core-queries
Source: https://github.com/islamu-ngo/Event/tree/main/.agents/skills/optimize-ef-core-queries
Command: npx skills add https://github.com/islamu-ngo/Event --skill optimize-ef-core-queries-islamu-ngo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? EF Core queries often suffer from N+1 command patterns, over-fetching, unnecessary change tracking, and cartesian explosion, causing high database load and slow response times. This Skill provides a measurement-first workflow to identify and fix these query-shape problems without breaking tenant isolation, authorization, or repository boundaries. ## Core Features & Use Cases - Query Diagnostics: Capture EF command logs, generated SQL, durations, and row counts to pinpoint N+1, over-fetch, tracking, and premature materialization issues. - Query-Shape Fixes: Apply targeted rewrites such as moving filters before materialization, replacing Count with Any, projecting only required values, adding AsNoTracking, and using AsSplitQuery for proven collection-join explosion. - PostgreSQL Verification: Inspect translations with ToQueryString, validate hot paths with EXPLAIN (ANALYZE, BUFFERS), and confirm index use and round trips on the production provider. - Use Case: A repository method loads events with their sessions and tags, generating dozens of SQL commands per request. Use this Skill to capture the command logs, apply projection and split queries, and verify the query count drops while tenant isolation tests still pass. ## Quick Start Analyze the slow event list endpoint for N+1 queries and rewrite the repository query to use projection and AsNoTracking, then verify with the integration tests.

Frequently Asked Questions about optimize-ef-core-queries

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 queries in EF Core?▼

Capture EF command logs to confirm the N+1 pattern, then remove lazy loading from loops and load related data with Include or a single projected query. Verify the fix by checking that the query count and selected columns match the intended shape.

When should I use AsNoTracking in EF Core?▼

Use AsNoTracking for read-only entity loads where the results will not be modified and saved. Confirm the read-only optimization does not break a later write path that expects tracked entities.

When should I use AsSplitQuery vs Include in EF Core?▼

Use Include when the entity graph is genuinely needed, and choose AsSplitQuery only for large or multiple collection joins after measuring cartesian explosion. Weigh the extra round trips and consistency trade-offs before splitting.

How do I inspect generated SQL in EF Core with PostgreSQL?▼

Use ToQueryString() to inspect the LINQ translation, then configure Npgsql logging of DbLoggerCategory.Database.Command to see actual durations and row counts. For hot paths, run EXPLAIN (ANALYZE, BUFFERS) with representative parameters.

When should I use compiled queries in EF Core?▼

Use compiled queries only for measured hot paths where query compilation overhead is material to performance. Do not apply them speculatively; revert if the measured bottleneck does not improve.

Why is my EF Core query slow after adding Includes?▼

Multiple collection Includes cause cartesian explosion, multiplying result rows and allocations. Measure the row counts first, then consider projection of only required values or AsSplitQuery for the proven problematic collections.