// 36 tips tagged "database"
Tell Claude to read your models, migrations, and relationships, then generate factory definitions that produce realistic, interconnected test data — proper names, valid emails, sensible dates, correct foreign keys, and state variations that cover your actual business logic.
When a page takes five seconds to load or an API endpoint times out under load, tell Claude which route is slow and it traces the entire code path — controller, services, queries, loops — identifying N+1 queries, redundant computations, missing indexes, and cacheable operations, then fixes each bottleneck.
Tell Claude what data you need — "all users who signed up this month but haven't made a purchase" — and it reads your schema, understands the relationships, and writes the query in your framework's query builder or raw SQL, with the right joins, conditions, and aggregations.
Instead of memorizing migration syntax, describe the schema change you need — "add a nullable published_at timestamp to the posts table" or "create a many-to-many relationship between users and roles" — and Claude writes the complete migration file using your framework's conventions.
When you need to share sample data, create test fixtures, or debug with production-like records, tell Claude to sanitize the data — replacing real names, emails, phone numbers, and IDs with realistic fakes while keeping the structure, relationships, and data types intact.
When a specific query is slow, run EXPLAIN on it and paste the execution plan into Claude — it reads the plan, spots full table scans, bad join orders, and missing indexes, then rewrites the query and suggests schema changes to make it fast.
Tell Claude what you need — "add a tagging system where posts can have multiple tags" — and it writes the migration, creates the pivot table, adds the model relationships, and updates the relevant files, all from a plain English description of your schema change.
Tell Claude to read your models and database schema, then generate factory definitions and seed data that actually make sense — proper relationships, realistic values, domain-appropriate formats, and edge cases, not random strings and lorem ipsum.
Tell Claude what data you need — "monthly revenue by product category, excluding refunds, for the last quarter" — and it reads your schema, writes the query with proper joins and aggregations, and explains what each part does so you can verify it's correct.
Tell Claude to read your queries, ORM calls, and WHERE clauses, then suggest which database indexes to add — it identifies missing indexes that would speed up slow queries, flags redundant indexes wasting write performance, and generates the migration to add them.
Tell Claude to read your database schema and models, then add input validation to your API endpoints and forms — column types become type checks, NOT NULL becomes required, string lengths become max rules, and foreign keys become exists checks, all derived from the actual constraints.
When you need to transform existing data — backfill a new column, clean up inconsistent formats, merge duplicate records, or split a field into parts — describe the transformation in English and Claude writes a safe, reversible migration script.