跳到正文
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.

中文简介正在准备中。当前显示原文说明。

集成工具PostgreSQL
查看安装方法

包含的文件(1)

  • SKILL.md3.7 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

<!-- 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.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

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月20日 更新

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月20日 更新

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月20日 更新

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月20日 更新

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月20日 更新

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月20日 更新

查看 islamu-ngo 的全部技能

报告此技能的问题