c# - How to improve Linq-To-Sql code -
using stackexchange.profiling.miniprofiler
class profile asp.net mvc application linq-to-sql orm.
i'm trying reduce 1 action 1 sql, don't have duplicates anymore. changed linq-to-sql code accordingly, didn't have positive effect on speed.
then checked time needed sql.
this shows miniprofiler:
when fire exact same sql in management studio super fast:
here code:
from t in type let tdoc = (from d in context.documents d.key == t.no && d.rtype == (int)rtype.art && d.atype == (int)atype.doc select d).firstordefault(d => d.useforthumb) select new time { id = t.id, //... more simple mappings here // complex one: docscount = context.documents.count(d => (d.key == t.id.tostring() && d.rtype == (int)rtype.type && d.atype == (int)atype.doc) || (d.key == t.no && d.rtype == (int)rtype.art && d.atype == (int)atype.doc)), // , 1 thumbid = (tdoc != null && tdoc.frkey.hasvalue) ? tdoc.frkey.value : 0 };
what can reason huge difference? - edit: there no difference, misenterpreted ssms :(
anyway, problem persits. change make faster?
i read sometime mapping linq-to-sql has performance problem. there way workaround this?
i did trial , error , changed linq-to-sql code this:
from t in types let docs = context.documents.where(d => (d.rkey == t.id.tostring() && d.rtype == (int)rtype.type && d.atype == (int)atype.doc) || (d.rkey == t.no && d.rtype == (int)rtype.art && d.atype == (int)atype.doc)) let tdoc = docs.firstordefault(d => d.rtype == (int)rtype.art && d.useforthumb) let docscount = docs.count() select new time { id = t.id, //... more simple mappings here docscount = docscount, thumbid = (tdoc != null && tdoc.frkey.hasvalue) ? tdoc.frkey.value : 0, }
this made query much, faster.