agentleFS
agentsdanceagentsdance/agentskills/skills/database-query-optimizer/SKILL.md

database-query-optimizer

Skill3 starsChanged 11 months agoApache-2.0View on GitHub

Optimize database queries for better performance. Use when the user needs to improve slow queries, add indexes, or optimize database operations.

The file

Files
SKILL.md

Database Query Optimizer

Improve database query performance across SQL and NoSQL databases.

Query Analysis

Explain Plans (PostgreSQL)

EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';

-- Output interpretation
Seq Scan on users  (cost=0.00..35.50 rows=1 width=100)
  Filter: (email = 'test@example.com'::text)
  Rows Removed by Filter: 999

Add Index

CREATE INDEX idx_users_email ON users(email);

-- After index
Index Scan using idx_users_email on users  (cost=0.00..8.27 rows=1 width=100)

Common Optimizations

Avoid SELECT *

-- Bad
SELECT * FROM users WHERE id = 1;

-- Good
SELECT id, name, email FROM users WHERE id = 1;

Use JOINs Instead of Subqueries

-- Bad
SELECT * FROM orders WHERE user_id IN (SELECT id FROM users WHERE status = 'active');

-- Good
SELECT o.* FROM orders o
JOIN users u ON o.user_id = u.id
WHERE u.status = 'active';

Batch Inserts

-- Bad: Individual inserts
INSERT INTO items (name) VALUES ('a');
INSERT INTO items (name) VALUES ('b');

-- Good: Batch insert
INSERT INTO items (name) VALUES ('a'), ('b'), ('c');

Index Types

TypeUse Case
B-treeDefault, equality and range
HashEquality only
GINFull-text, arrays, JSONB
GiSTGeometric, full-text
PartialSubset of rows

MongoDB Optimization

// Check query explanation
db.users.find({ email: "test@example.com" }).explain("executionStats");

// Create index
db.users.createIndex({ email: 1 });

// Compound index
db.orders.createIndex({ userId: 1, createdAt: -1 });

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.