In my code, I used to load a related entity using await FindAsync
, hoping that i better conform to c# async guidelines.
var activeTemplate = await exec.DbContext
.FormTemplates.FindAsync(exec.Form.ActiveTemplateId);
and it ran slow, was slow in sql server profiler, the query text was fast in SSMS. It took 5 seconds to fetch this line.
The alternative:
var activeTemplate = exec.Form.ActiveTemplate;
is much faster. By all means, the problem does not seem to be parameter sniffing, as the number of reads in the fast and slow queries are the same.
One possibly irrelevant point is that the fetched object contains a string property containing ~1MB text. The application is asp.net mvc, running on the same computer as the sql server, connecting using (local).
What is the cause of the observed slowness?
EDIT: After @jbl's comment, I did some more experiments:
var activeTemplate = await exec.DbContext.FormTemplates
.FirstOrDefaultAsync(x => x.Id == exec.Form.ActiveTemplateId); // slow
var activeTemplate = exec.DbContext.FormTemplates
.FirstOrDefault(x => x.Id == exec.Form.ActiveTemplateId); // fast