Home
%3CLINGO-SUB%20id%3D%22lingo-sub-797670%22%20slang%3D%22en-US%22%3EAzure%20SQL%20DB%20around%20the%20globe%20and%20latency%20-%20An%20architecture%20talk%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-797670%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20SQL%20Databases%20can%20be%20created%20in%20multiple%20regions%20around%20the%20globe%20and%20this%20is%20great%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fglobal-infrastructure%2Fservices%2F%3Fproducts%3Dsql-database%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%20size%3D%222%22%3Ehttps%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fglobal-infrastructure%2Fservices%2F%3Fproducts%3Dsql-database%3C%2FFONT%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126408i24E7CCB21C575D93%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3E*Image%20from%202019-08-09%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20you%20need%20to%20be%20aware%20of%20that%20when%20designing%20the%20architecture%20of%20your%20application.%20Working%20on%20a%20case%2C%20a%20customer%20complained%20that%20%3CSTRONG%3Eone%20specific%20query%20was%20too%20slow%3C%2FSTRONG%3E.%20After%20some%20analysis%20on%20Azure%20SQL%20DB%20we%20found%20the%20issue%20was%20not%20on%20Azure%20SQL%20DB%20itself%20but%20on%20the%20network%2C%20and%20not%20a%20failure%20in%20the%20network%2C%20but%20latency.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFind%20below%20some%20tests%20I%20made%20connecting%20to%20%3CSTRONG%3EAzure%20SQL%20DBs%20(General%20Purpose%3A%20Gen5%2C%202%20vCores)%3C%2FSTRONG%3E%26nbsp%3Bfrom%20my%20own%20computer%20and%20also%20from%20Azure%20VM%2C%20using%20script%20similar%20to%20this%20connection%20test%20script%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FFonsecaSergio%2FScriptCollection%2Fblob%2Fmaster%2FPowershell%2FTest%2520SQL%2520Connection.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%20size%3D%222%22%3Ehttps%3A%2F%2Fgithub.com%2FFonsecaSergio%2FScriptCollection%2Fblob%2Fmaster%2FPowershell%2FTest%2520SQL%2520Connection.ps1%3C%2FFONT%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EFull%20script%20at%20end%20of%20this%20article%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1444136332%22%20id%3D%22toc-hId--1444136332%22%3ETest%201%20-%20FROM%20NOTEBOOK%20TO%20AZURE%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Client%20in%20Portugal)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20Europe)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESIMPLE%20SELECT%20(Select%201)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3E%3CFONT%20face%3D%22courier%20new%2Ccourier%22%20size%3D%222%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126804iD97EE7B5006DC4D4%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%2045.8%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-1043376501%22%20id%3D%22toc-hId-1043376501%22%3ETest%202%20-%20INSIDE%20SAME%20DATA%20CENTER%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Azure%20VM%20on%20West%20Europe)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20Europe)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESIMPLE%20SELECT%20(Select%201)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%204%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId--764077962%22%20id%3D%22toc-hId--764077962%22%3ETest%203%20-%20BETWEEN%20AZURE%20DATA%20CENTERS%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Azure%20VM%20on%20West%20Europe)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20US)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESIMPLE%20SELECT%20(Select%201)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126803iEB88ED3058A45ABB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%20145.2%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1723434871%22%20id%3D%22toc-hId-1723434871%22%3ETest%20(1%2F2%2F3)%3C%2FH2%3E%0A%3CP%3EJust%20running%20a%20simple%20query%20%22SELECT%201%22%20got%20latency%20decreased%20in%26nbsp%3B%3CFONT%20face%3D%22arial%20black%2Cavant%20garde%22%3E~41%20ms%20%3C%2FFONT%3Ewhen%20running%20workload%20from%20inside%20azure.%20And%26nbsp%3Band%20a%20total%20increase%20of%20%3CFONT%20face%3D%22arial%20black%2Cavant%20garde%22%3E~140%20ms%3CFONT%20face%3D%22arial%2Chelvetica%2Csans-serif%22%3E%26nbsp%3Brunning%20from%20Azure%20to%20Azure%20(West%20Eu%20to%20West%20US)%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELets%20see%20how%20this%20affect%20a%20BIG%20QUERY%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--84019592%22%20id%3D%22toc-hId--84019592%22%3ETest%204%20-%20FROM%20NOTEBOOK%20TO%20AZURE%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Client%20in%20Portugal)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20Europe)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESELECT%20(289536%20rows)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%201207.5%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId--1891474055%22%20id%3D%22toc-hId--1891474055%22%3ETest%205%20-%20INSIDE%20SAME%20DATA%20CENTER%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Azure%20VM%20on%20West%20Europe)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20Europe)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESELECT%20(289536%20rows)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%20641.4%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-596038778%22%20id%3D%22toc-hId-596038778%22%3ETest%206%20-%20BETWEEN%20AZURE%20DATA%20CENTERS%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESource%3A%20SQL%20Client%20(Azure%20VM%20on%20West%20Europe)%20%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ETarget%3A%20Azure%20SQL%20DB%20(West%20US)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CFONT%20size%3D%225%22%3ESELECT%20(289536%20rows)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EAVG%20latency%20for%2010%20executions%20(ms)%3A%201513.3%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1211415685%22%20id%3D%22toc-hId--1211415685%22%3ETest%20(3%2F4%2F5)%3C%2FH2%3E%0A%3CP%3E*For%20this%20test%20we%20open%20and%20close%20the%20connections%2C%20that%20would%20not%20be%20good%20practice%2C%20because%20It%20will%20also%20increase%20the%20connection%20time%20and%20we%20could%20get%20benefit%20of%20using%20connection%20pool%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunning%20a%20simple%20query%20%22SELECT%20*%20FROM%20TEST%22%20got%20an%20decrease%20on%20execution%20time%20of%20%3CFONT%20face%3D%22arial%20black%2Cavant%20garde%22%3E~566%20ms%20(per%20execution)%26nbsp%3B%3C%2FFONT%3Erunning%20from%20inside%20Azure%20and%26nbsp%3Band%20a%20total%20increase%20of%20%3CFONT%20face%3D%22arial%20black%2Cavant%20garde%22%3E~872ms%20(per%20execution)%3CFONT%20face%3D%22arial%2Chelvetica%2Csans-serif%22%3E%26nbsp%3Brunning%20from%20Azure%20to%20Azure%20(West%20Eu%20to%20West%20US)%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1276097148%22%20id%3D%22toc-hId-1276097148%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EThis%20will%20happen%20because%20even%20though%20data%20is%20traveling%20inside%20Azure%20Network%20(from%20Azure%20to%20Azure)%20the%20data%20need%20to%20go%20around%20the%20globe%20and%20this%20takes%20time%20and%20can%20get%20worse%20depending%20on%20the%20data%20size%20that%20you%20are%20returning.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126803iEB88ED3058A45ABB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhen%20developing%20to%20Azure%20always%20plan%20to%20put%20your%20client%20near%20your%20server.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20use%20geo%20replication%20to%20have%20read%20replicas%20near%20your%20clients%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-active-geo-replication%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-active-geo-replication%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E------------------------------------------------%3C%2FP%3E%0A%3CP%3EBelow%20the%20sample%20script%20used%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EClear-Host%0A%0A%24serverName%20%3D%20%22SERVER.database.windows.net%22%0A%24databaseName%20%3D%20%22sandbox%22%0A%24Username%20%3D%20%22FonsecaSergio%22%0A%24Password%20%3D%20%22xxxxxxxxxxxxxx%22%0A%24query%20%3D%20%22SELECT%201%22%0A%24LoopCount%20%3D%2010%0A%0A%0A%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%24ignore1execforAvg%20%3D%20%24true%0A%24ConnectionTimeout%20%3D%2015%0A%24connectionString%20%3D%20%22Server%3Dtcp%3A%24(%24serverName)%3BInitial%20Catalog%3D%24(%24databaseName)%3BPersist%20Security%20Info%3DFalse%3BUser%20ID%3D%24(%24Username)%3BPassword%3D%24(%24Password)%3BMultipleActiveResultSets%3DFalse%3BEncrypt%3DTrue%3BTrustServerCertificate%3DFalse%3BConnection%20Timeout%3D%24(%24ConnectionTimeout)%22%0A%24connection%20%3D%20New-Object%20-TypeName%20System.Data.SqlClient.SqlConnection(%24connectionString)%0A%24connection.StatisticsEnabled%20%3D%201%0A%0AWrite-Host%20%22Connection%20to%20Server%20(%24(%24serverName))%20%2F%20DB%20(%24(%24databaseName))%20%2F%20UserName%20(%24(%24Username))%22%0A%5BSystem.Collections.ArrayList%5D%24arrayLatency%20%3D%20%40()%0A%0ATry%7B%0A%24command%20%3D%20New-Object%20-TypeName%20System.Data.SqlClient.SqlCommand%0A%24command.CommandTimeout%20%3D%2060%0A%24command.CommandText%20%3D%20%24query%0A%24command.Connection%3D%24connection%0A%0A%24aux%20%3D%200%0Awhile%20(%24aux%20-le%20%24LoopCount)%0A%7B%0A%0A%24stopwatch%20%3D%20%5Bsystem.diagnostics.stopwatch%5D%3A%3AStartNew()%0A%0A%24connection.Open()%20%0A%24result%20%3D%20%24command.ExecuteScalar()%20%0A%23Write-Host%20%22Query%20success.%20Server%20currenttime%20(%24(%24result))%22%0A%0A%24connection.Close()%20%0A%0A%24stopwatch.Stop()%0A%0Aif(%24ignore1execforAvg%20-and%20%24aux%20-eq%200)%0A%7B%0Awrite-Output%20(%22FIRST%20EXECUTION%20WILL%20BE%20IGNORED%20for%201%20-%20AVG%20-%20Stopwatch%20latency%20(ms)%3A%20%24(%24stopwatch.ElapsedMilliseconds)%22)%0A%7D%0Aelse%0A%7B%0A%24arrayLatency.Add(%24stopwatch.ElapsedMilliseconds)%20%7C%20Out-Null%0A%0Awrite-Output%20(%22Exec%20(%24(%24aux))%20Stopwatch%20latency%20(ms)%3A%20%24(%24stopwatch.ElapsedMilliseconds)%22)%0A%0A%24data%20%3D%20%24connection.RetrieveStatistics()%0Awrite-Output%20%22-------------------------%22%0Awrite-Output%20(%22NetworkServerTime%20(ms)%3A%20%22%20%2B%24data.NetworkServerTime)%0Awrite-Output%20(%22Execution%20Time%20(ms)%20%3A%20%22%20%2B%24data.ExecutionTime)%0Awrite-Output%20(%22Connection%20Time%20%3A%20%22%20%2B%24data.ConnectionTime)%0Awrite-Output%20(%22ServerRoundTrips%20%3A%20%22%20%2B%24data.ServerRoundtrips)%0Awrite-Output%20(%22BuffersReceived%20%3A%20%22%20%2B%24data.BuffersReceived)%0Awrite-Output%20(%22SelectRows%20%3A%20%22%20%2B%24data.SelectRows)%0Awrite-Output%20(%22SelectCount%20%3A%20%22%20%2B%24data.SelectCount)%0Awrite-Output%20(%22BytesSent%20%3A%20%22%20%2B%24data.BytesSent)%0Awrite-Output%20(%22BytesReceived%20%3A%20%22%20%2B%24data.BytesReceived)%0Awrite-Output%20%22-------------------------%22%20%0A%7D%0A%0A%24connection.ResetStatistics()%0A%0A%24aux%20%3D%20%24aux%20%2B%201%0A%7D%0A%0A%24LatencyAvg%20%3D%20(%24arrayLatency%20%7C%20Measure-Object%20-Average).Average%0Awrite-Output%20(%22Stopwatch%20AVG%20latency%20for%20%24(%24LoopCount)%20executions%20(ms)%3A%20%24(%24LatencyAvg)%22)%0A%0A%0A%7D%0Acatch%20%5BSystem.Data.SqlClient.SqlException%5D%0A%7B%0A%24_.Exception.Errors%5B0%5D%20%7C%20Out-Host%0A%0A%24ExceptionMessage%20%3D%20%22SQL%20Exception%3A%20(%24(%24_.Exception.Number))%20%2F%20State%3A%20(%24(%24_.Exception.State))%20%2F%20%24(%24_.Exception.Message)%22%20%0AWrite-Error%20%24ExceptionMessage%0AWrite-Host%20%22ClientConnectionId(%24(%24connection.ClientConnectionId))%22%0AWrite-Host%20%22Exception.ClientConnectionId%20(%24(%24_.Exception.ClientConnectionId))%22%0A%7D%0ACatch%0A%7B%0AWrite-Error%20%24_.Exception.Message%0A%7D%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-797670%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20to%20develop%20globally%20distributed%20queries%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

Azure SQL Databases can be created in multiple regions around the globe and this is great

https://azure.microsoft.com/en-us/global-infrastructure/services/?products=sql-database

 

clipboard_image_0.png

*Image from 2019-08-09

 

But you need to be aware of that when designing the architecture of your application. Working on a case, a customer complained that one specific query was too slow. After some analysis on Azure SQL DB we found the issue was not on Azure SQL DB itself but on the network, and not a failure in the network, but latency.

 

Find below some tests I made connecting to Azure SQL DBs (General Purpose: Gen5, 2 vCores) from my own computer and also from Azure VM, using script similar to this connection test script: 

https://github.com/FonsecaSergio/ScriptCollection/blob/master/Powershell/Test%20SQL%20Connection.ps1

Full script at end of this article

 

Test 1 - FROM NOTEBOOK TO AZURE

  • Source: SQL Client (Client in Portugal)
  • Target: Azure SQL DB (West Europe)
  • SIMPLE SELECT (Select 1)

clipboard_image_1.png

AVG latency for 10 executions (ms): 45.8

Test 2 - INSIDE SAME DATA CENTER

  • Source: SQL Client (Azure VM on West Europe)
  • Target: Azure SQL DB (West Europe)
  • SIMPLE SELECT (Select 1)
AVG latency for 10 executions (ms): 4

