简体   繁体   English

如何在LINQ中加快查询二进制搜索

[英]how to speed up query binary search in LINQ

I use Linq select statement to get file as byte from the database. 我使用Linq select语句从文件夹中获取文件作为字节。 It take long time to get the byte array from my Files Table. 从我的文件表中获取字节数组需要很长时间。

I'm using this code: 我正在使用此代码:

Fun<Files,bool> Filter  = delegate(Files x)
{
     return x.FileID == 10;
}; 
Files File = DAL.FilesDAL.GetFile(Filter).First();


public static List<Files> GetFile(Func<Files,bool> lambda){
return db.Where(lamnda).ToList();
}

For 1M file size it take up to 1m. 对于1M文件大小,它需要1米。 That too long for my clients. 这对我的客户来说太长了。

How I can improve the speed of that query? 我怎样才能提高查询的速度?

Looks like you're missing the fact, that using Func<> makes your query be executed as LINQ to Objects. 看起来你错过了这个事实,使用Func<>使你的查询作为LINQ to Objects执行。 That means entire table is fetched into application memory and then filtering is performed by the application, not by the database. 这意味着将整个表提取到应用程序内存中,然后由应用程序执行过滤,而不是由数据库执行。 To make the filter execute at database it should be passed as Expression<Func<>> instead: 要使过滤器在数据库中执行,它应该作为Expression<Func<>>传递:

public static List<Files> GetFile(Expression<Func<Files,bool>> lambda){
    return db.Where(lamnda).ToList();
}

I assumed, that db here is IQueryable<Files> . 我假设,这里的dbIQueryable<Files>

To call it use: 要调用它:

Files File = DAL.FilesDAL.GetFile(x => x.Filter == 10).First();

To make it even more descriptive, you should probably change your method to only return one Files item. 为了使其更具描述性,您可能应该将方法更改为仅返回一个“ Files项。 Method name is GetFile , so I would expect it to return one file, not collection of files: 方法名称是GetFile ,所以我希望它返回一个文件,而不是文件集合:

public static Files GetFile(Expression<Func<Files,bool>> lambda){
    return db.FirstOrDefault(lamnda);
}

usage: 用法:

var File = DAL.FilesDAL.GetFile(x => x.Filter == 10);

Or to increase semantics, you can refactor the method to be GetFileById and take Id , not an expression: 或者为了增加语义,您可以将方法重构为GetFileById并获取Id ,而不是表达式:

public static Files GetFileById(int Id)
{
    return db.FirstOrDefault(x => x.FileId == id);
}

usage 用法

var File = DAL.FilesDAL.GetFileById(10);

How I can improve the speed of that query? 我怎样才能提高查询的速度?

Faster server. 更快的服务器。 Faster network. 更快的网络。 Do not get the binary content you do not need. 不要获取您不需要的二进制内容。

SImple like that. 就像这样。 NOthing you can do on LINQ level that will magically fix a bad design. 你可以在LINQ级别上做什么,这将神奇地修复一个糟糕的设计。

Generally: 通常:

Btw., a bad mistake - you return ToList in GetFiles. 顺便说一句,这是一个错误的错误 - 你在GetFiles中返回ToList。 Never do that. 永远不要那样做。 Either let it a queryable, so the user can add conditions and order and grouping or return an enumerable (which is way less powerfull) but ToList means all gets into memory - even if the user then does not want a list. 要么让它成为可查询的,那么用户可以添加条件和顺序,分组或返回一个可枚举的(这样就不那么强大了)但是ToList意味着所有进入内存 - 即使用户不想要一个列表。

And man, the way to hand in the lambdaa is just redundant and limiting. 而男人,交出lambdaa的方式只是多余和限制。 Bad design. 糟糕的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM