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

Iterative Include SQL Simplification #4699

Merged
merged 6 commits into from
Nov 19, 2024

Conversation

LTA-Thinking
Copy link
Collaborator

@LTA-Thinking LTA-Thinking commented Oct 24, 2024

Description

Simplifies the SQL query generated when lots of iterative include statements are added to a query.

Related issues

Addresses AB#130995, AB#131246

Testing

Tested the results returned before and after including the simplification on a known data set.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch|Skip|Feature|Breaking (reason)

@LTA-Thinking LTA-Thinking added Enhancement Enhancement on existing functionality. Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs labels Oct 24, 2024
@LTA-Thinking LTA-Thinking added this to the S152 milestone Oct 24, 2024
@LTA-Thinking LTA-Thinking requested a review from a team as a code owner October 24, 2024 19:28
@LTA-Thinking
Copy link
Collaborator Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -15,6 +15,161 @@ namespace Microsoft.Health.Fhir.SqlServer.Features.Search
{
internal static class SqlCommandSimplifier
{
private static readonly Regex FindCteMatch = new Regex(",cte(\\d+) AS\\s*\\r\\n\\s*\\(\\s*\\r\\n\\s*SELECT DISTINCT refTarget.ResourceTypeId AS T1, refTarget.ResourceSurrogateId AS Sid1, 0 AS IsMatch\\s*\\r\\n\\s*FROM dbo.ReferenceSearchParam refSource\\s*\\r\\n\\s*JOIN dbo.Resource refTarget ON refSource.ReferenceResourceTypeId = refTarget.ResourceTypeId AND refSource.ReferenceResourceId = refTarget.ResourceId\\s*\\r\\n\\s*WHERE refSource.SearchParamId = (\\d*)\\s*\\r\\n\\s*AND refTarget.IsHistory = 0\\s*\\r\\n\\s*AND refTarget.IsDeleted = 0\\s*\\r\\n\\s*AND refSource.ResourceTypeId IN \\((\\d*)\\)\\s*\\r\\n\\s*AND EXISTS \\(SELECT \\* FROM cte(\\d+) WHERE refSource.ResourceTypeId = T1 AND refSource.ResourceSurrogateId = Sid1");

private const string RemoveCteMatchBase = "(\\s*,cte<CteNumber> AS\\s*\\r\\n\\s*\\(\\s*\\r\\n\\s*SELECT DISTINCT refTarget.ResourceTypeId AS T1, refTarget.ResourceSurrogateId AS Sid1, 0 AS IsMatch\\s*\\r\\n\\s*FROM dbo.ReferenceSearchParam refSource\\s*\\r\\n\\s*JOIN dbo.Resource refTarget ON refSource.ReferenceResourceTypeId = refTarget.ResourceTypeId AND refSource.ReferenceResourceId = refTarget.ResourceId\\s*\\r\\n\\s*WHERE refSource.SearchParamId = <SearchParamId>\\s*\\r\\n\\s*AND refTarget.IsHistory = 0\\s*\\r\\n\\s*AND refTarget.IsDeleted = 0\\s*\\r\\n\\s*AND refSource.ResourceTypeId IN \\(<ResourceTypeId>\\)\\s*\\r\\n\\s*AND EXISTS \\(SELECT \\* FROM cte<SourceCte> WHERE refSource.ResourceTypeId = T1 AND refSource.ResourceSurrogateId = Sid1.*\\r\\n\\s*\\)\\s*\\r\\n\\s*,cte<CteNextNumber> AS\\s*\\r\\n\\s*\\(\\s*\\r\\n\\s*SELECT DISTINCT .*T1, Sid1, IsMatch, .* AS IsPartial\\s*\\r\\n\\s*FROM cte<CteNumber>\\s*\\r\\n\\s*\\))";
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure this is simpler than fixing the actual generator? Should this be a rewriter instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can take another look. Changing the generator looked more complicated.

mikaelweave
mikaelweave previously approved these changes Nov 19, 2024
@LTA-Thinking LTA-Thinking merged commit 0adbfa9 into main Nov 19, 2024
47 checks passed
@LTA-Thinking LTA-Thinking deleted the personal/rojo/include-sql-simplification branch November 19, 2024 17:45
StringBuilder.Append("TOP (").Append(Parameters.AddParameter(context.IncludeCount, true)).Append(") ");

StringBuilder.Append("T1, Sid1, IsMatch, ");
StringBuilder.Append("CASE WHEN count_big(*) over() > ")
Copy link
Member

Choose a reason for hiding this comment

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

Did the missing count for includes get resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Enhancement Enhancement on existing functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants