🚀 优化索引重建和添加系统监控

🔧 索引重建优化:
- 当数据库为空时跳过索引重建,避免频繁日志
- 只在有数据的页面时记录详细扫描日志
- 添加记录计数统计,显示重建的记录数量
- 减少 CPU 使用率,提高空数据库性能

💻 系统监控功能:
- 添加完整的 CPU 和内存监控
- 实时显示 Goroutine 数量和内存使用情况
- 垃圾回收统计和对象分配监控
- 每10秒显示系统资源状态

📊 自动处理示例增强:
- 集成系统资源监控到示例中
- 提供性能分析和资源优化指导
- 完善的监控文档和使用说明

🎯 性能提升:
- 解决空数据库时的高 CPU 使用问题
- 优化日志输出频率和级别
- 提供实时性能监控能力
This commit is contained in:
Pipeline Database
2025-09-30 17:48:28 +08:00
parent 9b91d16192
commit 6f78cdd8a9
6 changed files with 445 additions and 2 deletions

View File

@@ -1393,11 +1393,18 @@ func (pdb *PipelineDB) performCooldown(record *DataRecord) error {
// rebuildIndexes 重建所有索引(用于数据库重启后恢复)
func (pdb *PipelineDB) rebuildIndexes() error {
// 如果没有根页面或总页数为0跳过重建
if pdb.header.RootPage == 0 || pdb.header.TotalPages <= 1 {
pdb.logger.Info("🔄 数据库为空,跳过索引重建")
return nil
}
pdb.logger.Info("🔄 重建索引...")
// 遍历所有数据页重建索引
pdb.logger.Info("📊 开始扫描页面", "totalPages", pdb.header.TotalPages, "rootPage", pdb.header.RootPage)
recordCount := 0
// 从根页面开始扫描页链
for pageNo := pdb.header.RootPage; pageNo != 0 && pageNo < pdb.header.TotalPages; {
// 读取页面
@@ -1417,9 +1424,13 @@ func (pdb *PipelineDB) rebuildIndexes() error {
numSlots := binary.LittleEndian.Uint16(page[0:2]) // 正确的槽数量
freeOff := binary.LittleEndian.Uint16(page[2:4])
nextPage := binary.LittleEndian.Uint16(page[4:6])
pdb.logger.Info("📄 扫描页面", "pageNo", pageNo, "numSlots", numSlots, "freeOff", freeOff, "nextPage", nextPage)
// 只在有数据时记录详细日志
if numSlots > 0 {
pdb.logger.Info("📄 扫描页面", "pageNo", pageNo, "numSlots", numSlots, "freeOff", freeOff, "nextPage", nextPage)
}
if numSlots == 0 {
pageNo = nextPage
continue
}
@@ -1463,6 +1474,7 @@ func (pdb *PipelineDB) rebuildIndexes() error {
// 添加到索引
idx := pdb.indexMgr.GetOrCreateIndex(record.Group)
idx.Insert(int64(id), pageNo, slotNo)
recordCount++
}
// 移动到下一个页面
@@ -1473,6 +1485,6 @@ func (pdb *PipelineDB) rebuildIndexes() error {
pageNo = nextPageNo
}
pdb.logger.Info("✅ 索引重建完成")
pdb.logger.Info("✅ 索引重建完成", "recordCount", recordCount)
return nil
}