使用EF Core 3.x+Mysql 时,如下EF表达式
var list=await ctx.Set<User>().where(v=>v.Account.Contains("test")).ToListAsync();
生成的语句:
SELECT
`b1`.`id`,
`b1`.`account`,
...
FROM
`basis_user` AS `b1`
WHERE
LOCATE(CONVERT( 'test' USING UTF8MB4) COLLATE utf8mb4_bin,
`b1`.`account`) > 0
其中 LOCATE(CONVERT( 'test' USING UTF8MB4) COLLATE utf8mb4_bin,`b1`.`account`) > 0
是数据库函数操作,数量量大时,会非常慢.
解决办法:拦截EF生成的语句,将上述函数操作,替换成like
伪代码如下:
1:定义拦截器
public class FmtCommandInterceptor : DbCommandInterceptor, IDbCommandInterceptor
{
public static readonly Regex Regex_Replace_MySql_Like
= new Regex(@"LOCATE(CONVERT('(?<v>.+?)' USING utf8mb4) COLLATE utf8mb4_bin, (?<k>`.+`?)) > 0",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
{
command.CommandText = Regex_Replace_MySql_Like.Replace(command.CommandText, " ${k} like '%${v}%' ");
return base.ReaderExecuting(command, eventData, result);
}
public override Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
{
command.CommandText = Regex_Replace_MySql_Like.Replace(command.CommandText, " ${k} like '%${v}%' ");
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}
}
2:注册拦截器
services
.AddDbContextPool<yearDbContext>(o =>
{
o.UseMySql(Configuration.GetConnectionString("youdbConfigName"), mySqlOptions =>
{
mySqlOptions.ServerVersion(new Version(5, 6, 40), ServerType.MySql);
}).AddInterceptors(new FmtCommandInterceptor());
})
有其它场景时,也可以优化上面的正则替换方法,达到替换执行语句的效果
大家如果有其它办法处理这类问题,也麻烦分享一下