%3CLINGO-SUB%20id%3D%22lingo-sub-1692819%22%20slang%3D%22en-US%22%3EMaking%20Log%20Analytics%20Queries%20More%20Efficient%20%E2%80%93%20Part%201%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1692819%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHello%20folks%2C%3C%2FP%3E%0A%3CP%3EBruno%20Gabrielli%20here%20again.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20there%20are%20lots%20of%20good%20resources%20about%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Flog-query%2Flog-query-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewriting%20Log%20Analytics%20queries%3C%2FA%3E%20and%20many%20blog%20posts%20have%20been%20written%2C%20today%20I%20would%20like%20to%20point%20your%20attention%20to%20something%20that%20I%20am%20frequently%20seeing%20on%2099%25%20of%20my%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fresources%2Fvideos%2Fazure-friday-azure-log-analytics%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Azure%20Log%20Analytics%3C%2FA%3E%20customers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20is%20about%20a%20very%20simple%2C%20but%20most%20often%20under%20evaluated%2C%20aspect%20of%20writing%20queries%3A%20query%20performance%20also%20referred%20as%20query%20speed%20or%20query%20efficiency.%20I%20will%20give%20you%20my%20point%20of%20view%20using%20a%20series-of%20posts.%20Welcome%20to%20part%201%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet%20me%20quickly%20spend%20some%20words%20on%20the%20query%20language%20behind%20Azure%20Log%20Analytics.%20The%20language%20was%20first%20conceived%20when%20a%20service%20codenamed%20Kusto%20was%20first%20launched%20internally%20at%20Microsoft%20in%202014.%20This%20service%2C%20now%20available%20to%20everyone%20as%20Azure%20Data%20Explorer%2C%20introduced%20among%20its%20many%20components%20the%20Kusto%20Query%20Language%20(KQL).%20Eventually%2C%20the%20service%20kept%20growing%20and%20powering%20more%20and%20more%20services%2C%20including%20the%20service%20today%20known%20as%20Azure%20Monitor%20Logs%20(formerly%20known%20as%20OMS%2C%20and%20Azure%20Log%20Analytics).%20As%20you%20can%20imagine%2C%20this%20journey%20was%20not%20only%20a%20simple%20rename%20but%20also%20included%20enhancements%20in%20the%20number%20of%20commands%2C%20functions%2C%20operators%2C%20etc.%20as%20well%20as%20performance%20and%20scalability%20improvements.%20Nowadays%2C%20it%20is%20a%20very%20powerful%20query%20language%20with%20lots%20of%20good%20features%20and%20functionalities.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAmong%20all%2C%20the%202%20things%20I%20appreciate%20the%20most%20in%20this%20language%20are%3A%20the%20use%20of%20pipelines%20(like%20in%20PowerShell%3B%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Flearn%2Funderstanding-the-powershell-pipeline%3Fview%3Dpowershell-5.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUnderstanding%20pipelines%3C%2FA%3E)%20and%20a%20set%20of%20commands%20that%20are%20instantly%20familiar%20to%20anyone%20who%20has%20used%20languages%20such%20as%20SQL.%20This%20means%20to%20me%20that%20if%20you%20have%20even%20a%20basic%20understanding%20of%20SQL%20Syntax%2C%20you%20start%20with%20an%20advantage%20(see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fsqlcheatsheet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQL%20to%20Kusto%20query%20translation%3C%2FA%3E)%20%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E.%20If%20you%20wish%2C%20you%20can%20read%20more%20about%20the%20query%20language%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOverview%3C%2FA%3E%20documentation%20page.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20want%20to%20stress%20that%20understanding%20the%20use%20of%20pipelines%20is%20fundamental%20to%20the%20creation%20of%20%E2%80%9C%3CSTRONG%3E%3CEM%3Eefficient%3C%2FEM%3E%3C%2FSTRONG%3E%E2%80%9D%20queries.%20If%20you%20went%20through%20the%20pipelines%20article%20above%2C%20you%20are%20now%20familiar%20with%20the%20concept%20that%20pipelines%20are%20used%20to%20pass%20commands%E2%80%99%20output%2C%20or%20in%20this%20case%20the%20dataset%2C%20to%20the%20next%20command.%20With%20that%20clear%20in%20mind%2C%20let%20me%20ask%20you%20a%20question%20and%2C%20at%20the%20same%20time%2C%20give%20you%20food%20for%20thought%3A%20what%20happens%20if%20you%20pass%20a%20big%20amount%20of%20data%20to%20the%20next%20command%20and%20then%20to%20the%20next%20and%20so%20on%3F%20Simple%20answer%3A%20the%20commands%20will%20be%20taking%20more%20time%20than%20necessary%20to%20complete%20because%20they%20will%20have%20to%20process%20the%20entire%20amount%20of%20data%20passed%20by%20the%20previous%20command(s)%20which%20include%20useless%20data.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20we%20definitely%20agree%20on%20the%20fact%20that%20probably%20there%20is%20no%20single%20%3CSTRONG%3E%3CEM%3Ecorrect%3C%2FEM%3E%3C%2FSTRONG%3E%20way%20of%20writing%20queries%2C%20but%20that%20there%20are%20for%20sure%20some%20not%20optimized%20ways%20of%20doing%20it.%20For%20instance%2C%20what%20do%20you%20think%20about%20doing%20a%20search%20on%20all%20tables%20or%20not%20filtering%20against%20time%20as%20first%20thing%20in%20your%20query%3F%20I%20tried%20myself%20experimenting%20these%20best-practices%20and%20found%20out%20the%20huge%20benefit%20out%20of%20them.%20In%20particular%20the%20first%20query%20was%20very%20time%20consuming%20and%20ran%20longer%20than%20expected%20from%20a%20platform%20like%20Azure.%20But%20it%20is%20not%20Azure%20fault%3B%20it%E2%80%99s%20actually%20our%20fault%20not%20following%20them!%20Know%20your%20data%20first!!!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%20means%20that%20we%20should%20avoid%20as%20much%20as%20possible%20production%20queries%20that%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EUse%20the%20%3CSTRONG%3E%3CEM%3Esearch%3C%2FEM%3E%3C%2FSTRONG%3E%20command%20to%20search%20the%20entire%20workspace.%3C%2FLI%3E%0A%3CLI%3EDo%20not%20filter%20enough%20(remember%20to%20filter%20against%20time%20first).%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3Ein%20favor%20of%20being%20selective%20with%20the%20data%20we%20need%20and%20applying%20filters%20pipeline%20after%20pipeline.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20secret%20is%20exactly%20in%20the%20table%20selection%20as%20well%20as%20in%20the%20filtering%20since%20each%20pipeline%20will%20pass%20a%20smaller%20dataset%20to%20the%20next%2C%20making%20the%20rest%20of%20the%20query%20more%20and%20more%20efficient.%20You%20got%20it!%20We%20can%20reduce%20the%20amount%20data%20upfront%20by%20selecting%20the%20right%20table(s)%20and%20we%20can%20reduce%20even%20more%20by%20cutting%20useless%20data%20by%20filtering.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUsing%20the%20funnel%20approach%20represented%20below%2C%20you%20surely%20notice%20that%20by%20selecting%20the%20right%20table%20and%20by%20filtering%20out%20pipeline%20after%20pipeline%2C%20the%20effective%20data%20manipulation%20will%20happen%20on%20a%20smaller%20amount%20of%20data.%20This%20will%20make%20the%20query%20much%20more%20efficient%20and%20the%20underlying%20engine%20working%20faster%20as%20we%20all%20expect.%20Remember%20that%2C%20even%20if%20%3CSTRONG%3Escalability%20is%20not%20even%20a%20point%20in%20Azure%3C%2FSTRONG%3E%2C%20if%20we%20all%20run%20poor-written%20queries%20we%20all%20suffer%20the%20consequences.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_6%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-90px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Funnel.png%22%20style%3D%22width%3A%20365px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220218i7198F4AFC3EA27AA%2Fimage-dimensions%2F365x305%3Fv%3D1.0%22%20width%3D%22365%22%20height%3D%22305%22%20title%3D%22Funnel.png%22%20alt%3D%22Funnel.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3ESo%2C%20coming%20back%20to%20the%20bad%20examples%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EUsing%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fsearchoperator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esearch%20operator%3C%2FA%3E%20searching%20the%20entire%20workspace%20means%20that%20query%20engine%20will%20scan%20each%20and%20every%20table%20and%20column%20which%20is%20very%20bad.%3C%2FLI%3E%0A%3CLI%3ENot%20using%20good%20filters%20will%20make%20a%20dataset%20bigger%20than%20needed%2Fexpected%20to%20be%20passed%20along%20to%20the%20next%20pipeline.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EIf%20what%20I%20explained%20so%20far%20makes%20sense%2C%20you%E2%80%99ll%20discover%20that%20the%20gotcha%20is%3A%20you%20should%20know%20how%20your%20data%20looks%20like%20and%20should%2C%20as%20much%20as%20possible%2C%20never%20write%20a%20query%20for%20production%20environments%20using%20the%202%20approaches%20above.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20prove%20what%20I%20am%20saying%2C%20I%20am%20going%20to%20give%20you%20some%20examples%20of%20what%20happens%20when%20not%20following%20the%20above%20mentioned%20best-practices%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EScenario%20%231%3A%20Searching%20the%20entire%20table%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E%3CEM%3Esearch%20%2226401%22%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSTRONG%3Evs%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CFONT%20color%3D%22%23339966%22%3E%3CSTRONG%3E%3CEM%3ESecurityEvent%20%7C%20where%20EventId%20%3D%3D%20%E2%80%9C26401%E2%80%9D%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3Eto%20search%20for%20an%20event%20ID%20with%20number%2026401.%20The%20result%20screenshots%20below%20show%20the%202%20queries%20returning%20the%20same%20number%20of%20records%20but%20taking%20a%20considerable%20different%20amount%20of%20time.%20You%20can%20easily%20compare%20the%20execution%20time%3A%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E~18%3C%2FSTRONG%3E%3C%2FFONT%3E%20secs%20compared%20to%20%3CFONT%20color%3D%22%23339966%22%3E%3CSTRONG%3E~5%3C%2FSTRONG%3E%3C%2FFONT%3E%20secs.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Un-scoped%20Search%20Query.png%22%20style%3D%22width%3A%20606px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220219i8CC343F872190E31%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Un-scoped%20Search%20Query.png%22%20alt%3D%22Un-scoped%20Search%20Query.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Scoped%20Query.png%22%20style%3D%22width%3A%20616px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220220i42632FBB66B367CE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Scoped%20Query.png%22%20alt%3D%22Scoped%20Query.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EScenario%20%232%3A%20Always%20filter%20by%20time%20%3CU%3Efirst%3C%2FU%3E%2C%20either%20by%3CUL%3E%0A%3CLI%3Eusing%20the%20built-in%20%3CSTRONG%3E%3CEM%3ETime%20Range%3C%2FEM%3E%3C%2FSTRONG%3E%20picker%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CFONT%20color%3D%22%23339966%22%3E%3CSTRONG%3E%3CEM%3EEvent%20%7C%20where%20EventID%20%3D%3D%207036%20%7C%20where%20Computer%20%3D%3D%20%22SC-DC01.Contoso.Local%22%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22TimePicker.png%22%20style%3D%22width%3A%20665px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220221iBD3CA9FC644F4688%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22TimePicker.png%22%20alt%3D%22TimePicker.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3Eor%20using%20the%20%3CSTRONG%3E%3CEM%3Ewhere%3C%2FEM%3E%3C%2FSTRONG%3E%20clause%20directly%20in%20the%20query%3A%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CFONT%20color%3D%22%23339966%22%3E%3CSTRONG%3E%3CEM%3EEvent%20%7C%20where%20%3CFONT%20color%3D%22%23339966%22%3ETimeGenerated%20%3C%2FFONT%3E%26gt%3B%20ago(7d)%20%7C%20where%20EventID%20%3D%3D%207036%20%7C%20where%20Computer%20%3D%3D%20%22SC-DC01.Contoso.Local%22%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_4%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Where%20TimeGenerated.png%22%20style%3D%22width%3A%20664px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220224iE366A607DD84ED3A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Where%20TimeGenerated.png%22%20alt%3D%22Where%20TimeGenerated.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-left%3A%2060px%3B%20padding-left%3A%2040px%3B%20background%3A%20%23d2f9d2%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-left%3A%2060px%3B%20padding-left%3A%2020px%3B%20background%3A%20%23d2f9d2%3B%22%3E%3CSTRONG%3E%3CEM%3ETIP%3A%3C%2FEM%3E%3C%2FSTRONG%3E%20%3CSTRONG%3E%3CEM%3ETime%20Range%3C%2FEM%3E%3C%2FSTRONG%3E%20picker%20and%20%3CSTRONG%3E%3CEM%3Ewhere%3C%2FEM%3E%3C%2FSTRONG%3E%20clause%20behave%20the%20same.%20The%20only%20advantage%20using%20the%20where%20clause%20is%20that%20you%20can%20define%20more%20intervals%20compared%20to%20the%20list%20offered%20by%20the%20Time%20Picker.%20The%20screenshots%20above%2C%20show%20the%20method%20in%20in%20action.%3C%2FP%3E%0A%3CP%20style%3D%22margin-left%3A%2060px%3B%20padding-left%3A%2040px%3B%20background%3A%20%23d2f9d2%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-210px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-210px%22%3E%3CSTRONG%3EInstead%20of%20doing%20this%20after%20other%20filters%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E%3CEM%3EEvent%20%7C%20where%20EventID%20%3D%3D%207036%20%7C%20where%20Computer%20%3D%3D%20%22SC-DC01.Contoso.Local%22%20%7C%20where%20TimeGenerated%20%26gt%3B%20ago(7d)%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorBruno%20Gabrielli_5%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Filter%20at%20the%20end.png%22%20style%3D%22width%3A%20665px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220227i38BDF0710C2B6F17%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Filter%20at%20the%20end.png%22%20alt%3D%22Filter%20at%20the%20end.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3Eto%20search%20for%20EventID%20%3CU%3E7036%3C%2FU%3E%20received%20from%20a%20computer%20called%20%3CU%3ESC-DC-01.Contoso.Local%3C%2FU%3E%20in%20the%20necessary%20amount%20of%20time%20(i.e.%20last%20%3CU%3E7%3C%2FU%3E%20days).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3EYou%20can%20easily%20notice%20they%20are%20returning%20the%20same%20number%20of%20records%20(I%20precisely%20counted%20them%20using%20a%20different%20query%20and%20they%20were%20%3CFONT%20color%3D%22%233366FF%22%3E%3CSTRONG%3E272%3C%2FSTRONG%3E%3C%2FFONT%3E)%20running%20for%20an%20interesting%20different%20amount%20of%20time%20(%3CFONT%20color%3D%22%23339966%22%3E%3CSTRONG%3E~0.5%3C%2FSTRONG%3E%3C%2FFONT%3E%20secs%20compared%20to%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E~1.2%3C%2FSTRONG%3E%3C%2FFONT%3E%20secs).%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConsidering%20that%20I%20am%20using%20my%20lab%20workspace%20which%20contains%20less%20than%209GB%20of%20data%2C%20would%20you%20image%20how%20much%20time%20can%20you%20save%20on%20a%20workspace%20of%201TB%3F%20And%20how%20much%20time%20can%20%3CSTRONG%3Ewe%3C%2FSTRONG%3E%20save%2C%20and%20what%20benefit%20can%20%3CSTRONG%3Ewe%3C%2FSTRONG%3E%20get%20altogether%20by%20running%20well%20written%20queries%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20leave%20you%20reflecting%20on%20this%20and%20making%20your%20calculation%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%2C%3C%2FP%3E%0A%3CP%3EBruno.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EDisclaimer%3C%2FSTRONG%3E%3CBR%20%2F%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1692819%22%20slang%3D%22en-US%22%3E%3CP%3EWhile%20there%20are%20lots%20of%20good%20resources%20about%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Flog-query%2Flog-query-overview%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ewriting%20Log%20Analytics%20queries%3C%2FA%3E%20and%20many%20blog%20posts%20have%20been%20written%2C%20today%20I%20would%20like%20to%20point%20your%20attention%20to%20something%20that%20I%20am%20frequently%20seeing%20on%2099%25%20of%20my%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fresources%2Fvideos%2Fazure-friday-azure-log-analytics%2F%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3E%20Azure%20Log%20Analytics%3C%2FA%3E%20customers.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Funnel.png%22%20style%3D%22width%3A%20877px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F220218i7198F4AFC3EA27AA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Funnel.png%22%20alt%3D%22Funnel.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1692819%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBrunoGabrielli%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

 

