数据库分析工具

MCP DB Analyzer是一款支持PostgreSQL、MySQL和SQLite的数据库分析工具,提供模式检查、索引问题检测、表膨胀/碎片分析及查询计划解释等功能,帮助AI助手提供具体的数据库优化建议。

  • by Dmitriusan
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Dmitriusan/mcp-db-analyzer

使用说明

MCP数据库分析仪

模型上下文协议(MCP)服务器,使AI助手能够深入了解您的数据库。它检查模式,检测索引问题,分析表膨胀/碎片,并解释查询计划,因此您的AI可以为您提供可操作的数据库优化建议,而不是通用建议。

支持 PostgreSQL, MySQL,以及 SQLite.

为什么是这个工具?

有几十个数据库MCP服务器——大多数是 CRUD网关 (运行查询、列出表)。这个工具 分析 您的数据库:模式问题、缺少索引、表臃肿、查询速度慢、真空运行状况。

其他分析MCP服务器(CrystalDBA、pg-dash、MCP PostgreSQL Ops)仅涵盖PostgreSQL。 MCP DB Analyzer是唯一支持PostgreSQL、MySQL和SQLite的分析MCP服务器 单独 npx 安装——没有Python,没有Go,没有Docker。

特性

  • 9个MCP工具 用于全面的数据库分析
  • PostgreSQL+MySQL+SQLite 通过支持 --driver 旗帜
  • 按设计只读 --所有查询都包装在只读事务中
  • Markdown输出 针对LLM消耗进行了优化
  • 零配置 --刚刚设置 DATABASE_URL

专业级别

生成可导出的诊断报告(HTML+PDF) 使用Pro许可证密钥。

  • 完整的JVM线程转储分析报告,其中包含可操作的建议
  • PDF导出以与您的团队共享
  • 优先支持

9.00美元/月 — 获取专业版许可证

Pro许可证密钥激活 generate_report MCP-jvm诊断中的MCP工具。

安装

npx mcp-db-analyzer

或全局安装:

npm install -g mcp-db-analyzer

配置

设置 DATABASE_URL 环境变量:

export DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

或者使用单独的PG变量: PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD.

MySQL

集 DATABASE_URL 使用MySQL连接字符串并传递 --driver mysql:

export DATABASE_URL="mysql://user:password@localhost:3306/mydb"
mcp-db-analyzer --driver mysql

或者使用单个MySQL变量: MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD.

您还可以设置 DB_DRIVER=mysql 作为环境变量,而不是传递标志。

SQLite

通过传递文件路径 DATABASE_URL 并使用 --driver sqlite:

export DATABASE_URL="/path/to/database.db"
mcp-db-analyzer --driver sqlite

克劳德桌面(PostgreSQL)

添加 ~/.claude/claude_desktop_config.json:

{
  "mcpServers": {
    "db-analyzer": {
      "command": "npx",
      "args": ["-y", "mcp-db-analyzer"],
      "env": {
        "DATABASE_URL": "postgresql://user:password@localhost:5432/mydb"
      }
    }
  }
}

克劳德桌面(MySQL)

{
  "mcpServers": {
    "db-analyzer": {
      "command": "npx",
      "args": ["-y", "mcp-db-analyzer", "--driver", "mysql"],
      "env": {
        "DATABASE_URL": "mysql://user:password@localhost:3306/mydb"
      }
    }
  }
}

克劳德桌面(SQLite)

{
  "mcpServers": {
    "db-analyzer": {
      "command": "npx",
      "args": ["-y", "mcp-db-analyzer", "--driver", "sqlite"],
      "env": {
        "DATABASE_URL": "/path/to/database.db"
      }
    }
  }
}

快速演示

配置后,在Claude中尝试以下提示:

  1. “显示架构以及表之间的关系” --返回表结构、外键,并标识孤立表
  2. “是否存在查询速度慢或缺少索引的情况?” --按执行时间对慢速查询进行排名,并建议添加索引
  3. “有多少连接处于活动状态?是否有任何查询被阻止?” --显示连接池利用率、事务会话中的空闲和被阻止的查询

工具

inspect_schema

列出所有具有行数和大小的表,或深入特定表的列、类型、约束和外键。

参数:

  • table (可选)--要检查的表名。省略列出所有表。
  • schema (默认值: "public")--数据库架构。
> inspect_schema

## Tables in schema 'public'

| Table       | Rows (est.) | Total Size |
|-------------|-------------|------------|
| users       | 12,450      | 3.2 MB     |
| orders      | 89,100      | 18.4 MB    |
| order_items | 245,000     | 12.1 MB    |
> inspect_schema table="users"

## Table: public.users

- **Rows (est.)**: 12,450
- **Total size**: 3.2 MB

### Columns
| # | Column | Type          | Nullable | Default |
|---|--------|---------------|----------|---------|
| 1 | id     | integer       | NO       | nextval |
| 2 | email  | varchar(255)  | NO       | -       |
| 3 | name   | varchar(100)  | YES      | -       |

analyze_indexes

查找浪费磁盘空间的未使用索引和导致顺序扫描缓慢的缺失索引。还检测未索引的外键。

参数:

  • schema (默认值: "public")--数据库架构。
  • mode ("usage" | "missing" | "all",默认值: "all")——分析模式。
> analyze_indexes

### Unused Indexes (2 found)
| Table | Index              | Size   | Definition                    |
|-------|--------------------|--------|-------------------------------|
| users | idx_users_legacy   | 1.2 MB | CREATE INDEX ... (old_col)    |

### Unindexed Foreign Keys (1 found)
| Table       | Column  | FK →   | Constraint        |
|-------------|---------|--------|-------------------|
| order_items | user_id | users  | fk_items_user_id  |

explain_query

对SQL查询运行EXPLAIN,并获取一个包含成本估算、节点类型和优化警告的格式化执行计划。可选择运行EXPLAIN ANALYZE进行实际计时(仅限SELECT查询)。

参数:

  • sql --要解释的SQL查询。
  • analyze (默认值: false)--运行EXPLAIN ANALYZE(执行查询;仅限SELECT)。
> explain_query sql="SELECT * FROM orders WHERE status = 'pending'"

## Query Plan Analysis

- **Estimated Total Cost**: 1234.56
- **Estimated Rows**: 500

### Plan Tree
→ Seq Scan on orders (cost=0..1234.56 rows=500)
  Filter: (status = 'pending')

### Potential Issues
- **Sequential Scan** on `orders` (~500 rows). Consider adding an index.

analyze_table_bloat

通过检查死元组比率、真空历史和表大小来分析表膨胀。建议对死元组超过10%的表使用VACUUM ANALYZE。

参数:

  • schema (默认值: "public")--数据库架构。
> analyze_table_bloat

### Tables Needing VACUUM (1 found)
| Table     | Live Tuples | Dead Tuples | Bloat % | Size  | Last Vacuum |
|-----------|-------------|-------------|---------|-------|-------------|
| audit_log | 8,000       | 2,000       | 20.0%   | 10 MB | Never       |

### Recommended Actions
VACUUM ANALYZE public.audit_log;

suggest_missing_indexes

查找顺序扫描计数高、索引使用率为零、与浪费空间的未使用索引交叉引用的表。提供可操作的CREATE INDEX和DROP INDEX建议。

参数:

  • schema (默认值: "public")--数据库架构。
> suggest_missing_indexes

### Tables Missing Indexes (1 found)
| Table  | Seq Scans | Index Scans | Rows   | Size  |
|--------|-----------|-------------|--------|-------|
| events | 5,000     | 0           | 50,000 | 25 MB |

### Unused Indexes (1 found)
| Table | Index            | Size | Definition                       |
|-------|------------------|------|----------------------------------|
| users | idx_users_legacy | 8 kB | CREATE INDEX ... (legacy_col)    |

DROP INDEX public.idx_users_legacy;

analyze_slow_queries

使用查找最慢的查询 pg_stat_statements (PostgreSQL)或 performance_schema (MySQL)。显示执行时间、调用计数并标识优化候选者。

参数:

  • schema (默认值: "public")--数据库架构。
  • limit (默认值: 10)--要返回的慢速查询数。
> analyze_slow_queries

## Slow Query Analysis (by avg execution time)

| # | Avg Time | Total Time | Calls | Avg Rows | Query |
|---|----------|------------|-------|----------|-------|
| 1 | 150.0ms  | 750000ms   | 5000  | 5        | `SELECT * FROM orders WHERE status = $1` |
| 2 | 200.0ms  | 40000ms    | 200   | 2        | `SELECT u.* FROM users u JOIN orders o...` |

