CtrlK
BlogDocsLog inGet started
Tessl Logo

sql-master

Database expert - query optimization, schema design

Install with Tessl CLI

npx tessl i github:TurnaboutHero/oh-my-antigravity --skill sql-master
What are skills?

Overall
score

61%

Does it follow best practices?

Validation for skill structure

SKILL.md
Review
Evals

SQL Master - Database Architect

You are SQL Master, the database and SQL specialist.

Query Optimization

Indexing Strategy

-- Before: Slow query
SELECT * FROM orders 
WHERE user_id = 123 
AND created_at > '2024-01-01';

-- Analysis
EXPLAIN ANALYZE SELECT ...;
-- Seq Scan (SLOW)

-- Add composite index
CREATE INDEX idx_orders_user_created 
ON orders(user_id, created_at);

-- After: Fast query with index scan
-- Index Scan (FAST)

JOIN Optimization

-- ❌ Bad - Multiple subqueries
SELECT u.name,
  (SELECT COUNT(*) FROM orders WHERE user_id = u.id) as order_count,
  (SELECT SUM(total) FROM orders WHERE user_id = u.id) as total_spent
FROM users u;

-- ✅ Good - Single JOIN with aggregation
SELECT u.name,
  COUNT(o.id) as order_count,
  SUM(o.total) as total_spent
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name;

Schema Design

Normalization

-- Users table
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  name VARCHAR(100) NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

-- Orders table
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id),
  total DECIMAL(10, 2) NOT NULL,
  status VARCHAR(20) NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

-- Order items (many-to-many)
CREATE TABLE order_items (
  id SERIAL PRIMARY KEY,
  order_id INTEGER REFERENCES orders(id) ON DELETE CASCADE,
  product_id INTEGER REFERENCES products(id),
  quantity INTEGER NOT NULL,
  price DECIMAL(10, 2) NOT NULL
);

-- Indexes
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_orders_status ON orders(status);
CREATE INDEX idx_order_items_order_id ON order_items(order_id);

Advanced Queries

Window Functions

-- Running total
SELECT 
  date,
  revenue,
  SUM(revenue) OVER (ORDER BY date) as running_total
FROM daily_sales;

-- Rank by category
SELECT 
  product_name,
  category,
  sales,
  RANK() OVER (PARTITION BY category ORDER BY sales DESC) as rank
FROM products;

CTEs (Common Table Expressions)

WITH monthly_sales AS (
  SELECT 
    DATE_TRUNC('month', created_at) as month,
    SUM(total) as revenue
  FROM orders
  GROUP BY month
),
growth AS (
  SELECT 
    month,
    revenue,
    LAG(revenue) OVER (ORDER BY month) as prev_revenue,
    (revenue - LAG(revenue) OVER (ORDER BY month)) / 
      LAG(revenue) OVER (ORDER BY month) * 100 as growth_pct
  FROM monthly_sales
)
SELECT * FROM growth WHERE growth_pct > 10;

Performance Tuning

Query Plan Analysis

-- Get slow queries
SELECT 
  query,
  calls,
  total_time,
  mean_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;

-- Analyze table statistics
ANALYZE users;
VACUUM ANALYZE orders;

"A well-designed database is the foundation of a scalable application."

Repository
github.com/TurnaboutHero/oh-my-antigravity
Last updated
Created

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.