Hello folks,

Bruno Gabrielli here again.

 

While there are lots of good resources about writing Log Analytics queries and many blog posts have been written, today I would like to point your attention to something that I am frequently seeing on 99% of my Azure Log Analytics customers.

 

It is about a very simple, but most often under evaluated, aspect of writing queries: query performance also referred as query speed or query efficiency. I will give you my point of view using a series-of posts. Welcome to part 1 :smile:.

 

Let me quickly spend some words on the query language behind Azure Log Analytics. The language was first conceived when a service codenamed Kusto was first launched internally at Microsoft in 2014. This service, now available to everyone as Azure Data Explorer, introduced among its many components the Kusto Query Language (KQL). Eventually, the service kept growing and powering more and more services, including the service today known as Azure Monitor Logs (formerly known as OMS, and Azure Log Analytics). As you can imagine, this journey was not only a simple rename but also included enhancements in the number of commands, functions, operators, etc. as well as performance and scalability improvements. Nowadays, it is a very powerful query language with lots of good features and functionalities.

 

Among all, the 2 things I appreciate the most in this language are: the use of pipelines (like in PowerShell; see Understanding pipelines) and a set of commands that are instantly familiar to anyone who has used languages such as SQL. This means to me that if you have even a basic understanding of SQL Syntax, you start with an advantage (see SQL to Kusto query translation) :smile:. If you wish, you can read more about the query language on the Overview documentation page.

 

