本文へ移動
cccskills
無料GitHub で公開

optimize-ef-core-queries

Load when EF Core reads are slow, emit N+1 or excessive SQL, over-fetch, track too many entities, cause high DB load, or need projection/split-query/compiled-query analysis; not for general migrations, DbContext setup, or repository conventions.

インストール方法を見る

含まれるファイル(1)

  • SKILL.md3.7 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

<!-- ABOUTME: Measurement-first workflow for improving EF Core query shape and cost. --> <!-- ABOUTME: Preserves repository, tenant, authorization, and tracking boundaries. -->

Optimize EF Core Queries

Rules

  • Measure query count, generated SQL, duration, rows, and allocations before and after the change.
  • Preserve tenant filters, authorization predicates, ordering, pagination, and result semantics.
  • Repositories return entities; handlers do not receive IQueryable or access DbContext.
  • Prefer server-side projection for summaries; use Include only when the entity graph is actually needed.
  • Use AsNoTracking() for read-only entity loads and identity resolution only when duplicate entity instances matter.
  • Choose split queries for large/multiple collections only after considering extra round trips and consistency.
  • Use compiled queries only for measured hot paths where compilation overhead is material.
  • Never enable sensitive-data logging outside a safe local development environment.
  • Raw SQL is the last rung and must remain parameterized.

Workflow

  1. Locate every caller and reproduce the slow path with representative cardinality.
  2. Capture EF command logs or diagnostics and identify N+1, over-fetch, tracking, cartesian expansion, premature materialization, or repeated compilation.
  3. Apply the smallest query-shape fix:
    • move filters before materialization;
    • replace existence Count with Any;
    • project only required values;
    • remove lazy loading from loops;
    • add AsNoTracking to read-only entity loads;
    • use AsSplitQuery for proven collection-join explosion.
  4. Inspect the resulting SQL and query count.
  5. Run the caller's integration test with tenant isolation and realistic data volume.

PostgreSQL And Provider Checks

PostgreSQL is the production reference provider. Configure diagnostics through Npgsql and log completed database commands without parameter values:

options.UseNpgsql(connectionString)
    .LogTo(writeLog, [DbLoggerCategory.Database.Command.Name], LogLevel.Information);
  • Use ToQueryString() to inspect translation, then verify actual duration and row counts from command diagnostics.
  • Use PostgreSQL EXPLAIN (ANALYZE, BUFFERS) with representative parameters for measured hot paths; a sequential scan is not automatically wrong for small or low-selectivity sets.
  • Check index use, sort/spill behavior, join cardinality, and round trips rather than applying SQL Server plan assumptions.
  • For every other configured provider, run its own behavioral test because translation, collation, null semantics, and index choices can differ. Optimize the shared LINQ shape first and isolate provider-specific SQL only when measurement proves it necessary.

Checks

  • No client-side evaluation or N+1 command pattern appears in logs.
  • Query count and selected columns match the intended shape.
  • Read-only optimization did not break a later write path.
  • Tenant isolation and authorization tests still pass.
  • The measured bottleneck improved; otherwise revert the speculative optimization.

Verification

  • dotnet build --configuration Release --verbosity quiet
  • dotnet test --project tests/Event.Persistence.IntegrationTests/Event.Persistence.IntegrationTests.csproj --configuration Release --verbosity quiet
  • Also use EF Core project conventions when repository or DbContext behavior changes.

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Load for Blazor/MudBlazor UI changes involving forms, dialogs, focus, keyboard navigation, landmarks, ARIA, color contrast, RTL-safe styling, or WCAG 2.2 AA tests; not for backend-only changes.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

Load when a task asks to research, verify, compare, or look up framework/package behavior, release notes, standards, RFCs, CVEs, or unfamiliar APIs; use repository evidence first, then official docs, and not for codebase navigation alone.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

aspire

無料

Load for Aspire AppHost work: start/stop/wait resources, inspect dashboard/logs/traces, add integrations/resources, rebuild a service, run isolated worktrees, or diagnose Aspire orchestration; not for plain dotnet apps, container-only deployments, or cloud deployment.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

Load for authentication or authorization changes involving BFF cookies/tokens, JWT validation, claims/user ID extraction, policies, handler access checks, impersonation, or 401/403 bugs; not for UI affordance gating alone.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

Load for Blazor BFF server work involving YARP proxy routes, cookie sessions, access-token forwarding/refresh, downstream API calls, BFF handlers, or browser-to-API auth failures; not for client component rendering or API JWT validation alone.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

Load when editing Blazor `.razor.css`, scoped selectors, `::deep`, BEM class names, RTL/logical CSS properties, or styling nested MudBlazor components; not for global design tokens or non-Blazor CSS.

日本語の概要は準備中です。原文の説明を表示しています。

islamu-ngo/Event82026年10月12日 更新

islamu-ngo のスキルをすべて見る

このスキルの問題を報告する