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

Optimize SQL based on parameter nullability #18491

Merged
merged 1 commit into from
Oct 22, 2019
Merged

Optimize SQL based on parameter nullability #18491

merged 1 commit into from
Oct 22, 2019

Conversation

maumar
Copy link
Contributor

@maumar maumar commented Oct 21, 2019

This is part of #17543 - Queries really slow due to null checks

Problem was that at the time we performed null semantics and related optimizations we didn't know the value of parameters, so we always had to assume they can be nullable, which resulted in additional IS NULL checks being created.
However, later in the pipeline we know the parameter values and we can optimize out those checks for parameters whose values are not null.

@maumar
Copy link
Contributor Author

maumar commented Oct 21, 2019

depends on #18035


protected override Expression VisitExtension(Expression extensionExpression)
{
if (extensionExpression is SelectExpression selectExpression)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary workaround, correct fix tracked here: #18492

var newOperand = (SqlExpression)Visit(sqlUnaryExpression.Operand);
if (newOperand is SqlParameterExpression parameterOperand)
{
var parameterValue = _parametersValues[parameterOperand.Name];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use TryGetValue?

@AndriySvyryd ?

Copy link
Member

@AndriySvyryd AndriySvyryd Oct 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to get this far for a query with a parameter that doesn't have a value?
If yes then use TryGetValue and throw a nice exception and add a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do it in a different PR, we have a few places where we directly access parameterValues elements, without TryGetValue - will update all of them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filed #18502

This is part of #17543 - Queries really slow due to null checks

Problem was that at the time we performed null semantics and related optimizations we didn't know the value of parameters, so we always had to assume they can be nullable, which resulted in additional IS NULL checks being created.
However, later in the pipeline we know the parameter values and we can optimize out those checks for parameters whose values are not null.
@maumar maumar merged commit 069d334 into release/3.1 Oct 22, 2019
@maumar maumar deleted the fix17543 branch October 22, 2019 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants