Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update等操作允许按需返回受影响的行数据 #1681

Closed
LeaFrock opened this issue Dec 9, 2023 · 1 comment
Closed

Update等操作允许按需返回受影响的行数据 #1681

LeaFrock opened this issue Dec 9, 2023 · 1 comment

Comments

@LeaFrock
Copy link

LeaFrock commented Dec 9, 2023

Feature 特性

主流数据库都支持返回C/U/D操作后影响的行数据:SqlServer-OUTPUT,MySql/PosgreSQL-RETURNING等等。

为Update等操作允许按需返回受影响的行数据,可以最大化利用数据库性能。

例如IUpdate下新增:

Task<List<T1>> ExecuteUpdatedAsync<TMember>(Expression<Func<T1, TMember>> returnCols, CancellationToken cancellationToken = default);

// 或者至少返回主键
Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default);

简要描述原因

现有API在某些场景下不能生成更好的SQL,以有效降低数据库端压力。

假设有1张列数很多的表,以及1个Web API。后者允许传入表主键ID的集合,进行业务上的批量更新操作(如软删除);操作结束后需要根据受影响的行数据进行后续操作,如清理相关缓存资源、消息通知等。

存在以下情形:

1、若使用ExecuteAffrowsAsync,默认所有传入的主键ID都受影响,则在很多情况下会导致后续操作的浪费。假设上游传入的主键不能保证只传入1次,某一批传入500个主键,但只有10个是之前未传入过的,那么在操作结束后只能知道影响了10行,却不得不对下游继续发起500个主键的相关清理或通知操作。

2、若使用ExecuteUpdatedAsync,则返回所有受影响行的所有列数据,这存在极大的IO浪费。若表中某一列比较特殊,存有较大体积的文本或二进制内容,则对系统性能的影响更严重。

使用场景

var updatedUser = freeSql.Update<User>()
    .Where(u => ids.Contains(u.Id))
    .Set(u => u.IsDeleted, true)
    .ExecuteUpdatedAsync(u => new { u.Id, u.Name });
@LeaFrock
Copy link
Author

已使用3.2.806,目前测试环境功能正常。叶老板给力!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant