Home
%3CLINGO-SUB%20id%3D%22lingo-sub-751165%22%20slang%3D%22en-US%22%3EOne%20specific%20query%20runs%20slower%20in%20Azure%20DB%20than%20on-premises%20SQL%20Server%3F!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751165%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20inherit%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EWe%20often%20got%20complaints%20from%20customers%20that%20they%20found%20some%20specific%20query%20runs%20slower%20in%20Azure%20SQL%20DB%20than%20on-premises.%20Here%20are%20some%20steps%20that%20I%20normally%20check%3A%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20inherit%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EFirst%20of%20all%2C%20we%20need%20to%20know%20if%20we%20are%20comparing%20apple%20to%20apple%20here.%20A%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%22%3Ere%20you%20using%20the%20same%20parameters%20when%20running%20the%20query%20in%20both%20environments%3F%20Are%20the%20database%2Ftable%20size%20and%20row%20counts%20exactly%20the%20same%3F%20Are%20the%20table%20schema%2Findexes%20the%20same%20or%20not%3F%20Consider%20to%20export%20the%20database%20to%20the%20other%20environment%20to%20compare%20if%20possible.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20'Microsoft%20YaHei'%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20inherit%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3ESecondly%2C%20I%20would%20get%20the%20query%20actual%20execution%20plans%20if%20possible%20from%20both%20on-premises%20and%20Azure%20SQL%20DB%20so%20that%20we%20can%20compare.%20This%20will%20help%20us%20know%20what%20direction%20that%20we%20need%20to%20go%20next.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%20font-size%3A%2012.0pt%3B%20font-weight%3A%20normal%3B%20font-style%3A%20normal%3B%22%3EA.%20If%20the%20query%20plans%20are%20the%20same%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%20font-size%3A%2012.0pt%3B%22%3Eg%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3Eet%20wait%20type%20and%20wait%20time%20from%20the%20plan%20itself.%20%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22margin-top%3A%200%3B%20margin-bottom%3A%200%3B%20vertical-align%3A%20middle%3B%20color%3A%20%23333333%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3Eor%20check%20the%20query%20wait%20type%20during%20query%20execution%20by%20running%20this%20dmv%20continuously%20while%20the%20slow%20query%20is%20running.%20We%20need%20to%20confirm%20if%20there%20is%20any%20problematic%20wait%3A%20%3CBR%20%2F%3Eselect%20*%20from%20sys.dm_exec_requests%26nbsp%3B%20where%20database_id%3D%20%5Bdatabaseid%5D%20%26nbsp%3B%20(you%20will%20need%20to%20identify%20the%20problematic%20query%20request%20id)%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EFor%20example%2C%20if%20the%20wait%20type%20is%20LCK%2C%20you%20will%20want%20to%20check%20the%20blocker%20and%20kill%20it.%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EIf%20the%20wait%20type%20is%20SOS_SCHEDULER_YIELD%2C%20check%20the%20database%20usage%20as%20mentioned%20below.%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EIf%20the%20wait%20type%20is%20ASYNC_NETWORK_IO%2C%20ensure%20your%20application%20and%20azure%20sql%20db%20are%20in%20the%20same%20region.%20Also%20you%20can%20capture%20network%20trace%20or%20psping%20to%20understand%20if%20there%20is%20any%20latency.%20In%20addition%2C%20you%20can%20get%20the%20client%20statistics%20to%20see%20the%20network%20statistics%2C%20query%20execution%20statistics%20and%20et.%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EYou%20may%20see%20other%20wait%20types%20as%20well%20and%20troubleshoot%20based%20on%20what%20you%20see.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22color%3A%20%23333333%3B%20font-size%3A%2012pt%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3ECheck%20database%20resource%20usage%20by%20running%3A%20Select%20*%20FROM%20sys.dm_db_resource_stats.%20If%20any%20resource%20is%20hitting%20the%20100%25%2C%20consider%20to%20scale%20the%20database%20to%20next%20performance%20level%20or%20tune%20the%20top%20resource%20usage%20queries.%20Also%20ensure%20the%20statistics%20maintenance%20is%20done%20which%20can%20possibly%20improve%20the%20overall%20database%20performance.%20%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20'Microsoft%20YaHei'%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20font-weight%3A%20normal%3B%20font-style%3A%20normal%3B%22%3EB.%20If%20the%20query%20plans%20are%20different%2C%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3Erun%20the%20update%20statistics%20with%20fullscan%20against%20all%20the%20tables%20touched%20by%20the%20problematic%20query.%20Then%20check%20the%20performance%20and%20query%20plan%20again.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22margin-top%3A%200%3B%20margin-bottom%3A%200%3B%20vertical-align%3A%20middle%3B%20color%3A%20%23333333%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3Ecompare%20the%20compatibility%20level%20on%20both%20environments%3A%20SELECT%20name%2C%20compatibility_level%26nbsp%3BFROM%26nbsp%3Bsys.databases%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22margin-top%3A%200%3B%20margin-bottom%3A%200%3B%20vertical-align%3A%20middle%3B%20color%3A%20%23333333%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3Echeck%20the%20azure%20sql%20server%20configurations%20by%20select%20*%20from%20sys.database_scoped_configurations%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3Elook%20for%20cardinality%20estimation.%20If%20your%20onpremise%20sql%20server%20is%20running%20on%20relatively%20old%20version%2C%20consider%20to%20turn%20on%20LEGACY_CARDINALITY_ESTIMATION%20for%20testing.%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%20color%3A%20%23333333%3B%22%3EAlso%20you%20can%20compare%20the%20MAXDOP%20settings%20which%20can%20also%20been%20seen%20in%20query%20plan.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22color%3A%20%23333333%3B%20font-size%3A%2012pt%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3ESome%20other%20settings%20might%20be%20different%20too.%20For%20example%2C%20tempdb%20setting%2C%20encryption%2C%20traceflag%20and%20etc.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22color%3A%20%23333333%3B%20font-size%3A%2012pt%3B%22%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3EIn%20addition%2C%20the%20query%20plan%20can%20also%20change%20over%20the%20time.%20You%20can%20look%20into%20Query%20Store%20for%20this.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20%26amp%3Bquot%3B%20font-size%3A%2012.0pt%3B%22%3EHope%20it%20helps.%20Your%20feedback%20is%20very%20welcome!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

We often got complaints from customers that they found some specific query runs slower in Azure SQL DB than on-premises. Here are some steps that I normally check:

 

First of all, we need to know if we are comparing apple to apple here. Are you using the same parameters when running the query in both environments? Are the database/table size and row counts exactly the same? Are the table schema/indexes the same or not? Consider to export the database to the other environment to compare if possible.

 

Secondly, I would get the query actual execution plans if possible from both on-premises and Azure SQL DB so that we can compare. This will help us know what direction that we need to go next.

 

A. If the query plans are the same,

  • get wait type and wait time from the plan itself.
  • or check the query wait type during query execution by running this dmv continuously while the slow query is running. We need to confirm if there is any problematic wait:
    select * from sys.dm_exec_requests  where database_id= [databaseid]   (you will need to identify the problematic query request id)

For example, if the wait type is LCK, you will want to check the blocker and kill it.

If the wait type is SOS_SCHEDULER_YIELD, check the database usage as mentioned below.

If the wait type is ASYNC_NETWORK_IO, ensure your application and azure sql db are in the same region. Also you can capture network trace or psping to understand if there is any latency. In addition, you can get the client statistics to see the network statistics, query execution statistics and et.

You may see other wait types as well and troubleshoot based on what you see.

  • Check database resource usage by running: Select * FROM sys.dm_db_resource_stats. If any resource is hitting the 100%, consider to scale the database to next performance level or tune the top resource usage queries. Also ensure the statistics maintenance is done which can possibly improve the overall database performance.

 

B. If the query plans are different,

  • run the update statistics with fullscan against all the tables touched by the problematic query. Then check the performance and query plan again.
  • compare the compatibility level on both environments: SELECT name, compatibility_level FROM sys.databases;
  • check the azure sql server configurations by select * from sys.database_scoped_configurations

look for cardinality estimation. If your onpremise sql server is running on relatively old version, consider to turn on LEGACY_CARDINALITY_ESTIMATION for testing.

Also you can compare the MAXDOP settings which can also been seen in query plan.

  • Some other settings might be different too. For example, tempdb setting, encryption, traceflag and etc.
  • In addition, the query plan can also change over the time. You can look into Query Store for this.

 

Hope it helps. Your feedback is very welcome!