C# 使用Linq Any查询或表达式构建性能查询

C# 使用Linq Any查询或表达式构建性能查询,c#,mysql,lambda,entity-framework-6,expression,C#,Mysql,Lambda,Entity Framework 6,Expression,我们是我们项目中的实体框架。需要知道.ANY和表达式之间的性能影响,以形成Where子句 在下面的函数中,我使用了两种方法来获得结果: 方法1-使用任意 根据我的观察,使用.Any并不是在执行查询时添加where子句在sql探查器中检查EF所做的是获取内存中存储的所有匹配的内部连接记录,然后应用.Any中指定的条件 方法2-表单表达式查询开始 通过表达式,我显式地形成where子句并执行。在SQL查询探查器中检查相同的表达式,我可以看到where子句。 注意:为了形成表达式where子句,我正在

我们是我们项目中的实体框架。需要知道.ANY和表达式之间的性能影响,以形成Where子句

在下面的函数中,我使用了两种方法来获得结果:

方法1-使用任意 根据我的观察,使用.Any并不是在执行查询时添加where子句在sql探查器中检查EF所做的是获取内存中存储的所有匹配的内部连接记录,然后应用.Any中指定的条件

方法2-表单表达式查询开始 通过表达式,我显式地形成where子句并执行。在SQL查询探查器中检查相同的表达式,我可以看到where子句。 注意:为了形成表达式where子句,我正在做额外的循环和组合预测

现在,我的疑问是:

哪种方法可以提高性能。我需要和Lambda一起去吗 有什么特别的意思吗

从where子句改进性能的正确方法是什么

如果不是的话,这两种方法建议我正确的方法

private bool GetClientNotifications(int clientId, IList<ClientNotification> clientNotifications)
{
IList<string> clientNotificationList = null;
var clientNotificationsExists = clientNotifications?.Select(x => new { x.Name, x.notificationId 
}).ToList();
if (clientNotificationsExists?.Count > 0)
{

    //**Approach 1 => Form Lamada  Query starts**
    notificationList = this._clientNotificationRepository?.FindBy(x => clientNotificationsExists.Any(x1 => x.notificationId == x1.notificationId && x.clientId == 
  clientId)).Select(x => x.Name).ToList();
    **//Form Lamada  Query Ends**

    //**Approach 2 =>Form Expression Query Starts**
    var filterExpressions = new List<Expression<Func<DbModel.ClientNotification, bool>>>();
    Expression<Func<DbModel.ClientNotification, bool>> predicate = null;
    foreach (var clientNotification in clientNotificationsExists)
    {
        predicate = a => a.clientId == clientId && a.notificationId == clientNotification .notificationId;
        filterExpressions.Add(predicate);
    }
    predicate = filterExpressions.CombinePredicates<DbModel.ClientNotification>(Expression.OrElse);
    clientNotificationList = this._clientNotificationRepository?.FindBy(predicate).Select(x => x.Name).ToList();
    //**Form Expression Query Ends**
}
return clientNotificationList;
  }

如果没有一种方法是好的,请建议我正确的方法。

我注意到两种方法中使用的clientId都是在与clientNotificationsExists链接的条件下使用的,但与它的对象没有实际关系,因此可以将此条件提高一个级别。这可能带来极小的好处,因为如果没有重复的子句,整个sql将更小

从所描述的行为来看,第一种方法筛选器没有被转换为sql,因此它在客户端被解析。但是,如果它可以被转换,那么性能将类似或相同

如果要生成IN sql子句,可以使用数组并在筛选器表达式中使用Contains方法。这可能会有所改善,但不要抱太高的希望

请尝试以下操作:

private bool GetClientNotifications(int clientId, IList<ClientNotification> clientNotifications)
{
    IList<string> clientNotificationList = null;
    var clientNotificationsExists = clientNotifications?
        .Select(x => new { x.Name, x.notificationId }).ToList();
    if (clientNotificationsExists?.Count > 0)
    {
        var notificationIds = clientNotificationsExists.Select(x => x.notificationId).ToArray();
        clientNotificationList = this._clientNotificationRepository?
            .FindBy(x => x.clientId == clientId && notificationIds.Contains(x.notificationId));
    }
    return clientNotificationList;
}

还要比较它生成的原始sql。还要检查数据库在任何情况下是否正确索引,这将是数据库表正确索引的最大瓶颈。在这里,我需要从上述场景中了解提高性能的最佳方法