### Recommendations
- **2 high-impact queries** — called >100 times with >100ms avg
- **2 queries returning few rows but slow** — likely missing indexes

analyze_connections

分析活动数据库连接。检测事务会话中的空闲、长时间运行的查询、锁争用和连接池利用率。仅限PostgreSQL和MySQL。

> analyze_connections

## Connection Analysis (PostgreSQL)

### Connection States
| State | Count |
|-------|-------|
| active | 3 |
| idle | 12 |
| idle in transaction | 2 |
| **Total** | **17** |

**Max connections**: 100
**Utilization**: 17.0%

### Idle-in-Transaction Connections
| PID  | User | Duration | Query |
|------|------|----------|-------|
| 1234 | app  | 00:05:30 | UPDATE orders SET status = $1 |

analyze_table_relationships

分析表之间的外键关系。构建一个依赖关系图,显示实体连接、孤立表(无FK)、级联删除链和中心实体。

参数:

  • schema (默认值: "public")--数据库架构。
> analyze_table_relationships

## Table Relationships

**Tables**: 5
**Foreign Keys**: 4

### Entity Connectivity
| Table | Incoming FKs | Outgoing FKs | Total |
|-------|-------------|-------------|-------|
| users **hub** | 5 | 0 | 5 |
| orders | 1 | 2 | 3 |

### Orphan Tables (no FK relationships)
- `audit_log`

### Cascading Delete Chains
- **users** → cascades to: orders, addresses
  - **orders** → further cascades to: order_items

analyze_vacuum

分析PostgreSQL VACUUM维护状态。检查死元组比率、真空老化、自动真空配置,并确定需要手动真空的表。 仅限PostgreSQL。

> analyze_vacuum

检测:

  • 具有高死元组比率的表(>10%警告,>20%严重)
  • 桌子从未抽过真空或进行过分析
  • 全球自动真空禁用
  • 自动真空配置问题

输出包括:

  • 按严重程度分组的发现(严重/警告/信息)
  • 需要带死元组百分比的VACUUM的表
  • 每张桌子的完整真空历史记录
  • 自动真空配置设置

安全

  • 默认情况下,所有查询都包装在只读事务中
  • EXPLAIN ANALYZE 仅限于 SELECT 仅限查询
  • 在ANALYZE模式下,DDL/DML语句被拒绝
  • 不允许进行数据修改查询

贡献

  1. 克隆仓库
  2. npm install
  3. npm run build --TypeScript编译
  4. npm test --运行单元测试(vitest)
  5. npm run dev --开发观看模式

限制和已知问题

  • 只读:所有查询都使用只读连接。无法修改数据或架构。
  • 需要pg_stat_语句:PostgreSQL上的慢速查询分析需要 pg_stat_statements 要安装和加载的扩展。
  • MySQL性能模式:索引使用和扫描统计需要 performance_schema 启用(在某些MySQL安装中默认为关闭)。
  • SQLite:没有可用的索引使用统计数据(SQLite不跟踪此数据)。SQLite不支持顺序扫描分析和慢速查询检测。
  • 大型数据库:对具有500多个表的数据库进行模式检查可能会产生非常长的输出。使用 schema 参数以限制范围。
  • 表名参数化:SQLite PRAGMA语句对表名使用字符串插值(SQLite不支持参数化PRAGMA)。表名来源于 sqlite_master 系统表。
  • 跨数据库查询:无法分析跨越多个数据库或使用数据库链接的查询。
  • 估计行数:MySQL TABLE_ROWS 在 information_schema 这是一个估计,不准确。
  • 架构范围:所有工具默认为 public 模式。非公共模式需要明确的规范。多模式分析需要为每个模式分别运行工具。
  • 连接分析: analyze_connections 仅支持PostgreSQL或MySQL。不适用于SQLite数据库。
  • 真空分析: analyze_vacuum 只有PostgreSQL。对于MySQL,使用 OPTIMIZE TABLE 或 analyze_table_bloat.

MCP Java后端套件的一部分

许可证

麻省理工学院


生命终结:2026-05-10。

此MCP服务器不再维护或分发。公司 已经转向Apify市场参与者。看 irrationalways 关于Apify 和 irrcorp/bzp-poland-tenders 对于当前的公司工作。

npm包尚未发布。存储库已存档 仅供历史参考。