简体   繁体   English

实体框架查询过滤器实现最佳性能

[英]Entity Framework Query filters implementation for best perfomance

As the title says, I´m using entity framework 4.0 for a financial application. 如标题所述,我正在将实体框架4.0用于金融应用程序。 I have a winform where I list all the cheques (checks) I have. 我有一个winform,其中列出了我拥有的所有支票(支票)。 But in that form, the user can specify some filters. 但是以这种形式,用户可以指定一些过滤器。

在此处输入图片说明

If the user does not apply any filter, I just can make the query like this: 如果用户不应用任何过滤器,我可以进行如下查询:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").ToList();
datagridview.Datasource = lista_cheques;

That is simple. 那很简单。 But when it applies filters, the problem gets bigger. 但是,当应用过滤器时,问题会变得更大。

As you see, the user can use filter to see cheques (checks) of a specific client, dates, bank, CUIT number, check state, etc. 如您所见,用户可以使用过滤器查看特定客户的支票(支票),日期,银行,CUIT号,支票状态等。

Now, my question is related to performance in the queries. 现在,我的问题与查询的性能有关。

I was thinking on doing the filters separeted, like this: 我在考虑分离过滤器,如下所示:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha).ToList();
lista_cheques = lista_cheques.Where(x => x.banco.id_banco = banco).ToList();
lista_cheques = lista_cheques.Where(x => x.Operacion.Cliente.id_cliente = id_cliente).ToList();

Translation: fecha is date Operacion is a group of checks Cliente is client. 翻译:fecha是日期Operacion是一组检查Cliente是客户端。

In this way, I´m doing a query, then, a query from that query result, then a new query from that new result and that goes on. 通过这种方式,我正在执行查询,然后根据该查询结果进行查询,然后根据该新结果进行新查询,然后继续进行。

I think this way might have a big performance issues. 我认为这种方式可能会有很大的性能问题。 I know that SQL server optimize queries. 我知道SQL Server优化查询。 So, if I´m doing fragmented queries, the optimizer is not working properly. 因此,如果我正在执行零散查询,那么优化器将无法正常工作。

The other way I thought about but it´s very tedious, is to create one big query to handle every possible filter selection. 我想到的另一种方法却很繁琐,那就是创建一个大查询来处理所有可能的过滤器选择。

For example, the other example would be like this: 例如,另一个示例将如下所示:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha && x.banco.id_banco = banco && x.Operacion.Cliente.id_cliente = id_cliente).ToList();

The big problem is that I will need lots of combinations to be able to handle all the filter posibilities. 最大的问题是,我将需要大量组合才能处理所有过滤条件。

Ok guys, now, will I have performace issues in the first code example? 好的,现在,我在第一个代码示例中会遇到性能问题吗? I doing there one big query to the database, and then I´m doing the query in the list of objects (which I think will be faster). 我在那里对数据库做一个大查询,然后在对象列表中进行查询(我认为这样会更快)。 I´m pretty new to this ORM, and this listing will have to handle a lot of registries.. 我对这个ORM来说还很陌生,这个清单必须要处理很多注册表。

I somebody can give me some advice? 我有人可以给我一些建议吗? I made pretty much a mess explaining, hope you can understand.. 我讲得一团糟,希望您能理解。

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha).ToList();
lista_cheques = lista_cheques.Where(x => x.banco.id_banco = banco).ToList();
lista_cheques = lista_cheques.Where(x => x.Operacion.Cliente.id_cliente = id_cliente).ToList();

Nearly perfect. 几乎完美。 Kill all those ToList and it is good. 杀死所有那些ToList,这很好。

The ToList means SQL is evaluated, so if all 3 flters trigger, 2 and 3 are evaluated in memory. ToList表示对SQL进行了评估,因此,如果所有3个flter触发器均被评估,则将在内存中评估2和3。

Kick the ToList away, and the different Where clauses get combined on the database. 踢掉ToList,然后将不同的Where子句合并到数据库中。

Standard LINQ 101. Works like a charm and is always nice to see. 标准LINQ101。工作就像一个吊饰,总是很高兴看到。

Then add as LAST line: 然后添加为LAST行:

lista_cheques = lista_cheques.ToList (); lista_cheques = lista_cheques.ToList();

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

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