I want to stress that understanding the use of pipelines is fundamental to the creation of “efficient” queries. If you went through the pipelines article above, you are now familiar with the concept that pipelines are used to pass commands’ output, or in this case the dataset, to the next command. With that clear in mind, let me ask you a question and, at the same time, give you food for thought: what happens if you pass a big amount of data to the next command and then to the next and so on? Simple answer: the commands will be taking more time than necessary to complete because they will have to process the entire amount of data passed by the previous command(s) which include useless data.

 

I think we definitely agree on the fact that probably there is no single correct way of writing queries, but that there are for sure some not optimized ways of doing it. For instance, what do you think about doing a search on all tables or not filtering against time as first thing in your query? I tried myself experimenting these best-practices and found out the huge benefit out of them. In particular the first query was very time consuming and ran longer than expected from a platform like Azure. But it is not Azure fault; it’s actually our fault not following them! Know your data first!!!

 

That means that we should avoid as much as possible production queries that:

  1. Use the search command to search the entire workspace.
  2. Do not filter enough (remember to filter against time first).

in favor of being selective with the data we need and applying filters pipeline after pipeline.

 

The secret is exactly in the table selection as well as in the filtering since each pipeline will pass a smaller dataset to the next, making the rest of the query more and more efficient. You got it! We can reduce the amount data upfront by selecting the right table(s) and we can reduce even more by cutting useless data by filtering.

 

