Skip to content

Adding EF.Constant to prevent parameterization in query #31552

@Liero

Description

@Liero

What problem are you trying to solve?

"Query is fast in SSMS but slow in production" kind of performance issues, where query is parametrized by EF and SQL Server chooses query execution plan suboptimal for concrete parameter value.

For example:

SELECT TOP(1000) * FROM dbo.MyView  -- fast
int pageSize = 60
if (loadMore) pageSize = 1000;
ctx.MyView.Take(pageSize).ToArray();  //slow when pageSize is 1000

Describe the solution you'd like

Introduce an EF.Constant() mechanism, which is identified by EF Core and prevents parameterization.
This goes hand in hand with #28151

ctx.MyView.Take(() => EF.Contant(pageSize)).ToArray();
ctx.MyView.Where(b => b.Name == EF.Constant("bar"))

Alternatively, add support for query hints like RECOMPILE or OPTIMIZE FOR
/cc @dsyme @NinoFloris @roji

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions