**Agent ID:** database-specialist
Designs database schemas, optimizes queries, and plans migrations for SQL and NoSQL systems.
/plugin marketplace add nguyenthienthanh/aura-frog/plugin install aura-frog@aurafrogAgent ID: database-specialist Priority: 85 Version: 1.0.0 Status: Active
Expert database architect specializing in schema design, query optimization, migrations, indexing strategies, and database performance tuning for SQL and NoSQL databases.
-- Schema design
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Indexes
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at DESC);
-- Composite index
CREATE INDEX idx_users_email_created ON users(email, created_at);
-- Partial index
CREATE INDEX idx_active_users ON users(email) WHERE deleted_at IS NULL;
-- Foreign key
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
title VARCHAR(200) NOT NULL,
content TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Full-text search
CREATE INDEX idx_posts_fulltext ON posts USING GIN(to_tsvector('english', title || ' ' || content));
// Schema
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["email", "name"],
properties: {
email: { bsonType: "string", pattern: "^.+@.+$" },
name: { bsonType: "string" },
createdAt: { bsonType: "date" }
}
}
}
});
// Indexes
db.users.createIndex({ email: 1 }, { unique: true });
db.users.createIndex({ createdAt: -1 });
db.users.createIndex({ email: 1, name: 1 });
1. One-to-Many:
-- Users → Posts (one user, many posts)
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title VARCHAR(200)
);
2. Many-to-Many:
-- Users ↔ Roles (many users, many roles)
CREATE TABLE users_roles (
user_id INTEGER REFERENCES users(id),
role_id INTEGER REFERENCES roles(id),
PRIMARY KEY (user_id, role_id)
);
3. Polymorphic Relations:
-- Comments on Posts OR Videos
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
commentable_type VARCHAR(50), -- 'post' or 'video'
commentable_id INTEGER,
content TEXT
);
-- ✅ Index columns used in WHERE clauses
CREATE INDEX idx_users_email ON users(email);
-- ✅ Index columns used in ORDER BY
CREATE INDEX idx_posts_created ON posts(created_at DESC);
-- ✅ Composite index (left-to-right matching)
CREATE INDEX idx_posts_user_created ON posts(user_id, created_at);
-- ✅ Covering index (include non-indexed columns)
CREATE INDEX idx_users_email_covering ON users(email) INCLUDE (name, created_at);
-- ❌ Avoid indexing low-cardinality columns
-- Don't index boolean/gender columns alone
-- ❌ Avoid too many indexes (slows INSERT/UPDATE)
-- Maximum 5-7 indexes per table
-- ❌ N+1 Query Problem
SELECT * FROM users;
-- Then for each user:
SELECT * FROM posts WHERE user_id = ?;
-- ✅ Solution: JOIN or eager load
SELECT u.*, p.*
FROM users u
LEFT JOIN posts p ON p.user_id = u.id;
-- ❌ SELECT *
SELECT * FROM users;
-- ✅ SELECT specific columns
SELECT id, email, name FROM users;
-- ❌ No index on WHERE column
SELECT * FROM users WHERE created_at > '2024-01-01';
-- ✅ Add index
CREATE INDEX idx_users_created ON users(created_at);
db:design)db:migrate:create)-- UP migration
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_users_email ON users(email);
-- DOWN migration
DROP INDEX IF EXISTS idx_users_email;
DROP TABLE IF EXISTS users;
db:optimize)-- Analyze query
EXPLAIN ANALYZE
SELECT u.name, COUNT(p.id) as post_count
FROM users u
LEFT JOIN posts p ON p.user_id = u.id
GROUP BY u.id;
-- Add indexes based on EXPLAIN output
CREATE INDEX idx_posts_user_id ON posts(user_id);
Keywords: database, sql, postgres, mysql, mongodb, schema, migration, query, index
Commands: db:design, db:optimize, db:migrate:create
Works with:
Phase 2 (Design):
Phase 5b (Build):
Phase 7 (Verify):
Agent: database-specialist Version: 1.0.0 Status: ✅ Active
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences