We recently started using completed queries to improve the performance of our Linq to SQL tuning. There are several queries that always take a few seconds to run for the first time in a second in subsequent runs. This is because compilation is not actually performed until the call is made for the first time.
Is there an easy way to make this compilation happen during program compilation, or at least during startup?
EDIT: Therefore, from the comments I see, it looks like the linq queries are definitely not compiled until the call is made. Now I write my queries as follows:
static readonly Func<DataContext, int, IQueryable<Item>> getByPLUComp =
CompiledQuery.Compile<DataContext, int, IQueryable<Item>>((db, PLU) =>
from i in db.Items
where i.IntPLU == PLU && i.Terminated == null
select i);
I have a bunch of these static readonly Funcs floating around. Is there a simple way that I can run my program and initialize it at startup so that the costs are incurred there, and not with regular use?
EDIT 2: One last attempt before I give up on this issue. To fix this problem, I just added calls to compiled requests during the initialization of my program. For instance:
public void Initialize()
{
DataContext db = new DataContext();
getByPLUComp(db, 0);
}
Is there a more elegant way to force compilation without querying and discarding the results?
source
share