Csharp/C#教程:由于派生表,entity framework很慢分享


由于派生表,entity framework很慢

我使用MySQL Connector / Net 6.5.4与LINQ实体,我经常得到糟糕的查询性能,因为entity framework生成使用派生表的查询。

这是我多次遇到的简化示例。 在C#中,我写了一个这样的查询:

var culverCustomers = from cs in db.CustomerSummaries where cs.Street == "Culver" select cs; // later... var sortedCustomers = culverCustomers.OrderBy(cs => cs.Name).ToList(); 

而不是像这样生成简单的查询:

 SELECT cust.id FROM customer_summary cust WHERE cust.street = "Culver" ORDER BY cust.name 

entity framework使用派生表生成一个查询,如下所示:

 SELECT Project1.id FROM ( SELECT cust.id, cust.name, cust.street FROM customer_summary cust WHERE Project1.street = "Culver" ) AS Project1 -- here is where the EF generates a pointless derived table ORDER BY Project1.name 

如果我解释两个查询,我会得到第一个查询:

 id, select_type, table, type, possible_keys, rows 1, PRIMARY, addr, ALL, PRIMARY, 9 1, PRIMARY, cust, ref, PRIMARY, 4 

……对于entity framework查询来说,这样的事情很糟糕

 id, select_type, table, type, possible_keys, rows 1, PRIMARY, , ALL, 9639 2, DERIVED, addr, ALL, PRIMARY, 9 2, DERIVED, cust, ref, PRIMARY, 4 

请注意第一行,其中MySQL解释说它正在扫描9000+条记录 。 由于派生表,MySQL正在创建临时表并加载每一行。 (或者我根据这样的文章推断出: 派生表和视图性能 )

如何防止entity framework使用派生表,或者如何说服MySQL对此类查询进行明显的优化?

为了完成,这里是该linq查询的源视图:

 create view customer_summary as select cust.id, cust.name, addr.street customers cust join addresses addr on addr.customer_id = cust.id 

我认为你的查询语句缺少’select’。 您尚未确定所需的记录。 您的查询:

 var culverCustomers = from cs in db.CustomerSummaries where cs.Street == "Culver"; 

//没有选择

你从桌子上选择什么? 试试这个

例:

上述就是C#学习教程:由于派生表,entity framework很慢分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

 var culverCustomers = from cs in db.CustomerSummaries where cs.Street == "Culver" select cs; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/cdevelopment/999693.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