SQL 查詢最佳化

SQL 查詢最佳化

利用量測與 execution plan 找出慢 SQL 的瓶頸。

Community · 社群來源 · 資料與分析

來源狀態

可用來源

原始名稱: sql-optimization-patterns

原始作者: scops

這是第三方 Community Skill;2lus 收錄不代表已完整安全審計或保證安全。

資源類型

Skill

與類似 Skill 有什麼不同?

  • SQL Server Agent Skills 教學

    SQL Server Agent Skills 是 SSMS AI 技能官方文件指南;SQL Query Optimization 是以量測和計畫分析查詢的 Community Skill。

  • 資料庫設計

    Database Design 設計實體關係、約束及 schema 演進;SQL Query Optimization 針對已量測的查詢瓶頸調整存取方式。

來源描述的能力

這些是來源描述的可能操作,不代表 2lus 已授予權限或已測試。

尚未列出能力,請審查原始來源。

這個 Skill 是什麼?

提出有證據、風險可評估的查詢調整,而非看到慢就加索引。

可以做什麼?何時適合使用?

  • 分析慢查詢計畫
  • 評估 join 與 sort 成本
  • 比較 N+1 與批次查詢

如何使用

  1. Measure:取得基準與代表性參數
  2. Explain:解讀計畫並定位瓶頸
  3. Change → Re-measure:先提案,授權後才調整並重測

使用前你需要準備

  • 去識別化 SQL
  • Execution Plan(推薦)
  • Table Schema 與現有 Indexes

你可以替換:

[database]、[table]

環境與相依需求

  • 提供引擎版本、去識別 SQL、schema、統計與既有 plan;保留參考檔。

使用範例與 Prompt

以下是 2lus 撰寫的示範需求;請替換為你有權處理的檔案與專案,不代表已執行或保證結果。

入門

解讀附件 PostgreSQL plan 的估計列數與實際列數差異,不執行 SQL。

實務

分析這組列表請求的 N+1 查詢紀錄,比較 batch、join 與快取方案的正確性及成本。

進階

以 Measure → Explain → Identify Bottleneck → Change → Re-measure 設計慢報表優化計畫,比較排序 spill、索引及重寫;不自動套用任何 DDL。

實用提醒

  • 同時比較結果一致性、寫入成本與延遲,不只看單次耗時。

限制與注意事項

  • EXPLAIN ANALYZE 可能實際執行 SQL;快取與資料分布會影響量測。

安全注意事項

  • 禁止未授權連線或改索引;敏感參數遮蔽,先使用提供的計畫。

使用與設定

此條目不提供已確認的通用安裝指令;請依官方文件與 Agent 版本操作。

支援平台

未確認特定 Agent 相容性

來源與授權

來源查核日期(非安全認證): 2026-09-29

MIT

原始來源 ↗ 授權條款 ↗ 官方文件 ↗

相關 Skills

使用第三方 Skill 前,請先檢查來源、權限與執行內容。安裝指令只供查看與複製,不會由 2lus 執行。

2lus AI Skills Library 提供 Skill 的整理與使用導覽。第三方 Skill 的內容、授權與可用性以原始來源為準。使用或安裝前,請自行確認其權限與執行內容。

SQL Query Optimization

Use measurements and execution plans to locate slow SQL bottlenecks.

Community · Community source · Data & Analysis

Source status

Active source

Original name: sql-optimization-patterns

Original author: scops

This is a third-party community skill. Inclusion by 2lus is not a complete security audit or safety guarantee.

Resource type

Skill

How is this different from similar skills?

  • SQL Server Agent Skills

    SQL Server Agent Skills is official documentation for SSMS AI skills; SQL Query Optimization is a community skill for measurement and plan-based diagnosis.

  • Database Design

    Database Design models relationships, constraints and schema evolution; SQL Query Optimization targets measured query bottlenecks.

Documented capabilities

These are operations described upstream, not permissions granted or tested by 2lus.

Capabilities not declared here; review the original source.

What is this skill?

Propose evidence-based query changes rather than automatically adding indexes.

Use cases and when to use it

  • Interpret slow-query plans
  • Assess joins and sorts
  • Compare N+1 and batching

How to use it

  1. Measure representative baseline parameters
  2. Explain the plan and identify the bottleneck
  3. Propose a change; remeasure only after authorization

What you need

  • Redacted SQL
  • Execution plan (recommended)
  • Table schema and existing indexes

You can replace:

[database], [table]

Environment and dependencies

  • Supply engine version, redacted SQL, schema, statistics and plans; retain references.

Usage and prompt examples

These example requests were written by 2lus. Substitute files and projects you may use; examples are not executed results or guarantees.

Beginner

Interpret estimated versus actual rows in this PostgreSQL plan without executing SQL.

Practical

Analyze these N+1 request logs; compare batching, joins and caching for correctness and cost.

Advanced

Design a Measure → Explain → Identify Bottleneck → Change → Re-measure plan for a slow report, comparing spills, indexes and rewrites without applying DDL.

Tips

  • Compare result equivalence, write costs and latency, not one timing.

Limitations

  • EXPLAIN ANALYZE can execute SQL; caching and data distribution affect measurements.

Security notes

  • No unauthorized connections or index changes; redact sensitive parameters.

Usage and setup

No verified universal installation command is provided for this entry. Follow the official documentation for your agent version.

Supported agents

Specific agent compatibility unknown

Sources and license

Source check date (not a safety certification): 2026-09-29

MIT

Original source ↗ License terms ↗ Documentation ↗

Related skills

Before using a third-party skill, review its source, permissions and executable content. Commands are for viewing and copying only; 2lus does not execute them.

2lus AI Skills Library provides curated educational guides. Third-party content, licenses and availability are governed by their original sources. Review permissions and executable content before use or installation.