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.
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.
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.
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.
| Signal | Likely cause | First action |
|---|---|---|
| Pool timeout errors, DB CPU low | Pool too small or connections held | Pool metrics, connection hold time |
| Seq scan on a large table | Missing or unusable index | Plan plus index list |
| Estimates far from actual rows | Stale statistics | ANALYZE the table, recheck the plan |
| Same query fast alone, slow under load | Locks or a saturated server | Lock 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.