feat(migration): 添加 flow_run_logs 复合索引并修复 flow_id 类型

优化 flow_run_logs 查询性能,添加常用排序和过滤的复合索引
将 flow_id 从 VARCHAR(64) 改为 BIGINT 以匹配实体模型
在分页查询中实现末页优化策略
This commit is contained in:
2025-09-25 22:38:06 +08:00
parent dfa1cbdd2f
commit a71bbb0961
4 changed files with 208 additions and 13 deletions

View File

@ -25,6 +25,10 @@ mod m20220101_000017_create_flow_run_logs;
mod m20220101_000018_add_flow_code_to_flow_run_logs;
// 新增:计划任务表
mod m20220101_000019_create_schedule_jobs;
// 新增:为 flow_run_logs 创建复合索引
mod m20220101_000020_add_indexes_to_flow_run_logs;
// 修复 flow_run_logs.flow_id 类型为 BIGINT
mod m20220101_000021_alter_flow_run_logs_flow_id_to_bigint;
pub struct Migrator;
@ -57,8 +61,12 @@ impl MigratorTrait for Migrator {
Box::new(m20220101_000017_create_flow_run_logs::Migration),
// 新增:为 flow_run_logs 添加 flow_code 列
Box::new(m20220101_000018_add_flow_code_to_flow_run_logs::Migration),
// 新增:计划任务表
// 新增:计划任务表(恢复注册)
Box::new(m20220101_000019_create_schedule_jobs::Migration),
// 修复 flow_run_logs.flow_id 类型为 BIGINT
Box::new(m20220101_000021_alter_flow_run_logs_flow_id_to_bigint::Migration),
// 新增:为 flow_run_logs 创建复合索引
Box::new(m20220101_000020_add_indexes_to_flow_run_logs::Migration),
]
}
}

View File

@ -0,0 +1,93 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 覆盖常用排序与过滤的索引
// 1) 无过滤ORDER BY started_at DESC, id DESC 使用 (started_at, id)
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_flow_run_logs_started_at_id")
.table(FlowRunLogs::Table)
.col(FlowRunLogs::StartedAt)
.col(FlowRunLogs::Id)
.to_owned(),
)
.await?;
// 2) flow_id = ? 且 ORDER BY started_at DESC, id DESC 使用 (flow_id, started_at, id)
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_flow_run_logs_flow_id_started_at_id")
.table(FlowRunLogs::Table)
.col(FlowRunLogs::FlowId)
.col(FlowRunLogs::StartedAt)
.col(FlowRunLogs::Id)
.to_owned(),
)
.await?;
// 3) flow_code = ? 且 ORDER BY started_at DESC, id DESC 使用 (flow_code, started_at, id)
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_flow_run_logs_flow_code_started_at_id")
.table(FlowRunLogs::Table)
.col(FlowRunLogs::FlowCode)
.col(FlowRunLogs::StartedAt)
.col(FlowRunLogs::Id)
.to_owned(),
)
.await?;
// 4) ok = ? 且 ORDER BY started_at DESC, id DESC 使用 (ok, started_at, id)
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_flow_run_logs_ok_started_at_id")
.table(FlowRunLogs::Table)
.col(FlowRunLogs::Ok)
.col(FlowRunLogs::StartedAt)
.col(FlowRunLogs::Id)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(Index::drop().name("idx_flow_run_logs_started_at_id").table(FlowRunLogs::Table).to_owned())
.await?;
manager
.drop_index(Index::drop().name("idx_flow_run_logs_flow_id_started_at_id").table(FlowRunLogs::Table).to_owned())
.await?;
manager
.drop_index(Index::drop().name("idx_flow_run_logs_flow_code_started_at_id").table(FlowRunLogs::Table).to_owned())
.await?;
manager
.drop_index(Index::drop().name("idx_flow_run_logs_ok_started_at_id").table(FlowRunLogs::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum FlowRunLogs {
#[sea_orm(iden = "flow_run_logs")]
Table,
Id,
FlowId,
FlowCode,
StartedAt,
Ok,
}

View File

@ -0,0 +1,40 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 将 flow_id 从 VARCHAR(64) 改为 BIGINT以匹配实体模型与查询参数类型
manager
.alter_table(
Table::alter()
.table(FlowRunLogs::Table)
.modify_column(ColumnDef::new(FlowRunLogs::FlowId).big_integer().not_null())
.to_owned(),
)
.await?
;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 回滚:将 flow_id 改回 VARCHAR(64)
manager
.alter_table(
Table::alter()
.table(FlowRunLogs::Table)
.modify_column(ColumnDef::new(FlowRunLogs::FlowId).string_len(64).not_null())
.to_owned(),
)
.await
}
}
#[derive(DeriveIden)]
enum FlowRunLogs {
#[sea_orm(iden = "flow_run_logs")]
Table,
FlowId,
}