tencent cloud

TencentDB for PostgreSQL

AI-Assisted Slow SQL Diagnosis and Optimization

Download
Focus Mode
Font Size
Last updated: 2026-06-08 10:32:13

Overview

TencentDB for PostgreSQL integrates with the DBbrain intelligent Ops platform and AI large models to provide automated slow SQL diagnosis and optimization recommendations. The system automatically collects slow query logs, analyzes execution plans using AI models, and provides suggestions such as index optimization, SQL rewriting, and parameter tuning.

Feature Architecture

┌────────────────────────────────────────────────────────┐
│ DBbrain Intelligent Diagnosis │
├────────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────────┐ ┌─────────────┐ │
│ │ Slow Query │ │ AI Diagnostic Engine │ │ Optimization Suggestions │ │
│ │ Collection │ → │ (Large Model Analysis) │ → │ Generation │ │
│ └──────────┘ └──────────────┘ └─────────────┘ │
│ ↑ ↑ │
│ │ │ │
│ ┌──────────┐ ┌──────────────┐ │
│ │ pg_stat │ │ EXPLAIN │ │
│ │ Statistics │ │ Execution Plan │ │
│ └──────────┘ └──────────────┘ │
└────────────────────────────────────────────────────────┘

Usage

Console Operations

1. Log in to the DBbrain Console.
2. On the instance management page, select a PostgreSQL instance.
3. Go to Slow SQL AnalysisAI Diagnosis.
4. View the AI diagnostic report and optimization suggestions for each slow query.

SQL-Level Usage

Note:
In the following example, the LLM name <your-llm-model> is a placeholder only. Replace it with the model you have actually enabled, such as Tencent Hunyuan (hunyuan-*).
By integrating the tencentdb_ai plugin, you can perform AI analysis on slow queries directly within SQL. Note: In the example, when target SQL is retrieved via pg_stat_statements, it is recommended to use queryid for precise identification in production environments, rather than using fuzzy LIKE matching, to avoid matching irrelevant queries.
-- 1) Obtain the execution plan for the slow query
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT * FROM orders
WHERE customer_id = 12345
AND order_date > '2026-01-01'
ORDER BY order_date DESC;

-- 2) Use AI to analyze the execution plan (In production environments, it is recommended to retrieve the target SQL by queryid)
SELECT tencentdb_ai.chat_completions(
'<your-llm-model>',
'You are a PostgreSQL performance optimization expert. Analyze the following execution plan, identify performance bottlenecks, and provide optimization suggestions:' ||
E'\\n' ||
(SELECT query FROM pg_stat_statements WHERE queryid = $1)
);

Using tencentdb_index_advisor

-- Use intelligent index recommendations for slow queries.
SELECT * FROM tencentdb_index_advisor.advise_index(
'SELECT * FROM orders WHERE customer_id = $1 AND order_date > $2 ORDER BY order_date DESC'
);

-- Batch workload analysis
SELECT * FROM tencentdb_index_advisor.advise_index_workload(
ARRAY[
'SELECT * FROM orders WHERE customer_id = $1',
'SELECT * FROM orders WHERE status = $1 AND created_at > $2',
'SELECT count(*) FROM orders WHERE product_id = $1'
]
);

Typical Scenarios

Scenario 1: Missing Indexes

Problematic SQL:
SELECT * FROM users WHERE email = 'user@example.com';
-- Execution time: 2.5s, Seq Scan on users (rows=1000000)
AI Diagnostic Conclusion:
The table users contains 1 million rows, and the email column is not indexed.
A full table sequential scan results in excessively long execution time.
Optimization Suggestions:
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
-- Optimized execution time: 0.5ms

Scenario 2: Outdated Statistics

Problem: An incorrect JOIN order was selected in the query plan.
AI Diagnostic Conclusion:
The row quantity estimation deviation in pg_statistic exceeds 100 times.
Execute ANALYZE to update statistics.
Optimization Suggestions:
ANALYZE orders;
ANALYZE order_items;

Scenario 3: SQL Rewriting

Problematic SQL:
SELECT * FROM products WHERE id NOT IN (SELECT product_id FROM discontinued);
AI Optimization Suggestions:
-- Rewrite using NOT EXISTS to avoid NULL value issues and improve efficiency.
SELECT p.* FROM products p
WHERE NOT EXISTS (SELECT 1 FROM discontinued d WHERE d.product_id = p.id);

References

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback