Why are my compiled linq queries slowing down for the first time?

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?

+3
source share
4 answers

2 SQL:

, "". , , , , , .

+1

, .

0

Sql Cashes data for the first time will improve other runs. You can make a script that is executed, although calls to be cashed. You can add this to the installation process. ,, This may work.

You can create a table that updates the necessary information. That way, when you need readable information. (if there is a lot of processing when combining data)

0
source

Source: https://habr.com/ru/post/1727412/


All Articles