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

"data-sql-optimization"

"Optimize SQL query performance using EXPLAIN analysis, indexing strategies, and common anti-pattern fixes. Use this skill when the user needs to speed up slow queries, design indexes, fix N+1 problems, or optimize database performance — even if they say 'this query is slow', 'optimize our database', 'which indexes do we need', or 'our dashboard takes 30 seconds to load'.".

インストール方法を見る

含まれるファイル(4)

  • SKILL.md5.2 KB
  • examples/sample_scenario.md6.3 KB
  • references/cte-vs-temp.md11.1 KB
  • references/pg-optimization.md13.3 KB

SKILL.md(原文)

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

SQL Query Optimization

Framework

IRON LAW: Measure Before Optimizing

NEVER guess which query is slow or why. Use EXPLAIN (EXPLAIN ANALYZE in
PostgreSQL) to see the actual execution plan. The database's plan often
differs from what you expect — a query you think is efficient may do
a full table scan, and a complex-looking query may use an index perfectly.

Measure → identify bottleneck → fix → measure again.

EXPLAIN Output Reading

Key metrics in EXPLAIN ANALYZE (PostgreSQL):

MetricWhat It MeansRed Flag
Seq ScanFull table scanOn large tables (>100K rows)
Index ScanUsing an indexExpected for filtered queries
Nested LoopJoin method (row-by-row)On large tables without index
Hash JoinJoin method (hash table)Normal for larger tables
SortSorting resultsWithout index support on large sets
Actual TimeMilliseconds for this stepCompare to identify bottleneck
RowsActual rows processed vs estimatedLarge mismatch = stale statistics

Indexing Strategy

When to IndexIndex TypeExample
WHERE clause columnB-Tree (default)CREATE INDEX idx_user_email ON users(email)
JOIN columnB-TreeCREATE INDEX idx_order_user ON orders(user_id)
Composite filterComposite indexCREATE INDEX idx_order_status_date ON orders(status, created_at)
Text searchGIN / Full-textCREATE INDEX idx_product_name_gin ON products USING gin(name gin_trgm_ops)
Range queriesB-TreeColumns used with BETWEEN, >, <

Composite index column order matters: Put the most selective (highest cardinality) column first. INDEX(status, date) is good if you always filter by status. INDEX(date, status) is better if you always filter by date range first.

Common Anti-Patterns

Anti-PatternProblemFix
SELECT *Reads all columns, prevents index-only scansSelect only needed columns
Subquery in WHERERe-executes for each rowRewrite as JOIN or CTE
OR in WHEREPrevents index useRewrite as UNION or separate queries
Function on indexed columnWHERE YEAR(date) = 2024 bypasses indexWHERE date >= '2024-01-01' AND date < '2025-01-01'
N+1 queries1 query for list + N queries for detailsJOIN or batch query with IN
Missing paginationFetching all rows when only showing 20LIMIT + OFFSET or keyset pagination
Implicit type conversionWHERE id = '123' (string vs int)Use correct type: WHERE id = 123

Optimization Workflow

  1. Identify slow queries: Database slow query log (pg_stat_statements, MySQL slow log)
  2. Run EXPLAIN ANALYZE on the slowest
  3. Find the bottleneck: Seq Scan on large table? Missing index? Expensive sort?
  4. Apply fix: Add index, rewrite query, or restructure schema
  5. Verify: Run EXPLAIN ANALYZE again — confirm improvement
  6. Monitor: Check that fix didn't degrade other queries

Partitioning (Large Tables)

When tables exceed millions of rows:

StrategyHow It WorksBest For
Range partitionSplit by date range (monthly, yearly)Time-series data, logs
Hash partitionDistribute by hash of a columnEven distribution, high-throughput
List partitionSplit by specific valuesMulti-tenant, status-based

Output Format

# Query Optimization: {Context}

## Slow Query
```sql
{the original slow query}
  • Execution time: {current ms}
  • Rows scanned: {N}
  • Problem: {what EXPLAIN revealed}

Fix Applied

{What was changed — new index, query rewrite, etc.}

Result

  • Execution time: {original ms} → {optimized ms} ({X% improvement})
  • Rows scanned: {original N} → {optimized N}

## Gotchas

- **Indexes have write cost**: Every INSERT/UPDATE must update all indexes. Over-indexing slows writes. Index what you query, not everything.
- **Statistics can be stale**: If EXPLAIN estimates are way off from actuals, run `ANALYZE` (PostgreSQL) or `ANALYZE TABLE` (MySQL) to update statistics.
- **Query cache hides problems**: A query may appear fast because it's cached. Test with cache cleared or cold start.
- **ORM-generated queries**: ORMs (Django, SQLAlchemy, ActiveRecord) generate SQL that may not be optimal. Always inspect the actual SQL for performance-critical paths.
- **Connection pooling**: Sometimes the bottleneck isn't the query but connection overhead. Use connection pooling (PgBouncer, ProxySQL) for high-concurrency applications.

## References

- For PostgreSQL-specific optimization, see `references/pg-optimization.md`
- For CTE vs temp table performance comparison, see `references/cte-vs-temp.md`

レビュー

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

同じリポジトリのスキル

概要と使いどころ

"Implement and select ad bidding strategies from manual CPC to automated target-CPA and target-ROAS. Use this skill when the user needs to choose a bidding strategy, set up automated bidding, or optimize bid parameters — even if they say 'what bidding strategy should I use', 'target CPA setup', or 'smart bidding configuration'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

"Optimize advertising budget allocation across campaigns using marginal returns analysis. Use this skill when the user needs to distribute budget across multiple campaigns, optimize spend pacing, or maximize overall ROAS under budget constraints — even if they say 'how to split my ad budget', 'campaign budget optimization', or 'diminishing returns on ad spend'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

"Build CTR prediction models for estimating ad click-through rates from features. Use this skill when the user needs to predict click probability, build an ad ranking model, or evaluate ad creative performance — even if they say 'predict click rate', 'ad relevance scoring', or 'which ad will get more clicks'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

"Implement Generalized Second Price auction for ad slot allocation and pricing. Use this skill when the user needs to understand search ad auctions, compute ad positions and costs-per-click, or analyze bidding dynamics — even if they say 'how does Google Ads auction work', 'ad rank calculation', or 'second price auction for ads'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

"Implement VCG mechanism for incentive-compatible ad slot allocation with truthful bidding. Use this skill when the user needs to design a truthful auction mechanism, compute externality-based payments, or understand why platforms may prefer GSP over VCG — even if they say 'truthful auction design', 'VCG payments', or 'incentive-compatible mechanism'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

"Explain blockchain fundamentals including distributed ledger architecture, consensus mechanisms, and block structure. Use this skill when the user needs to understand blockchain concepts, evaluate whether blockchain fits a use case, or design a blockchain-based solution — even if they say 'how does blockchain work', 'do I need blockchain', or 'distributed ledger'.".

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

charlieviettq/awesome-agent-skill262026年7月20日 更新

charlieviettq のスキルをすべて見る

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