DevExtreme.AspNet.Data icon indicating copy to clipboard operation
DevExtreme.AspNet.Data copied to clipboard

Use variables instead of literals when building the "WHERE" clause

Open artem-kurchenko opened this issue 3 years ago • 1 comments

Databases can skip the SQL parsing as well as the creation of the execution plan when bind variables are used and the same query (with different values) was already executed (what is the typical case for a grid SQL). This should make the query execution faster

See the following SC thread for details: T974081

artem-kurchenko avatar Mar 04 '21 13:03 artem-kurchenko

For reference we had the same problem in our code-base for a long while. In our app, since EntityFramework caches every expression (so that it doesn't have to re-translate to sql) it also caused memory usage to increase over time.

The root issue for use was the usage of Expression.Constant for the object value, changing to () => value causes EF to parameterize the query properly.

Expression causing non-parameterized queries: var equalsExpression = Expression.Equal(property, Expression.Constant(kvp.Value));

Expression producing a parameterized query (EF CORE): Expression<Func<T>> vs = () => (T)value;
var equalsExpression = Expression.Equal(property, valueExpression);

houbi56 avatar May 16 '23 12:05 houbi56