Skip to content

Database Bottlenecks with AI: Slow Queries, Execution Plans, Pool Exhaustion

When response times rise with load and the app servers look calm, the database is often the next suspect. The work is reading slow query logs, execution plans and pool metrics, then turning them into a clear finding for the DBA or the developers. AI tools are good at explaining plans and suggesting what to check. They are also quick to suggest an index for everything. Here is how to use them well.

A slow request can wait in three places: for a free connection in the pool, in a slow query, or on locks and a busy database serverWhere the time goes on a slow requestuserApp serverthread waitsborrowConnectionpoolmax 20SQLDatabase1. waiting for afree connection2. the query itselfis slow3. locks or a busydatabase server
Find out which wait it is before you tune anything.

Step 1: Find which wait it is

A request that is "slow in the database" can be slow in three different places. It can wait for a connection from the pool, the query can be slow, or the query can wait on locks or a busy database server. Each needs a different fix, so separate them first.

Pool waits show up in the application logs. HikariCP, for example, logs a message like this when no connection is free in time:

HikariPool-1 - Connection is not available, request timed out after 30000ms.

If you see that, the database may be fine and the pool may simply be too small for the load, or connections may be held too long (or leaked) by the code. Paste the pool settings, the error and the thread count of the app to the AI and ask it to work out how many connections the load needs at the observed query time. Before raising the pool size, check that the database can take the extra connections.

Step 2: Turn on slow query logging

-- MySQL
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;   -- seconds

-- PostgreSQL (postgresql.conf, then reload)
log_min_duration_statement = 1000   # milliseconds

On PostgreSQL the pg_stat_statements extension gives totals per query shape, which is often more useful than single slow entries. On Oracle, AWR reports cover the same ground, but they need the Diagnostics Pack license, so check that before using them. Summarize the slow log first (for MySQL, mysqldumpslow or pt-query-digest) and pick the queries with the most total time, not just the single slowest one.

Step 3: Get the plan and give the AI full context

-- PostgreSQL: runs the query and shows real timings
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;

-- MySQL 8.0.18 and later
EXPLAIN ANALYZE SELECT ...;

Be careful: EXPLAIN ANALYZE really executes the statement. Use it on a SELECT, or inside a transaction you roll back.

Evidence for one slow query: the SQL text, the execution plan, the table definitions with indexes, and row counts, sent together to the AIWhat to give the AI for one slow querythe SQLthe planDDL + indexesrow countsAsk for:why it is slow+ how to prove a fix
Without the plan and the indexes, the AI can only guess.
PostgreSQL 15. This query is the top one by total time in
pg_stat_statements during our load test (avg 850 ms, 40 calls/s).
Below: the query, EXPLAIN (ANALYZE, BUFFERS) output, the CREATE
TABLE and CREATE INDEX statements for the tables involved, and
row counts.
1. Explain the plan in plain words: where is the time spent?
2. Are the row estimates far from the actual rows anywhere?
3. Suggest at most 2 changes (index or query rewrite) and,
   for each, the plan you would expect afterwards.
4. What could each change make worse?

Mask real values in the query before you paste it. Table and column names are usually fine, but check your company policy.

Step 4: Prove the fix

Apply one change in the test environment, run EXPLAIN again and compare with the AI's expected plan. Then rerun the same load test. An index that turns a sequential scan into an index scan is a good sign, but the load test is the proof.

Checklist for an AI-suggested index: the plan changed, the query is faster under load, writes are not slower, and it does not duplicate an existing indexBefore you accept a new indexPlan changed as expected?Query faster at test load?Inserts and updates still OK?No duplicate of an existing index?
An index that helps one read can slow down every write.

Verify the AI's answer

  • Every operation it explains should be in your plan output.
  • If it says "add an index on X", check that no existing index already starts with X.
  • Ask what the change costs on writes, and test the write transactions too.
SignalLikely causeFirst action
Pool timeout errors, DB CPU lowPool too small or connections heldPool metrics, connection hold time
Seq scan on a large tableMissing or unusable indexPlan plus index list
Estimates far from actual rowsStale statisticsANALYZE the table, recheck the plan
Same query fast alone, slow under loadLocks or a saturated serverLock waits and DB server metrics

Frequently asked questions

Can the AI read an Oracle AWR report?
Yes, it can explain the top wait events and top SQL sections if you paste them. Remove host and instance names first.

Should I let the AI rewrite the query?
It can suggest rewrites, but you must confirm the result set is identical. Compare row counts and a sample of rows.

How many pool connections are right?
There is no fixed number. It depends on query time, concurrency and what the database can handle. Measure, change, measure again.

Tomorrow in the AI in Performance Testing series: Dynatrace plus AI, with Davis problems and connecting Claude to Dynatrace through the official MCP server.

Page views