简体   繁体   English

慢速实体框架查询,但快速生成SQL

[英]slow entity framework query , but fast Generated SQL

please consider this model 请考虑这个模型 在此处输入图片说明

it's for a fitness center management app 这是一个健身中心管理应用程序

ADHERANT is the members table ADHERANT是成员表

INSCRIPTION is the subscription table INSCRIPTION是订阅表

SEANCE is the individual sessions table SEANCE是个人会话表

the seance table contain very fews rows (around 7000) seance表包含很少的行(大约7000)

now the query : 现在查询:

  var q = from n in ctx.SEANCES
                select new SeanceJournalType()
                        {
                            ID_ADHERANT = n.INSCRIPTION.INS_ID_ADHERANT,
                            ADH_NOM = n.INSCRIPTION.ADHERANT.ADH_NOM,
                            ADH_PRENOM = n.INSCRIPTION.ADHERANT.ADH_PRENOM,
                            ADH_PHOTO = n.INSCRIPTION.ADHERANT.ADH_PHOTO,
                            SEA_DEBUT = n.SEA_DEBUT
                        };

                var h = q.ToList();

this take around 3 seconds wich is an eternity, the same generated SQL query is almost instantaneous 这大约需要3秒钟的时间,而相同的生成SQL查询几乎是瞬时的

SELECT 
1 AS "C1", 
"C"."INS_ID_ADHERANT" AS "INS_ID_ADHERANT", 
"E"."ADH_NOM" AS "ADH_NOM", 
"E"."ADH_PRENOM" AS "ADH_PRENOM", 
"E"."ADH_PHOTO" AS "ADH_PHOTO", 
"B"."SEA_DEBUT" AS "SEA_DEBUT"
FROM   "TMP_SEANCES" AS "B"
LEFT OUTER JOIN "INSCRIPTIONS" AS "C" ON "B"."INS_ID_INSCRIPTION" = "C"."ID_INSCRIPTION"
LEFT OUTER JOIN "ADHERANTS" AS "E" ON "C"."INS_ID_ADHERANT" = "E"."ID_ADHERANT"

any idea on what's going on please, or how to fix that ? 对发生的事情有什么想法,或者如何解决?

thanks 谢谢

it needs some research to optimize this : 它需要一些研究来优化此:

if you neglect the data transfer from the db to the server then as Ivan Stoev Suggested calling the ToList method is the expensive part 如果您忽略了从db到服务器的数据传输,则由于Ivan Stoev建议调用ToList方法是昂贵的部分

as for improving the performance it depends on your needs: 至于提高性能,则取决于您的需求:

1.if you need add-delete functionality at the server side it is probably best to stick with the list 1.如果您需要在服务器端添加或删除功能,最好坚持使用列表

2.if no need for add-delete then consider ICollection ,, or even better 2.如果不需要添加删除,则考虑使用ICollection,甚至更好

3.if you have more conditions which will customize the query even more best use IQuerable 3.如果您有更多条件可以自定义查询,甚至可以更好地使用IQuerable

customizing the query like selecting a single record based on a condition : 自定义查询,例如根据条件选择单个记录:

var q = from n in ctx.SEA.... // your query without ToList()
q.where(x=>"some condition") //let`s say x.Id=1

only one record will be transferred from the database to the server 只有一条记录将从数据库传输到服务器

but with the ToList Conversion all the records will be transferred to the server then the condition will be calculated 但通过ToList Conversion,所有记录将被传输到服务器,然后将计算条件

although it is not always the best to use IQuerable it depends on your business need 尽管使用IQuerable并非总是最好的方法,但这取决于您的业务需求

for more references check this and this 对于更多的参考检查这个这个

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

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