SQL Development
Purpose
Design efficient database schemas, write optimized SQL queries, and manage data operations following database best practices and performance patterns.
When to Use
- Designing database schemas
- Writing data queries
- Optimizing slow queries
- Creating database migrations
- Managing data relationships
Key Capabilities
- Schema Design - Create normalized, efficient table structures
- Query Optimization - Write performant SELECT, INSERT, UPDATE, DELETE
- Index Strategy - Design indexes for query performance
Approach
- Design schema following normalization principles
- Define primary keys, foreign keys, and constraints
- Write queries using proper JOINs and WHERE clauses
- Create indexes for frequently queried columns
- Use EXPLAIN to analyze query performance
- Test with realistic data volumes
Example
Context: Task management database
-- Schema Design
CREATE TABLE tasks (
id VARCHAR(50) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
assigned_agent VARCHAR(100) NOT NULL,
status VARCHAR(20) NOT NULL,
priority VARCHAR(20) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
started_at TIMESTAMP NULL,
completed_at TIMESTAMP NULL,
INDEX idx_status (status),
INDEX idx_agent_status (assigned_agent, status),
INDEX idx_created (created_at)
);
-- Optimized Query
SELECT t.id, t.title, t.status, a.name as agent_name
FROM tasks t
JOIN agents a ON t.assigned_agent = a.agent_file
WHERE t.status IN ('pending', 'active')
AND t.priority = 'high'
ORDER BY t.created_at DESC
LIMIT 10;
Best Practices
- ✅ Use indexes on frequently queried columns
- ✅ Avoid SELECT * - specify needed columns
- ✅ Use prepared statements to prevent SQL injection
- ✅ Normalize data to reduce redundancy
- ❌ Avoid: N+1 query problems
- ❌ Avoid: Missing WHERE clause on large tables
1---2name: sql-development3description: Design efficient database schemas, write optimized queries with proper indexes, and manage data operations following best practices4---5
6# SQL Development
7
8## Purpose
9Design efficient database schemas, write optimized SQL queries, and manage data operations following database best practices and performance patterns.
10
11## When to Use
12- Designing database schemas
13- Writing data queries
14- Optimizing slow queries
15- Creating database migrations
16- Managing data relationships
17
18## Key Capabilities
191. **Schema Design** - Create normalized, efficient table structures
202. **Query Optimization** - Write performant SELECT, INSERT, UPDATE, DELETE
213. **Index Strategy** - Design indexes for query performance
22
23## Approach
241. Design schema following normalization principles
252. Define primary keys, foreign keys, and constraints
263. Write queries using proper JOINs and WHERE clauses
274. Create indexes for frequently queried columns
285. Use EXPLAIN to analyze query performance
296. Test with realistic data volumes
30
31## Example
32**Context**: Task management database
33````sql
34-- Schema Design
35CREATE TABLE tasks (
36 id VARCHAR(50) PRIMARY KEY,
37 title VARCHAR(255) NOT NULL,
38 assigned_agent VARCHAR(100) NOT NULL,
39 status VARCHAR(20) NOT NULL,
40 priority VARCHAR(20) NOT NULL,
41 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
42 started_at TIMESTAMP NULL,
43 completed_at TIMESTAMP NULL,
44 INDEX idx_status (status),
45 INDEX idx_agent_status (assigned_agent, status),
46 INDEX idx_created (created_at)
47);
48
49-- Optimized Query
50SELECT t.id, t.title, t.status, a.name as agent_name
51FROM tasks t
52JOIN agents a ON t.assigned_agent = a.agent_file
53WHERE t.status IN ('pending', 'active')
54 AND t.priority = 'high'
55ORDER BY t.created_at DESC
56LIMIT 10;
57````
58
59## Best Practices
60- ✅ Use indexes on frequently queried columns
61- ✅ Avoid SELECT * - specify needed columns
62- ✅ Use prepared statements to prevent SQL injection
63- ✅ Normalize data to reduce redundancy
64- ❌ Avoid: N+1 query problems
65- ❌ Avoid: Missing WHERE clause on large tables
66