Test 3 - BETWEEN AZURE DATA CENTERS

  • Source: SQL Client (Azure VM on West Europe)
  • Target: Azure SQL DB (West US)
  • SIMPLE SELECT (Select 1)

clipboard_image_0.png

AVG latency for 10 executions (ms): 145.2

 

 

Test (1/2/3)

Just running a simple query "SELECT 1" got latency decreased in ~41 ms when running workload from inside azure. And and a total increase of ~140 ms running from Azure to Azure (West Eu to West US)

 

 

 

Lets see how this affect a BIG QUERY

Test 4 - FROM NOTEBOOK TO AZURE

  • Source: SQL Client (Client in Portugal)
  • Target: Azure SQL DB (West Europe)
  • SELECT (289536 rows)
AVG latency for 10 executions (ms): 1207.5

Test 5 - INSIDE SAME DATA CENTER

  • Source: SQL Client (Azure VM on West Europe)
  • Target: Azure SQL DB (West Europe)
  • SELECT (289536 rows)
AVG latency for 10 executions (ms): 641.4

Test 6 - BETWEEN AZURE DATA CENTERS

  • Source: SQL Client (Azure VM on West Europe)
  • Target: Azure SQL DB (West US)
  • SELECT (289536 rows)
AVG latency for 10 executions (ms): 1513.3

 

 