Using the funnel approach represented below, you surely notice that by selecting the right table and by filtering out pipeline after pipeline, the effective data manipulation will happen on a smaller amount of data. This will make the query much more efficient and the underlying engine working faster as we all expect. Remember that, even if scalability is not even a point in Azure, if we all run poor-written queries we all suffer the consequences.

 

Funnel.png

 

So, coming back to the bad examples:

  1. Using the search operator searching the entire workspace means that query engine will scan each and every table and column which is very bad.
  2. Not using good filters will make a dataset bigger than needed/expected to be passed along to the next pipeline.

If what I explained so far makes sense, you’ll discover that the gotcha is: you should know how your data looks like and should, as much as possible, never write a query for production environments using the 2 approaches above.

 

To prove what I am saying, I am going to give you some examples of what happens when not following the above mentioned best-practices:

  • Scenario #1: Searching the entire table:

search "26401"

vs

SecurityEvent | where EventId == “26401”

to search for an event ID with number 26401. The result screenshots below show the 2 queries returning the same number of records but taking a considerable different amount of time. You can easily compare the execution time: ~18 secs compared to ~5 secs.

 

Un-scoped Search Query.png

 

Scoped Query.png

 

  • Scenario #2: Always filter by time first, either by
    • using the built-in Time Range picker:

