1

您好我使用 MySQL Connector/Net 6.3.5 和 Entity Framework 4.1。

在数据库中,我有大约 320 000 行的表,我按列进行简单查询。

我对性能有一点问题。它存在一些如何加快查询速度的技术或方法?

查询超时时间约为 6.3 秒,服务器不在本地。

这是我使用的方法:

/// <summary>
/// select podla kluca
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public TEntity SelectByKey(string key)
{
    // First we define the parameter that we are going to use the clause.
    var xParam = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name);
    MemberExpression leftExpr = Expression.Property(xParam, KeyProperty);
    Expression rightExpr = Expression.Constant(key);
    BinaryExpression binaryExpr = Expression.Equal(leftExpr, rightExpr);

    //Create Lambda Expression for the selection
    Expression<Func<TEntity, bool>> lambdaExpr = Expression.Lambda<Func<TEntity, bool>>
        (binaryExpr, new ParameterExpression[] { xParam });

    //Searching ....
    IList<TEntity> resultCollection = ((IRepository<TEntity, TCtx>)this).SelectAll(new Specification<TEntity>(lambdaExpr));

    if (null != resultCollection && resultCollection.Count() > 0)
    {
        //return valid single result
        return resultCollection.First();
    }
    return null;
}
4

1 回答 1

0

如何替换你的最后几行(对不起,我没有测试过),但只是从我的头上。

IList<TEntity> resultCollection = ((IRepository<TEntity, TCtx>)this).SelectAll(new Specification<TEntity>(lambdaExpr));
((IRepository<TEntity, TCtx>)this)
if (null != resultCollection && resultCollection.Count() > 0)
{
   //return valid single result 
   return resultCollection.First();
}
return null;

// with following lines

foreach(TEntity entity in ((IRepository<TEntity, TCtx>)this).SelectAll(new Specification<TEntity>(lambdaExpr)))
    return entity;
return null;
于 2011-10-27T08:28:02.350 回答