Test (3/4/5)

*For this test we open and close the connections, that would not be good practice, because It will also increase the connection time and we could get benefit of using connection pool

 

Running a simple query "SELECT * FROM TEST" got an decrease on execution time of ~566 ms (per execution) running from inside Azure and and a total increase of ~872ms (per execution) running from Azure to Azure (West Eu to West US)

 

Conclusion

This will happen because even though data is traveling inside Azure Network (from Azure to Azure) the data need to go around the globe and this takes time and can get worse depending on the data size that you are returning. 

clipboard_image_0.png

 

When developing to Azure always plan to put your client near your server.

 

You can also use geo replication to have read replicas near your clients

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-active-geo-replication

 

------------------------------------------------

Below the sample script used

 

 

 

Clear-Host

$serverName = "SERVER.database.windows.net"
$databaseName = "sandbox"
$Username = "FonsecaSergio"
$Password = "xxxxxxxxxxxxxx"
$query = "SELECT 1"
$LoopCount = 10




########################################################################################################
$ignore1execforAvg = $true
$ConnectionTimeout = 15
$connectionString = "Server=tcp:$($serverName);Initial Catalog=$($databaseName);Persist Security Info=False;User ID=$($Username);Password=$($Password);MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=$($ConnectionTimeout)"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$connection.StatisticsEnabled = 1

Write-Host "Connection to Server ($($serverName)) / DB ($($databaseName)) / UserName ($($Username))"
[System.Collections.ArrayList]$arrayLatency = @()

Try{
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand
$command.CommandTimeout = 60
$command.CommandText = $query
$command.Connection=$connection

$aux = 0
while ($aux -le $LoopCount)
{

$stopwatch = [system.diagnostics.stopwatch]::StartNew()

$connection.Open() 
$result = $command.ExecuteScalar() 
#Write-Host "Query success. Server currenttime ($($result))"

$connection.Close() 

$stopwatch.Stop()

if($ignore1execforAvg -and $aux -eq 0)
{
write-Output ("FIRST EXECUTION WILL BE IGNORED for 1 - AVG - Stopwatch latency (ms): $($stopwatch.ElapsedMilliseconds)")
}
else
{
$arrayLatency.Add($stopwatch.ElapsedMilliseconds) | Out-Null

write-Output ("Exec ($($aux)) Stopwatch latency (ms): $($stopwatch.ElapsedMilliseconds)")

$data = $connection.RetrieveStatistics()
write-Output "-------------------------"
write-Output ("NetworkServerTime (ms): " +$data.NetworkServerTime)
write-Output ("Execution Time (ms) : " +$data.ExecutionTime)
write-Output ("Connection Time : " +$data.ConnectionTime)
write-Output ("ServerRoundTrips : " +$data.ServerRoundtrips)
write-Output ("BuffersReceived : " +$data.BuffersReceived)
write-Output ("SelectRows : " +$data.SelectRows)
write-Output ("SelectCount : " +$data.SelectCount)
write-Output ("BytesSent : " +$data.BytesSent)
write-Output ("BytesReceived : " +$data.BytesReceived)
write-Output "-------------------------" 
}

$connection.ResetStatistics()

$aux = $aux + 1
}

$LatencyAvg = ($arrayLatency | Measure-Object -Average).Average
write-Output ("Stopwatch AVG latency for $($LoopCount) executions (ms): $($LatencyAvg)")


}
catch [System.Data.SqlClient.SqlException]
{
$_.Exception.Errors[0] | Out-Host

$ExceptionMessage = "SQL Exception: ($($_.Exception.Number)) / State: ($($_.Exception.State)) / $($_.Exception.Message)" 
Write-Error $ExceptionMessage
Write-Host "ClientConnectionId($($connection.ClientConnectionId))"
Write-Host "Exception.ClientConnectionId ($($_.Exception.ClientConnectionId))"
}
Catch
{
Write-Error $_.Exception.Message
}