Event | where EventID == 7036 | where Computer == "SC-DC01.Contoso.Local"

 

TimePicker.png

 

or using the where clause directly in the query:

Event | where TimeGenerated > ago(7d) | where EventID == 7036 | where Computer == "SC-DC01.Contoso.Local"

 

Where TimeGenerated.png

 

 

TIP: Time Range picker and where clause behave the same. The only advantage using the where clause is that you can define more intervals compared to the list offered by the Time Picker. The screenshots above, show the method in in action.

 

 

Instead of doing this after other filters

 

Event | where EventID == 7036 | where Computer == "SC-DC01.Contoso.Local" | where TimeGenerated > ago(7d)

 

Filter at the end.png

 

to search for EventID 7036 received from a computer called SC-DC-01.Contoso.Local in the necessary amount of time (i.e. last 7 days).

 

You can easily notice they are returning the same number of records (I precisely counted them using a different query and they were 272) running for an interesting different amount of time (~0.5 secs compared to ~1.2 secs).

 

Considering that I am using my lab workspace which contains less than 9GB of data, would you image how much time can you save on a workspace of 1TB? And how much time can we save, and what benefit can we get altogether by running well written queries?

 

I leave you reflecting on this and making your calculation :smile:.

 

Thanks,

Bruno.

 

Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.