-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Named parameter / prepared query support #275
Comments
Thanks @olee for the suggestion. We'll take a look and see if it's possible to utilise query parameters / prepared queries in Linq2Couchbase. |
The first part of the problem is that we need a clear way within LINQ to indicate which values in the predicate are constants versus which will change dynamically between each query. In some cases, we can recognize a pure constant within the query. However, in many cases, we can't. The underlying Relinq infrastructure converts many variable inputs into ConstantExpressions very early in the process as a key optimization. This means that we don't have a clear way to know which fields to place directly on the query, i.e. At first glance, you would then assume we could just parameterize all the constants on the query, assuming that any of them may be changed between query executions, and then prepare the query for reuse. However, this introduces problems related to Couchbase indices. One of the parts of planning a query, including generating a saved query plan for reuse, is selecting the index or indices to use to support the query. Couchbase indices, unlike SQL indices, have predicates attached to the index itself. This predicate filters the documents which are included on the index, i.e. However, if all of the constants are parameterized, then the query planner cannot recognize that the index matches. For example, The only idea I have to address this would be a special method that flags a particular constant as intended to be dynamic, similar to the example in the original message. var data = userCtx.Query<User>()
.Where(x => x.Email == N1Ql.Parameter("test@example.com"))
.Select(u => new
{
FirstName = u.FirstName,
LastName = u.LastName,
}); In this case, the static function would be a passthrough for .NET purposes, but we'd be able to recognize it during query generation: public static T Parameter<T>(T value) => value; Wherever we encounter this method call, we'd apply the constant value to the query as a named parameter. I think we can avoid the need for a special method like I see two downsides to this approach:
|
Hi, would it be possible to integrate support for named / positional parameters, mainly to boost performance by utilizing query plan caching / prepared queries?
I could imagine something like this:
The
QueryParameter
class could be detected in the expression visitors and it would automatically generate a named parameter at its position and add its value to the final query with AddNamedParameter.Additionally the
UsePreparedQuery
extension would mark the generated query to be registered as a prepared query with couchbase to improve performance for future executions.I'm quite new to couchbase but have some knowledge regarding linq expression trees, so I hope this proposal makes sense.
The text was updated successfully, but these errors were encountered: