optimizing-ef-core-queries

Optimize slow EF Core queries by eliminating N+1 patterns and reducing generated SQL.

3|Updated Mar 18, 2024
One-click install
npx skills add https://github.com/deveel/deveel.events --skill optimizing-ef-core-queries-deveel
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-ef-core-queries
Source: https://github.com/deveel/deveel.events/tree/main/.agents/skills/optimizing-ef-core-queries
Command: npx skills add https://github.com/deveel/deveel.events --skill optimizing-ef-core-queries-deveel

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Optimize Entity Framework Core queries by fixing N+1 problems, choosing correct tracking modes, using compiled queries, and avoiding common performance traps. Use when EF Core queries are slow, generating excessive SQL, or causing high database load.

Core Features & Use Cases

  • Fix N+1 query patterns by using eager loading, splitting queries, or projection.
  • Optimize tracking behavior with NoTracking and identity resolution.
  • Use compiled queries and raw SQL for hot paths, and avoid common query traps.

Quick Start

Profile EF Core queries in your project and apply the recommended optimizations to eliminate N+1 queries.

Frequently Asked Questions about optimizing-ef-core-queries

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

FAQPage Schema
How do I fix N+1 query problems in EF Core?

Fix N+1 query problems in EF Core by applying eager loading, splitting queries, or using projection to fetch only necessary data, thereby eliminating excessive database round trips and reducing generated SQL.

When should I use no-tracking in Entity Framework Core?

Use no-tracking in Entity Framework Core for read-only queries to optimize tracking behavior, reduce memory and CPU pressure, and improve overall query performance when identity resolution is not required.

How do I optimize EF Core queries for high database load?

Optimize EF Core queries for high database load by profiling slow queries, adopting no-tracking options, using compiled queries for hot paths, and applying safe query practices to reduce memory and CPU pressure.

What is the best way to use compiled queries in EF Core hot paths?

The best way to use compiled queries in EF Core hot paths is to pre-compile your LINQ queries, allowing EF Core to bypass query translation overhead and execute raw SQL directly for frequently accessed data retrieval logic.

Are there limitations to optimizing EF Core queries with projection?

Limitations of optimizing EF Core queries with projection include losing entity tracking capabilities and potentially complicating data access layers when mapping partial results back to domain models, requiring careful profiling to ensure memory and CPU reduction.

Can I use raw SQL to improve EF Core performance?

Yes, you can use raw SQL to improve EF Core performance by bypassing the ORM query translation for hot paths, directly executing SQL commands to reduce CPU pressure and memory usage when generated SQL is excessive.