Gemini Prompts for Database Query Optimization: 5 Ready-to-Use Ideas

Five Gemini prompts for database query optimization, arranged in the order a slow query actually gets fixed: read the plan, question the index, rewrite the join, then prove the change before it ships. Paste them in sequence and you have a review trail, not a guess.

Before you start: the variables

Variable What to put in it
[engine and version] The database you are actually running, since plan output and hints differ between engines and versions.
[slow query] The query text, with the parameters it was called with.
[table sizes] Row counts and how fast each table grows, so the advice is not written for an empty table.
[latency target] The number the query has to hit, and whether that is average or worst case.
[read and write mix] How often the table is read versus written, because an index that speeds reads slows writes.

The prompts

1. Plan first, query second

Here is a slow query on [engine and version]: [slow query]. Ask me for the execution plan before you suggest any change, then walk the plan from the outermost node inward and name the operation that costs the most. Do not propose a fix until you have named it.

What it does: Forces the diagnosis to come from the plan rather than from a pattern the model has seen before.

How to use: Replace the engine and query, then paste the plan text when it asks. Keep the plan attached for the following prompts.

2. Index candidates from the predicates

Given [slow query], [table sizes], and [read and write mix], list candidate indexes for [engine and version]. For each one, name the columns in order, the predicate it serves, the write cost it adds, and what it would not help. Rank them and tell me which one you would test first.

What it does: Turns index guessing into a ranked shortlist with the tradeoff attached to each entry.

How to use: Replace the query, sizes, and mix. Test the top candidate on a copy of the data before touching production.

3. Rewrite the join, keep the result

Rewrite [slow query] for [engine and version] so it returns exactly the same rows. Try a different join order, a set-based form instead of row-by-row logic, and an earlier filter. Show each variant with the reasoning, and flag any variant where the result set could change.

What it does: Produces alternatives you can compare, with the equivalence risk stated up front.

How to use: Replace the query and engine. Diff the row counts of the original and the rewrite before you trust either one.

4. The tradeoff note your reviewer will ask for

Write a short review note for this change: [proposed change] on [engine and version], against a [latency target] target with [read and write mix] traffic. Cover what gets faster, what gets slower, what breaks if the data grows, and how to roll it back.

What it does: Turns a performance tweak into a decision someone else can approve.

How to use: Replace the change, target, and traffic mix. Paste it into the pull request instead of describing the change twice.

5. Regression check before it merges

I am changing [slow query] on [engine and version]. Give me a test plan that catches a slower query elsewhere: which other queries touch the same tables, what to measure before and after, and which metric would tell me the change backfired. Keep it to what I can run locally.

What it does: Guards the queries you did not mean to touch.

How to use: Replace the query and engine. Record the before numbers, then rerun after the change and compare the same metric.

A quick worked example

Take an orders table that grows daily and a report query that filters on status and created date, with [engine and version] as Postgres 15 and [latency target] as a two-second worst case. Prompt 1 asks for the plan and points at a sequential scan on the orders table rather than the join. Prompt 2 proposes a composite index on status and created date, notes that the write cost lands on order inserts, and ranks a single-column index behind it. Prompt 3 shows a version that filters before joining. Prompt 4 writes the note your reviewer needs about insert cost. Prompt 5 lists the other reports that read the same table, so you find out about them before they page you. Nothing here needs a number you did not measure yourself.

Keep going

Take what works here and paste it straight into your tool of choice.

Leave a Comment