您好,我使用的是MySQL连接器/NET6.3.5和实体框架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;
}发布于 2011-10-27 16:28:02
替换你最后几行代码(对不起,我还没有测试过它),但是从我的头顶开始如何?
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;https://stackoverflow.com/questions/7912678
复制相似问题