Skip to main content

Claude Prompt to Design a Database Indexing Strategy

Analyze query patterns and table structure to get optimal indexes: composite, covering, and partial indexes plus index maintenance strategies.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a database indexing expert. Analyze the following schema and query patterns to design an optimal indexing strategy.

**Database Engine:** MySQL 8.0
**Table Schema:**
```sql
Paste your CREATE TABLE statement here including existing indexes
```

**Common Query Patterns:**
```sql
Paste your 5-10 most common queries here
```

**Current Table Size:** 15 million rows, 4GB data, 1.2GB indexes
**Write/Read Ratio:** 20% writes, 80% reads — with occasional bulk import jobs

**Perform This Analysis:**

1. **Current Index Audit:**
   - List all existing indexes and their types (B-tree, hash, GIN, etc.)
   - Identify redundant indexes (index A is a prefix of index B)
   - Identify unused indexes (indexes that no query benefits from)
   - Calculate estimated storage overhead of current indexes

2. **Query Pattern Analysis:**
   For each query pattern:
   - `EXPLAIN ANALYZE` interpretation (what the planner would do)
   - Is it doing a sequential scan when an index scan is possible?
   - Estimated cost with current indexes vs. with recommended indexes
   - Index selectivity analysis (is the column selective enough to benefit from an index?)

3. **Recommended Indexes:**
   For each recommendation:
   ```sql
   CREATE INDEX <name> ON <table> (<columns>) <options>;
   -- Reason: [why this index helps]
   -- Queries benefited: [which queries]
   -- Estimated improvement: [X]x faster
   -- Storage cost: ~[X] MB
   ```

   Index types to consider:
   - **Composite indexes** — correct column order based on query patterns (equality first, then range, then sort)
   - **Covering indexes** — INCLUDE columns to enable index-only scans
   - **Partial indexes** — WHERE clause to index only relevant rows (e.g., active records)
   - **Expression indexes** — for computed/transformed column lookups
   - Full-text indexes for search columns

4. **Index Ordering Rules:**
   Explain the column ordering logic:
   - Equality conditions before range conditions
   - High selectivity columns first
   - Sort columns at the end (matching ORDER BY direction)
   - Why order matters with concrete examples from the provided queries

5. **Trade-off Analysis:**
   | Index | Read Improvement | Write Overhead | Storage Cost | Recommendation |
   |-------|-----------------|----------------|--------------|----------------|
   | <name> | <estimate> | <estimate> | <estimate> | Add / Skip / Defer |

6. **Maintenance Plan:**
   - Index rebuild/reindex schedule based on 15 million rows, 4GB data, 1.2GB indexes and write volume
   - Bloat monitoring queries
   - Statistics update recommendations (ANALYZE frequency)
   - How to safely add indexes to a 15 million rows, 4GB data, 1.2GB indexes table without locking (CONCURRENTLY / online DDL)

What this prompt does

This prompt makes the model act as a database indexing expert. You give it [database_engine], your [table_schema], your [query_patterns], the [table_size], and the [write_read_ratio], and it produces a full indexing strategy rather than a single quick suggestion. It audits existing indexes, analyzes each query pattern, recommends new indexes with CREATE INDEX statements, explains column ordering, lays out trade-offs, and gives a maintenance plan.

The structure forces the model to show its reasoning. For each query it walks through what the planner would do under EXPLAIN ANALYZE, whether a sequential scan is happening, and whether the column is selective enough to benefit from an index. Because you supply the [write_read_ratio], it can weigh read speedups against write overhead — an index that helps an 80%-read table may be wrong on a write-heavy one. The [additional_index_type] slot lets you nudge it toward full-text, GIN, or other specialized indexes your workload needs.

When to use it

  • When an app slows down and you suspect missing or wrong indexes rather than insufficient hardware.
  • Before adding an index to a large table, to confirm it actually helps the queries you run.
  • To find redundant indexes (where one is a prefix of another) you can safely drop.
  • When you need correct composite-index column order for multi-condition queries.
  • To plan a safe, non-locking index rollout on a big production table.

Example output

Expect a sectioned report: an audit listing current indexes and their types, a per-query analysis with planner reasoning and estimated cost before/after, a set of recommended CREATE INDEX statements each annotated with the reason, the queries it helps, estimated speedup, and storage cost. It includes a trade-off table (read improvement vs. write overhead vs. storage, with an Add/Skip/Defer call) and a maintenance plan covering reindex schedules, bloat monitoring, and online DDL.

Pro tips

  • Paste your actual [table_schema] including existing indexes — the audit can only spot redundancy if it sees what you already have.
  • Give it 5-10 real [query_patterns], the ones that run most often, not hypothetical queries. Index quality is only as good as the workload you describe.
  • Set [write_read_ratio] honestly. On write-heavy tables I expect it to recommend fewer indexes, because every index taxes inserts and updates.
  • Match [database_engine] exactly (MySQL 8.0 vs. Postgres 15) so the CREATE INDEX syntax and online-DDL advice (CONCURRENTLY vs. online DDL) are correct.
  • Treat the "estimated improvement" numbers as directional, not measured — always benchmark the suggested indexes on a copy of production before shipping.
  • Use [additional_index_type] to request specialized indexes (full-text, partial, expression) when your queries filter on computed columns or search text.

Frequently Asked Questions

Which databases does this prompt support?
It adapts to whatever you set in `[database_engine]`, such as MySQL 8.0, PostgreSQL, or others. Match the version exactly so the generated CREATE INDEX syntax and online-DDL advice are correct for your engine.
Will it tell me which indexes to remove?
Yes. The audit step identifies redundant indexes, where one index is a prefix of another, and indexes that no query benefits from. You should confirm against your real query log before dropping anything in production.
Are the speed-up estimates reliable?
The estimated improvements are directional, based on the planner reasoning the model infers, not measured benchmarks. Always test the recommended indexes on a copy of production data before trusting the numbers.
Can it handle composite index column ordering?
Yes, and that is one of its strengths. It applies the rule of equality conditions first, then range, then sort columns, and explains the ordering using your actual queries as examples.
Does it consider write overhead, not just read speed?
Yes. Because you provide the `[write_read_ratio]`, it weighs each index's read benefit against its write cost and storage, then gives an Add, Skip, or Defer recommendation per index.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Data & SQL Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support