-
Notifications
You must be signed in to change notification settings - Fork 566
[Performance] EWMA SQL Query Plan Section #5147
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
Open
fhibf
wants to merge
13
commits into
main
Choose a base branch
from
user/fernfe/sqlQueryPlanEwma
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e1e06d
Commit first changes to support EWMA SQL Query Plan Selection.
fhibf 5755e7c
Changes to support the generation of hashes on top of expressions.
fhibf 88f28ca
Improvements to handle SQL Query Plan.
fhibf 0c511cf
- Improving hashing calculation.
fhibf 6dd3948
Rename method for better usage.
fhibf fd528f8
Adding new header to response.
fhibf 7f0ae2c
Merge branch 'main' into user/fernfe/sqlQueryPlanEwma
fhibf e5154ec
Adding new logging.
fhibf aedf813
Adding hashing logic limiting the size of the hash used by QueryPlanS…
fhibf 0c5ba5f
Setting DynamicSqlQueryPlanSelectionEnabled as false for now.
fhibf 3d33b00
Moving Query configurations to Core Features.
fhibf 42c584c
Merge branch 'main' into user/fernfe/sqlQueryPlanEwma
fhibf 6ed45f0
SQL Query Plan Selector improvements
fhibf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
src/Microsoft.Health.Fhir.Core.UnitTests/Features/Search/Hackathon/QueryPlanSelectorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------------------------------------------- | ||
|
|
||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Health.Fhir.Core.Features.Search.Hackathon; | ||
| using Microsoft.Health.Fhir.Tests.Common; | ||
| using Microsoft.Health.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Health.Fhir.Core.UnitTests.Features.Search.Hackathon | ||
| { | ||
| [Trait(Traits.OwningTeam, OwningTeam.Fhir)] | ||
| [Trait(Traits.Category, Categories.Search)] | ||
| public sealed class QueryPlanSelectorTests | ||
| { | ||
| [Fact] | ||
| public void GivenExecutionTimes_WhereSettingsTRUEHasABetterPerformance_ThenSelectorShouldPreferTrue() | ||
| { | ||
| // This test simulates the case then SQL Query Plan Caching is beneficial. | ||
|
|
||
| QueryPlanSelector selector = new QueryPlanSelector(); | ||
|
|
||
| string hash = "hash1"; | ||
|
|
||
| // The following function simulates the execution time for each setting. | ||
| // For even iterations (setting=true), the execution time is 50ms. | ||
| // That will result in a faster execution, that will be learned by the selector. | ||
| // And the selector will eventually settle on setting=true. | ||
| var getValue = (int index) => index < 6 && index % 2 == 0 ? 100 : 50; | ||
|
|
||
| for (int i = 0; i < 20; i++) | ||
| { | ||
| bool setting = selector.GetQueryPlanCachingSetting(hash); | ||
|
|
||
| selector.ReportExecutionTime(hash, setting, getValue(i)); | ||
| } | ||
|
|
||
| Assert.True(selector.GetQueryPlanCachingSetting(hash), "Based on the logic of this test, the selector should have settled on setting=true."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenExecutionTimes_WhereSettingsFALSEHasABetterPerformance_ThenSelectorShouldPreferTrue() | ||
| { | ||
| // This test simulates the case then SQL Query Plan Caching is not beneficial. | ||
|
|
||
| QueryPlanSelector selector = new QueryPlanSelector(); | ||
|
|
||
| string hash = "hash1"; | ||
|
|
||
| // The following function simulates the execution time for each setting. | ||
| // For even iterations (setting=false), the execution time is 50ms. | ||
| // That will result in a faster execution, that will be learned by the selector. | ||
| // And the selector will eventually settle on setting=false. | ||
| var getValue = (int index) => index > 6 || index % 2 == 0 ? 50 : 100; | ||
|
|
||
| for (int i = 0; i < 20; i++) | ||
| { | ||
| bool setting = selector.GetQueryPlanCachingSetting(hash); | ||
|
|
||
| selector.ReportExecutionTime(hash, setting, getValue(i)); | ||
| } | ||
|
|
||
| Assert.False(selector.GetQueryPlanCachingSetting(hash), "Based on the logic of this test, the selector should have settled on setting=false."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenMultipleSequentialRequests_WithMultipleHashes_ThenTheSelectorShouldHandleIt() | ||
| { | ||
| QueryPlanSelector selector = new QueryPlanSelector(); | ||
|
|
||
| string hash1 = "hash1"; | ||
| string hash2 = "hash2"; | ||
| string hash3 = "hash3"; | ||
| string hash4 = "hash4"; | ||
|
|
||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| bool setting1 = selector.GetQueryPlanCachingSetting(hash1); | ||
| bool setting2 = selector.GetQueryPlanCachingSetting(hash2); | ||
| bool setting3 = selector.GetQueryPlanCachingSetting(hash3); | ||
| bool setting4 = selector.GetQueryPlanCachingSetting(hash4); | ||
|
|
||
| selector.ReportExecutionTime(hash1, setting1, setting1 ? 50 : 100); | ||
| selector.ReportExecutionTime(hash2, setting2, setting2 ? 100 : 50); | ||
| selector.ReportExecutionTime(hash3, setting3, setting3 ? 110 : 120); | ||
| selector.ReportExecutionTime(hash4, setting4, setting4 ? 30 : 20); | ||
| } | ||
|
|
||
| Assert.True(selector.GetQueryPlanCachingSetting(hash1)); | ||
| Assert.False(selector.GetQueryPlanCachingSetting(hash2)); | ||
| Assert.True(selector.GetQueryPlanCachingSetting(hash3)); | ||
| Assert.False(selector.GetQueryPlanCachingSetting(hash4)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenMultipleParallelRequests_WithTwoDifferentHashes_ThenTheSelectorShouldHandleIt() | ||
| { | ||
| QueryPlanSelector selector = new QueryPlanSelector(); | ||
|
|
||
| string hash1 = "hash1"; | ||
| string hash2 = "hash2"; | ||
| string hash3 = "hash3"; | ||
| string hash4 = "hash4"; | ||
|
|
||
| StringBuilder log = new StringBuilder(); | ||
|
|
||
| var result = Parallel.For(0, 100, (i) => | ||
| { | ||
| bool setting1 = selector.GetQueryPlanCachingSetting(hash1); | ||
| selector.ReportExecutionTime(hash1, setting1, setting1 ? 50 : 100); | ||
| log.AppendLine($"Hash1: Iteration {i}, Setting={setting1}"); | ||
|
|
||
| bool setting2 = selector.GetQueryPlanCachingSetting(hash2); | ||
| selector.ReportExecutionTime(hash2, setting2, setting2 ? 100 : 50); | ||
|
|
||
| bool setting3 = selector.GetQueryPlanCachingSetting(hash3); | ||
| selector.ReportExecutionTime(hash3, setting3, setting3 ? 110 : 120); | ||
|
|
||
| bool setting4 = selector.GetQueryPlanCachingSetting(hash4); | ||
| selector.ReportExecutionTime(hash4, setting4, setting4 ? 30 : 20); | ||
| }); | ||
|
|
||
| while (!result.IsCompleted) | ||
| { | ||
| await Task.Delay(10); | ||
| } | ||
|
|
||
| Assert.True(selector.GetQueryPlanCachingSetting(hash1)); | ||
| Assert.False(selector.GetQueryPlanCachingSetting(hash2)); | ||
| Assert.True(selector.GetQueryPlanCachingSetting(hash3)); | ||
| Assert.False(selector.GetQueryPlanCachingSetting(hash4)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenSeveralCacheEntries_WhenReachedTheLimit_ThenTheDefaultValueShouldBeReturned() | ||
| { | ||
| const int maxNumberOfEntries = 500; | ||
|
|
||
| QueryPlanSelector selector = new QueryPlanSelector(); | ||
|
|
||
| string hash = string.Empty; | ||
| bool setting = false; | ||
|
|
||
| for (int i = 0; i < maxNumberOfEntries; i++) | ||
| { | ||
| hash = $"hash{i}"; | ||
|
|
||
| // Learning phase. | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| selector.ReportExecutionTime(hash, setting, 1); | ||
|
|
||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.True(setting); | ||
| selector.ReportExecutionTime(hash, setting, 1); | ||
| } | ||
|
|
||
| // After limit is reached, new entries are not allowed. | ||
| hash = $"hash{maxNumberOfEntries + 1}"; | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
|
|
||
| // After limit is reached, new entries are not allowed. | ||
| hash = $"hash{maxNumberOfEntries + 2}"; | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
|
|
||
| // While older entries are still allowed. | ||
| for (int i = 0; i < maxNumberOfEntries; i++) | ||
| { | ||
| hash = $"hash{i}"; | ||
|
|
||
| // Learning phase. | ||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.False(setting); | ||
| selector.ReportExecutionTime(hash, setting, 1); | ||
|
|
||
| setting = selector.GetQueryPlanCachingSetting(hash); | ||
| Assert.True(setting); | ||
| selector.ReportExecutionTime(hash, setting, 1); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Health.Fhir.Core/Configs/QueryConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------------------------------------------- | ||
|
|
||
| namespace Microsoft.Health.Fhir.Core.Configs | ||
| { | ||
| public class QueryConfiguration | ||
| { | ||
| /// <summary> | ||
| /// If Dynamic Sql Query Plan Selection is enabled. | ||
| /// </summary> | ||
| public bool DynamicSqlQueryPlanSelectionEnabled { get; set; } = false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.