Skip to content
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

Feature: Generate Parameterized SQL #184

Closed
sspekinc opened this issue Jul 19, 2018 · 7 comments
Closed

Feature: Generate Parameterized SQL #184

sspekinc opened this issue Jul 19, 2018 · 7 comments
Assignees
Labels

Comments

@sspekinc
Copy link

sspekinc commented Jul 19, 2018

Dynamically generated SQL contains inline constants, which causes SQL Server to compile the query plan every time a new constant is provided for the same query. Compilation is a costly operation and it can be avoided if the generated SQL is parameterized.

Expression.Constant calls can be replaced by a "wrapped" constant as explained in the article below so that the generated SQL is parameterized and the query plan will be cached by the SQL Server.

https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd

@StefH StefH self-assigned this Jul 21, 2018
@StefH StefH added the feature label Jul 21, 2018
@StefH
Copy link
Collaborator

StefH commented Jul 21, 2018

This looks interesting. I'll investigate this.

@sspekinc
Copy link
Author

sspekinc commented Jul 21, 2018

I have modified the class System.Linq.Dynamic.Core.Parser.ExpressionHelper so that it generates parameterized SQL. It's working fine for me so far, but I couldn't run the tests yet so please take it with a grain of salt. Here is the proposed changes below.

#185

@StefH
Copy link
Collaborator

StefH commented Jul 22, 2018

I added some review comments, please take a look.

About the unit-tests, why doesn't this work on your machine?

And this PR did trigger CI build + test, take a look at the 3 fails.

@StefH
Copy link
Collaborator

StefH commented Nov 1, 2018

Closing this : PR has been merged.

@StefH StefH closed this as completed Nov 1, 2018
@BradleyUffner
Copy link

BradleyUffner commented Feb 15, 2019

How would I actually use this feature? I'm using version 1.0.9.2 of the nuget package (released after this PR was merged), and it is still inlining constants instead of using sql parameters.

@BradleyUffner
Copy link

BradleyUffner commented Feb 15, 2019

After more testing, it seems to be a problem with Contains, StartsWith, and EndsWith.

If I use .Where(config, "ActivityCode==@0", "AB") it works as expected, generating the parameterized value, but .Where(config, "ActivityCode.Contains(@0)", "A") still inlines the value.

@BradleyUffner
Copy link

I created a new issue for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants