Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架4.1和数据库(MySQL)性能问题{当表包含320 000行时,如何加快查询速度以返回第一条记录}_C#_Mysql_Entity Framework 4_Database Performance - Fatal编程技术网

C# 实体框架4.1和数据库(MySQL)性能问题{当表包含320 000行时,如何加快查询速度以返回第一条记录}

C# 实体框架4.1和数据库(MySQL)性能问题{当表包含320 000行时,如何加快查询速度以返回第一条记录},c#,mysql,entity-framework-4,database-performance,C#,Mysql,Entity Framework 4,Database Performance,您好,我使用MySQL Connector/Net 6.3.5和Entity Framework 4.1 在DB中,我有一个大约32万行的表,我按列进行简单的查询 我的表现有点问题。它存在一些技术或方法如何加速查询 查询超时约为6.3秒,服务器不是本地的 以下是我使用的方法: /// <summary> /// select podla kluca /// </summary> /// <param name="key"></param> ///

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

在DB中,我有一个大约32万行的表,我按列进行简单的查询

我的表现有点问题。它存在一些技术或方法如何加速查询

查询超时约为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;
}
//
///选择podla kluca
/// 
/// 
/// 
public TEntity SelectByKey(字符串键)
{
//首先,我们定义要使用子句的参数。
var xParam=Expression.Parameter(typeof(tenty)、typeof(tenty).Name);
MemberExpression leftExpr=Expression.Property(xParam,KeyProperty);
表达式rightExpr=表达式常数(键);
BinaryExpression binaryExpr=Expression.Equal(leftExpr,rightExpr);
//为所选内容创建Lambda表达式
表达式lambdaExpr=Expression.Lambda
(binaryExpr,新参数表达式[]{xParam});
//搜索。。。。
IList resultCollection=((IRepository)this).SelectAll(新规范(lambdaExpr));
if(null!=resultCollection&&resultCollection.Count()>0)
{
//返回有效的单个结果
返回resultCollection.First();
}
返回null;
}

换掉最后几行怎么样(对不起,我没有测试过),只是从头开始

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;
IList resultCollection=((IRepository)this)。选择All(新规范(lambdaExpr));
((我推测)这个)
if(null!=resultCollection&&resultCollection.Count()>0)
{
//返回有效的单个结果
返回resultCollection.First();
}
返回null;
//有以下几行
foreach(在((i假定)this中的TEntity实体)。选择all(新规范(lambdaExpr)))
返回实体;
返回null;

您好,首先感谢您的回复,我将测试您的解决方案…它现在正在工作,我需要比较性能。