%3CLINGO-SUB%20id%3D%22lingo-sub-1509389%22%20slang%3D%22en-US%22%3ERe%3A%20What%20is%20MaxDOP%20controlling%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1509389%22%20slang%3D%22en-US%22%3E%3CP%3EPedro%2C%20Thanks%20for%20a%20great%20article%2C%3C%2FP%3E%3CP%3ESince%20it%20seems%20this%20article%20is%20strictly%20based%20on%20general%20consideration%20and%20strait-forward%20with%20regards%20to%20other%20assumptions%2C%20it%20seems%20this%20is%20based%20on%20having%20SQL%20Server%20directly%20installed%20on%20a%20physical%20server%20(with%20no%20mention%20of%20virtualization)%2C%20then%20I%20would%20like%20to%20ask%2C%20how%20this%20article%20would%20fair%20against%20Windows%20Server%20and%20SQL%20Server%20running%20virtualized%20(VMWare%20or%20Hyper-V)%20and%20already%20have%20hyper-threading%20enabled%20and%20thin-provisioned.%26nbsp%3B%20What%20would%20be%20our%20implications%20on%20NUMA%2C%20soft-NUMA%20and%20general%20considerations%20for%20task%20scheduling%3F%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1509569%22%20slang%3D%22en-US%22%3ERe%3A%20What%20is%20MaxDOP%20controlling%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1509569%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Randy%2C%3C%2FP%3E%0A%3CP%3EThis%20is%20the%20way%20the%20engine%20works%2C%20so%20irrespective%20of%20being%20on%20bare%20metal%2C%20VM%2C%20or%20container.%20Having%20HT%20or%20not%20influences%20how%20many%20schedulers%20are%20seen%20by%20the%20DB%20engine%2C%20as%20does%20CPU%20affinity.%20The%20logic%20described%20here%20accounts%20for%20visible%20schedulers%20only%20(see%20the%20%3CEM%3Estatus%3C%2FEM%3E%20column%20in%20sys.dm_os_schedulers%2C%20usable%20schedulers%20will%20be%20VISIBLE%20ONLINE).%20So%20whether%20HT%20or%20not%2C%20bare%20metal%20or%20VM%2C%20the%20DB%20engine%20sees%20what's%26nbsp%3BVISIBLE%20ONLINE%20and%20works%20with%20that%20number.%20About%20NUMA%2C%20or%20Soft-NUMA%20-%20for%20the%20DB%20engine%2C%20there's%20no%20distinction%20(as%20we%20speak)%20between%20seeing%20a%20HW%20NUMA%20or%20a%20Soft-NUMA%20node.%20After%20Soft-NUMA%20is%20enabled%20and%20nodes%20are%20formed%2C%20the%20same%20logic%20I%20mentioned%20in%20the%20post%20is%20in%20effect%3A%20the%20DB%20Engine%26nbsp%3B%3CSPAN%3Ewill%20always%20try%20to%20assign%20schedulers%20from%20the%20same%20NUMA%20node%20for%20(child)%20task%20execution%2C%20to%20keep%20NUMA%20locality.%20The%20coordinating%20worker%20thread%20may%20be%20placed%20in%20a%20different%20NUMA%20node.%20Regarding%20NUMA%2C%20there%20are%20other%20scalability-affecting%20considerations%20for%20having%20Soft-NUMA%20enabled%20on%20large%20NUMA%20HW%2C%20such%20as%20additional%26nbsp%3BI%2FO%20Completion%20and%20Lazy%20Writer%20threads.%20You%20can%20see%20more%20about%20that%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fsoft-numa-sql-server%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fsoft-numa-sql-server%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1505968%22%20slang%3D%22en-US%22%3EWhat%20is%20MaxDOP%20controlling%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1505968%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20Max%20Degree%20of%20Parallelism%2C%20or%20MAXDOP%2C%20is%20one%20of%20the%20most%20known%20settings%20in%20the%20SQL%20Database%20Engine.%20There%20are%20guidelines%20of%20how%20to%20configure%20it%20that%20have%20intricate%20dependencies%20on%20the%20type%20of%20hardware%20resources%20you%E2%80%99re%20running%20on%2C%20and%20there%20are%20several%20occasions%20where%20someone%20might%20need%20to%20veer%20of%20those%20guidelines%20for%20more%20specialized%20workloads.%3C%2FP%3E%0A%3CP%3EThere%20are%20plenty%20of%20blogs%20on%20these%20topics%2C%20and%20the%20official%20documentation%20does%20a%20good%20job%20of%20explaining%20these%20(in%20my%20opinion).%20If%20you%20want%20to%20know%20more%20about%20the%20guidelines%20and%20ways%20to%20override%20for%20specific%20queries%2C%20refer%20to%20the%20%3CEM%3ERecommendations%3C%2FEM%3E%20section%20in%20the%20%3CA%20style%3D%22background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fconfigure-the-max-degree-of-parallelism-server-configuration-option%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConfigure%20the%20max%20degree%20of%20parallelism%20Server%20Configuration%20Option%3C%2FA%3E%20documentation%20page.%3C%2FP%3E%0A%3CP%3EBut%20what%20does%20MAXDOP%20control%3F%20A%20common%20understanding%20is%20that%20it%20controls%20the%20number%20of%20CPUs%20that%20can%20be%20used%20by%20a%20query%20%E2%80%93%20previous%20revisions%20of%20the%20documentation%20used%20this%20abstraction.%20And%20while%20that%20is%20a%20correct%20abstraction%2C%20it%E2%80%99s%20not%20exactly%20accurate.%20What%20MAXDOP%20really%20controls%20are%20the%20%3CSTRONG%3Enumber%20of%20tasks%20that%20can%20be%20used%20in%20each%20branch%20of%20a%20query%20plan%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EFor%20most%20use%20cases%2C%20talking%20about%20the%20number%20of%20CPUs%20used%20in%20a%20query%2C%20or%20the%20number%20of%20concurrent%20tasks%20scheduled%20won%E2%80%99t%20have%20a%20practical%20difference.%20But%20sometimes%20it%E2%80%99s%20relevant%20to%20know%20the%20full%20story%2C%20to%20go%20beyond%20the%20generalization%2C%20especially%20when%20troubleshooting%20those%20ginormous%20complex%20plans%20that%20involve%20dozens%20of%20operators%20%E2%80%93%20you%20know%20who%20you%20are%26nbsp%3B%E2%98%BA%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1301767122%22%20id%3D%22toc-hId--1301767122%22%3EDefining%20a%20few%20terms%3C%2FH2%3E%0A%3CP%3ESo%20before%20we%20dive%20deeper%20into%20it%2C%20let%E2%80%99s%20make%20sure%20we%E2%80%99re%20all%20on%20the%20same%20page%20regarding%20a%20few%20important%20terms%20that%20will%20be%20used%20in%20this%20post%2C%20and%20you%20find%20in%20documentation.%20These%20my%20simplified%20definitions%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EA%20%3CSTRONG%3Erequest%3C%2FSTRONG%3E%20is%20a%20logical%20representation%20of%20a%20query%20or%20batch%20an%20application%20sends%20to%20the%20Database%20Engine.%20Requests%20can%20be%20monitored%20through%20the%20sys.dm_exec_requests%20DMV%2C%20can%20be%20executed%20in%20parallel%20(multiple%20CPUs)%20or%20in%20a%20serial%20fashion%20(single%20CPU)%2C%20have%20a%20state%20that%20reflects%20the%20state%20of%20underlying%20tasks%2C%20and%20accumulate%20waits%20when%20resources%20needed%20are%20not%20available%20like%20a%20page%20latch%20or%20row%20lock.%3C%2FLI%3E%0A%3CLI%3EA%20%3CSTRONG%3Etask%3C%2FSTRONG%3E%20is%20a%20single%20unit%20of%20work%20that%20needs%20to%20be%20carried%20out%20for%20the%20request%20to%20be%20completed.%20A%20serial%20request%20will%20only%20have%20one%20active%20task%2C%20whereas%20a%20parallel%20request%20will%20have%20multiple%20tasks%20executing%20concurrently.%20Tasks%20can%20be%20monitored%20through%20the%20sys.dm_os_tasks%20and%20sys.dm_os_waiting_tasks%20DMVs%2C%20also%20have%20a%20state%20(running%2C%20runnable%2C%20or%20suspended)%20that%20reflects%20up%20to%20the%20owner%20request%20state.%3C%2FLI%3E%0A%3CLI%3EA%20%3CSTRONG%3Eworker%20thread%3C%2FSTRONG%3E%2C%20a.k.a%20worker%2C%20a.k.a%20thread%20is%20the%20equivalent%20of%20a%20CPU%20thread%20(see%20-ERR%3AREF-NOT-FOUND-Wikipedia).%20I%E2%80%99ll%20use%20these%20terms%20interchangeably.%20Tasks%20that%20need%20to%20be%20executed%20are%20assigned%20to%20a%20worker%2C%20which%20in%20turn%20is%20scheduled%20to%20run%20on%20the%20CPU.%20Workers%20can%20be%20monitored%20through%20the%20sys.dm_os_workers%20DMV.%3C%2FLI%3E%0A%3CLI%3EA%20%3CSTRONG%3Escheduler%3C%2FSTRONG%3E%20is%20the%20logical%20equivalent%20of%20a%20CPU.%20Workers%20are%20scheduled%20to%20actively%20carry%20out%20the%20task%20assigned%20to%20them%2C%20and%20in%20SQL%20Server%20most%20scheduling%20is%20cooperative%2C%20meaning%20a%20worker%20won%E2%80%99t%20cling%20to%20the%20CPU%2C%20but%20instead%20yield%20its%20active%20time%20(called%20the%20quantum%2C%20in%204ms%20chunks)%20to%20another%20worker%20waiting%20to%20execute%20its%20own%20task.%20Schedulers%20can%20be%20monitored%20through%20the%20sys.dm_os_schedulers%20DMV.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EDOP%3C%2FSTRONG%3E%20(Degree%20of%20Parallelism)%20designates%20the%20actual%20number%20of%20schedulers%20assigned%20to%20a%20given%20request%20(more%20accurately%2C%20the%20set%20of%20tasks%20belonging%20to%20a%20request).%3C%2FLI%3E%0A%3CLI%3EThe%20%3CSTRONG%3EMAXDOP%3C%2FSTRONG%3E%20server%20or%20database%20configuration%2C%20as%20well%20as%20the%20MAXDOP%20query%20hint%2C%20determine%20the%20DOP%20ceiling%2C%20the%20maximum%20number%20of%20schedulers%20that%20can%20be%20used%20during%20a%20request%20lifetime.%20It%20doesn%E2%80%99t%20mean%20they%E2%80%99ll%20all%20be%20used.%20For%20example%2C%20in%20a%20very%20busy%20server%2C%20parallel%20queries%20may%20execute%20with%20a%20DOP%20that%E2%80%99s%20lower%20than%20the%20MAXDOP%2C%20if%20that%20number%20of%20schedulers%20is%20simply%20not%20available.%20Hence%20the%20term%20%E2%80%9Cavailable%20DOP%E2%80%9D.%3C%2FLI%3E%0A%3CLI%3EA%20parallel%20query%20plan%20%3CSTRONG%3Ebranch%3C%2FSTRONG%3E.%20If%20you%20think%20of%20a%20query%20plan%20as%20a%20tree%2C%20a%20branch%20is%20an%20area%20of%20the%20plan%20that%20groups%20one%20or%20more%20operators%20between%20Parallelism%20operators%20(a.k.a%20Exchange%20Iterators).%20You%20can%20see%20more%20about%20the%20Parallelism%20operator%20and%20other%20physical%20operators%20in%20the%20-ERR%3AREF-NOT-FOUND-Showplan%20Logical%20and%20Physical%20Operators%20Reference.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-1185745711%22%20id%3D%22toc-hId-1185745711%22%3EBringing%20it%20together%3C%2FH2%3E%0A%3CP%3ELet%E2%80%99s%20get%20on%20with%20the%20example.%20My%20SQL%20Server%20is%20configured%20with%20MAXDOP%208%2C%20CPU%20Affinity%20set%20for%2024%20CPUs%20across%20two%20NUMA%20nodes.%20CPUs%200%20through%2011%20belong%20to%20NUMA%20node%200%2C%20CPUs%2012%20through%2023%20belong%20to%20NUMA%20node%201.%20I%E2%80%99ll%20be%20using%20the%20AdventureWorks2016_EXT%20database%2C%20and%20have%20enlarged%20the%20tables%20in%20the%20query%2050%20fold%2C%20to%20have%20the%20time%20to%20run%20all%20the%20DMV%20queries%20before%20the%20following%20query%20was%20done%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20h.SalesOrderID%2C%20h.OrderDate%2C%20h.DueDate%2C%20h.ShipDate%3CBR%20%2F%3EFROM%20Sales.SalesOrderHeaderBulk%20AS%20h%20%3CBR%20%2F%3EINNER%20JOIN%20Sales.SalesOrderDetailBulk%20AS%20d%20ON%20h.SalesOrderID%20%3D%20d.SalesOrderID%20%3CBR%20%2F%3EWHERE%20(h.OrderDate%20%26gt%3B%3D%20'2014-3-28%2000%3A00%3A00')%3B%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20the%20resulting%20actual%20execution%20plan%2C%20divided%20into%20its%203%20branches%3A%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorPedro%20Lopes_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22ScheduleParallelQry_Plan.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203815iE7529FECEC26A456%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ScheduleParallelQry_Plan.png%22%20alt%3D%22ScheduleParallelQry_Plan.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20example%2C%20looking%20at%20the%20plan%20properties%2C%20we%20can%20see%20more%20information%20about%20how%20many%20threads%20SQL%20Server%20will%20reserve%20to%20execute%20this%20plan%2C%20along%20with%20the%20worker%E2%80%99s%20placement%20on%20the%20NUMA%20nodes%3A%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorPedro%20Lopes_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22ThreadReservation1.png%22%20style%3D%22width%3A%20221px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203816i41733E8A88053C3F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ThreadReservation1.png%22%20alt%3D%22ThreadReservation1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThe%20Database%20Engine%20uses%20information%20about%20the%20plan%20shape%20(which%20allows%20for%202%20concurrent%20branches%20in%20this%20example%20%E2%80%93%20more%20on%20this%20further%20ahead)%20and%20MAXDOP%20configuration%20(which%20is%208)%2C%20to%20figure%20out%20how%20many%20threads%20to%20reserve.%20%3C%2FSPAN%3E%3CSTRONG%20style%3D%22font-family%3A%20inherit%3B%22%3E2%20x%208%20%3D%2016%3C%2FSTRONG%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E.%20So%20MAXDOP%20is%20setting%20overall%20how%20many%20parallel%20threads%20are%20spawned%20for%20the%20query%20(stay%20with%20me).%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20threads%20can%20be%20reserved%20across%20all%20NUMA%20nodes%2C%20or%20be%20reserved%20in%20just%20one%20NUMA%20node%2C%20and%20this%20is%20entirely%20dependent%20on%20scheduler%20load%20at%20the%20moment%20the%20reservation%20is%20made%20at%20runtime.%20In%20this%20case%2C%20the%20reservation%20was%20split%20between%20both%20NUMA%20nodes.%20But%20a%20few%20minutes%20later%2C%20when%20I%20executed%20the%20query%20again%2C%20thread%20reservations%20were%20all%20on%20NUMA%20node%201%2C%20as%20seen%20below%20from%20the%20actual%20execution%20plan%3A%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorPedro%20Lopes_2%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22ThreadReservation2.png%22%20style%3D%22width%3A%20230px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203817i7677EB7433EB193C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ThreadReservation2.png%22%20alt%3D%22ThreadReservation2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBack%20to%20the%201%3CSUP%3Est%3C%2FSUP%3E%20execution%3A%20if%20there%20are%203%20branches%20in%20the%20execution%20plan%2C%20why%20can%20only%202%20branches%20execute%20concurrently%3F%20That%E2%80%99s%20because%20of%20the%20type%20of%20join%20in%20this%20case%2C%20and%20the%20Query%20Processor%20knows%20this.%20A%20hash%20join%20requires%20that%20its%20build%20input%20be%20available%20before%20starting%20to%20generate%20the%20join%20output.%20Therefore%2C%20branches%202%20(build%20input)%20and%203%20(probe%20input)%20can%20be%20executed%20sequentially%20(for%20more%20details%20on%20hash%20joins%2C%20refer%20to%20the%20documentation%20on%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fjoins%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EJoins%3C%2FA%3E).%20Once%20the%20build%20input%20is%20complete%2C%20then%20branch%201%20can%20start.%20Only%20at%20that%20point%2C%20can%20branches%203%20and%201%20be%20executed%20concurrently.%20So%20we%20see%202%20concurrent%20branches.%3C%2FP%3E%0A%3CP%3EThe%20live%20execution%20plan%20gives%20us%20a%20view%20of%20this%20with%20branches%203%20and%201%20in%20flight%2C%20and%202%20completed%3A%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorPedro%20Lopes_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22ScheduleParallelQry_LivePlan.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203818i898DFE2325AE3EEA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ScheduleParallelQry_LivePlan.png%22%20alt%3D%22ScheduleParallelQry_LivePlan.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EOk%2C%20so%20how%20about%20tasks%3F%20Didn%E2%80%99t%20I%20say%20MAXDOP%20limits%20how%20many%20tasks%20are%20spawned%20for%20each%20branch%3F%20Let%E2%80%99s%20query%20the%20sys.dm_os_tasks%20DMV%20and%20find%20out%20what%E2%80%99s%20happening%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20parent_task_address%2C%20task_address%2C%20%26nbsp%3Btask_state%2C%20scheduler_id%2C%20worker_address%3CBR%20%2F%3EFROM%20sys.dm_os_tasks%3CBR%20%2F%3EWHERE%20session_id%20%3D%20100%20--%20my%20session%20ID%3CBR%20%2F%3EORDER%20BY%20parent_task_address%2C%20scheduler_id%3B%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20following%20result%3A%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-center%22%20image-alt%3D%22OsTasksDMV.png%22%20style%3D%22width%3A%20831px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203819i6C8CFC1D1539B5BA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22OsTasksDMV.png%22%20alt%3D%22OsTasksDMV.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENotice%20there%20are%2017%20active%20tasks%3A%2016%20child%20tasks%20corresponding%20to%20the%20reserved%20threads%20(8%20for%20each%20concurrent%20branch)%2C%20and%20the%20coordinating%20task.%20The%20latter%20can%20be%20recognized%20because%20the%20column%20%3CEM%3Eparent_task_address%3C%2FEM%3E%20is%20always%20NULL%20for%20the%20coordinating%20task.%3C%2FP%3E%0A%3CP%3EEach%20of%20the%2016%20child%20tasks%20has%20a%20different%20worker%20assigned%20to%20it%20(%3CEM%3Eworker_address%3C%2FEM%3E%20column)%2C%20but%20notice%20that%20all%2016%20workers%20are%20assigned%20to%20the%20%3CSTRONG%3Esame%20pool%20of%208%20schedulers%3C%2FSTRONG%3E%20(5%2C8%2C10%2C11%2C12%2C18%2C20%2C22)%2C%20and%20that%20the%20coordinating%20task%20is%20assigned%20to%20a%20scheduler%20outside%20this%20pool.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20MAXDOP%20set%20the%20limit%20of%20tasks%20per%20parallel%20branch%2C%208%20in%20this%20case.%20Because%20each%20task%20is%201%3A1%20with%20a%20worker%2C%20and%20each%20worker%20must%20be%20scheduled%20in%20a%20single%20scheduler%20to%20perform%20some%20computing%20work%20%E2%80%93%20%3CSTRONG%3Ethe%20MAXDOP%20configuration%2Fhint%3C%2FSTRONG%3E%26nbsp%3Beffectively%20limits%20the%20number%20of%20CPUs%20used%20in%20the%20query%2C%20just%20not%20directly%20as%20commonly%20perceived.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20fact%20the%20child%20tasks%20are%20on%20the%20same%20exact%20schedulers%20is%20%3CSTRONG%3Eby-design%3C%2FSTRONG%3E%3A%20once%20the%20first%20set%20of%208%20parallel%20tasks%20on%20a%20branch%20was%20scheduled%2C%20every%20additional%20task%20for%20any%20branch%20will%20use%20that%20same%20schedulers.%3C%2FP%3E%0A%3CP%3EThe%20coordinating%20task%20can%20be%20scheduled%20on%20any%20NUMA%20node%2C%20even%20on%20a%20node%20where%20no%20threads%20were%20reserved.%20In%20this%20case%20it%20was%20on%20scheduler%203%2C%20which%20is%20in%20NUMA%20node%200.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20this%20were%20a%20normal%20server%2C%20where%20multiple%20requests%20are%20running%2C%20I'd%20likely%20not%20notice%20that%20a%20given%20parallel%20request%20is%20using%209%20schedulers%20instead%20of%208.%20Because%20the%20scheduler%20associated%20with%20a%20coordinating%20task%20(through%20its%20worker)%20for%20a%20%3CEM%3Erequest%201%3C%2FEM%3E%26nbsp%3Bwould%20be%20a%20scheduler%20used%20for%20parallel%20task%20execution%20in%20a%20%3CEM%3Erequest%202%3C%2FEM%3E%20for%20example.%26nbsp%3BBut%20the%20extra%20scheduler%20for%20%3CEM%3Erequest%201%3C%2FEM%3E%20is%20there%2C%20outside%20the%20scope%20of%20MAXDOP.%3C%2FP%3E%0A%3CP%3EA%20picture%20is%20usually%20worth%20a%20thousand%20words%20so%20here%20it%20is%2C%20my%20SQL%20Server%20conceptually%20displaying%20several%20executions%20of%20the%20example%20query%3A%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-center%22%20image-alt%3D%22Concept.png%22%20style%3D%22width%3A%20700px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F203909iBB878DE35CA67DE9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Concept.png%22%20alt%3D%22Concept.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20%3CSTRONG%3ERequest%201%3C%2FSTRONG%3E%20uses%20schedulers%200%20through%207%20for%20parallel%20task%20execution%2C%20and%20scheduler%208%20for%20the%20coordinating%20task%2C%20which%20is%20also%20used%20by%20Request%202%20for%20parallel%20task%20execution.%20Thread%20reservation%20was%20done%20in%20NUMA%20node%200%20only%2C%20so%20all%20schedulers%20used%20for%20parallel%20tasks%20are%20in%20that%20node.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20%3CSTRONG%3ERequest%202%3C%2FSTRONG%3E%20uses%20schedulers%208%20through%2015%20for%20parallel%20task%20execution%2C%20and%20scheduler%2016%20for%20the%20coordinating%20task%2C%20which%20is%20also%20used%20by%20Request%203%20for%20parallel%20task%20execution.%20Thread%20reservation%20was%20done%20in%20NUMA%20nodes%200%20and%201%2C%20so%20some%20parallel%20tasks%20are%20executed%20in%20node%200%20(schedulers%208%20through%2011)%20and%20some%20on%20node%201%20(schedulers%2012%20through%2015).%3C%2FP%3E%0A%3CP%3E-%20%3CSTRONG%3ERequest%203%3C%2FSTRONG%3E%26nbsp%3Buses%20schedulers%2016%20through%2023%20for%20parallel%20task%20execution%2C%20and%20scheduler%2015%20for%20the%20coordinating%20task%2C%20which%20is%20also%20used%20by%20Request%202%20for%20parallel%20task%20execution.%20Thread%20reservation%20was%20done%20in%20NUMA%20node%201%20only%2C%20so%20all%20schedulers%20used%20for%20parallel%20tasks%20are%20in%20that%20node.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20scenario%2C%20schedulers%208%2C%2015%2C%20and%2016%20are%20used%20by%20a%20coordinating%20task%20from%20one%20request%2C%20and%20a%20parallel%20task%20from%20another%20request.%20If%20there%20were%20no%20bottlenecks%20with%20a%20coordinating%20task%20(which%20can%20do%20other%20work%2C%20but%20for%20simplicity%2C%20let's%20leave%20that%20aside%20now)%2C%20then%20the%20observation%20is%3A%20%223%20requests%20are%20using%208%20schedulers%20each%20(the%20MAXDOP%20number)%2C%20so%20MAXDOP%20is%20controlling%20schedulers%20per%20request.%22%26nbsp%3B%E2%98%BA%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20In%20practice%2C%20we've%20seen%20the%20coordinating%20task%20doesn't%20have%20to%20be%20scheduled%20on%20the%20same%20NUMA%20node.%20And%20while%20the%20%3CU%3Enumber%3C%2FU%3E%26nbsp%3Bof%20schedulers%20used%20for%20the%20parallel%20tasks%20within%20a%20given%20NUMA%20node%20is%20enforced%2C%20their%20%3CU%3Eplacement%3C%2FU%3E%20is%20only%20contiguous%20if%20schedulers%20are%20available%20inside%20a%20given%20NUMA%20node%20(like%20in%20the%20picture)%2C%20but%20the%20concept%20remains.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--621708752%22%20id%3D%22toc-hId--621708752%22%3ESummary%3C%2FH2%3E%0A%3CP%3ESo%2C%20we%E2%80%99ve%20seen%20how%20a%20single%20request%20can%20spawn%20multiple%20tasks%20up%20to%20the%20limit%20set%20by%20reserved%20worker%20threads%20%E2%80%93%2016%20in%20this%20case%20%E2%80%93%20plus%20one%20extra%20coordinating%20task.%20The%20threads%20reserved%20per%20branch%20are%20limited%20by%20MAXDOP%20%E2%80%93%208%20in%20our%20example.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBecause%20%3CSTRONG%3Eeach%20task%20must%20be%20assigned%20to%20a%20worker%20thread%20for%20execution%2C%20each%20worker%20to%20a%20scheduler%2C%20and%20all%20the%20schedulers%20assigned%20to%20parallel%20task%20execution%20are%20from%20the%20same%20pool%3C%2FSTRONG%3E%2C%20hence%20the%20perception%20that%20the%20MAXDOP%20limit%20is%20done%20at%20the%20request%20level%20(the%20query)%2C%20but%20we've%20seen%20it's%20really%20not.%20And%20we%20established%20the%20coordinating%20thread%20is%20not%20part%20of%20the%20calculation%20where%20MAXDOP%20is%20used%2C%20so%20in%20the%20example%20above%2C%20with%20MAXDOP%208%2C%20effectively%209%20schedulers%20were%20in%20use%2C%20not%20just%208%20(so%20MAXDOP%20%2B%201).%3C%2FP%3E%0A%3CP%3EAbout%20the%20perception%20of%20MAXDOP%20controlling%20schedulers%20per%20query%20(request)%2C%20we%20can%20call%20it%20observer%20bias.%20The%20practical%20effect%20with%20multiple%20requests%20in-flight%20is%20that%20I%20see%20all%20schedulers%20being%20used%2C%20and%20the%20coordinating%20task%20may%20not%20be%20noticeable%20for%20the%20overwhelming%20majority%20of%20cases.%3C%2FP%3E%0A%3CP%3EBut%20to%20be%20accurate%2C%20we%20now%20know%20the%20MAXDOP%20limit%20is%20enforced%20at%20the%20task%20level%2C%20per%20branch%2C%20not%20at%20the%20request%20level%2C%20and%20that%20for%20parallel%20requests%20there%20will%20always%20be%20an%20extra%20task%20and%20thread%20needed%2C%20and%20therefore%20an%20extra%20scheduler%20in%20use.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EEDIT%20(7%2F13%2F2020)%3C%2FSTRONG%3E%3A%20Does%20this%20mean%20that%20%3CA%20title%3D%22Configure%20the%20max%20degree%20of%20parallelism%20Server%20Configuration%20Option%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fconfigure-the-max-degree-of-parallelism-server-configuration-option%23Recommendations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMAXDOP%20configuration%20recommendations%3C%2FA%3E%20change%3F%20No.%20The%20coordinating%20task%2Fthread%20is%20not%20very%20busy%20for%20the%20overwhelming%20majority%20of%20workloads%2C%20and%20while%20it%20is%20useful%20to%20clarify%20the%20scope%20of%20MAXDOP%2C%20you%20shouldn't%20be%20going%20out%20to%20do%20any%20changes%20to%20your%20SQL%20Servers%20for%20the%20sake%20of%20doing%20changes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20know%20more%2C%20refer%20to%20the%20%3CEM%3ESQL%20Server%20task%20scheduling%3C%2FEM%3E%20section%20in%20the%20-ERR%3AREF-NOT-FOUND-Thread%20and%20Task%20Architecture%20Guide.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EPedro%20Lopes%20(%26nbsp%3B%3C%2FSPAN%3E-ERR%3AREF-NOT-FOUND-%40SQLPedro%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E)%20%E2%80%93%20Principal%20Program%20Manager%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1505968%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThe%20Max%20Degree%20of%20Parallelism%2C%20or%20MAXDOP%2C%20is%20one%20of%20the%20most%20known%20settings%20in%20the%20SQL%20Database%20Engine.%20There%20are%20guidelines%20of%20how%20to%20configure%20it%20that%20have%20intricate%20dependencies%20on%20the%20type%20of%20hardware%20resources%20you%E2%80%99re%20running%20on%2C%20and%20there%20are%20several%20occasions%20where%20someone%20might%20need%20to%20veer%20of%20those%20guidelines%20for%20more%20specialized%20workloads.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EBut%20what%20does%20MAXDOP%20control%3F%20A%20common%20understanding%20is%20that%20it%20controls%20the%20number%20of%20CPUs%20that%20can%20be%20used%20by%20a%20query%20%E2%80%93%20previous%20revisions%20of%20the%20documentation%20used%20this%20abstraction.%20And%20while%20that%20is%20a%20correct%20abstraction%2C%20it%E2%80%99s%20not%20exactly%20accurate.%20What%20MAXDOP%20really%20controls%20are%20the%20%3CSTRONG%3Enumber%20of%20tasks%20that%20can%20be%20used%20in%20each%20branch%20of%20a%20query%20plan%3C%2FSTRONG%3E%2C%20and%20through%20that%2C%20the%20CPUs%20used%20in%20a%20query.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1505968%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

The Max Degree of Parallelism, or MAXDOP, is one of the most known settings in the SQL Database Engine. There are guidelines of how to configure it that have intricate dependencies on the type of hardware resources you’re running on, and there are several occasions where someone might need to veer of those guidelines for more specialized workloads.

There are plenty of blogs on these topics, and the official documentation does a good job of explaining these (in my opinion). If you want to know more about the guidelines and ways to override for specific queries, refer to the Recommendations section in the Configure the max degree of parallelism Server Configuration Option documentation page.

But what does MAXDOP control? A common understanding is that it controls the number of CPUs that can be used by a query – previous revisions of the documentation used this abstraction. And while that is a correct abstraction, it’s not exactly accurate. What MAXDOP really controls are the number of tasks that can be used in each branch of a query plan.

For most use cases, talking about the number of CPUs used in a query, or the number of concurrent tasks scheduled won’t have a practical difference. But sometimes it’s relevant to know the full story, to go beyond the generalization, especially when troubleshooting those ginormous complex plans that involve dozens of operators – you know who you are ☺

 

Defining a few terms

So before we dive deeper into it, let’s make sure we’re all on the same page regarding a few important terms that will be used in this post, and you find in documentation. These my simplified definitions:

  • A request is a logical representation of a query or batch an application sends to the Database Engine. Requests can be monitored through the sys.dm_exec_requests DMV, can be executed in parallel (multiple CPUs) or in a serial fashion (single CPU), have a state that reflects the state of underlying tasks, and accumulate waits when resources needed are not available like a page latch or row lock.
  • A task is a single unit of work that needs to be carried out for the request to be completed. A serial request will only have one active task, whereas a parallel request will have multiple tasks executing concurrently. Tasks can be monitored through the sys.dm_os_tasks and sys.dm_os_waiting_tasks DMVs, also have a state (running, runnable, or suspended) that reflects up to the owner request state.
  • A worker thread, a.k.a worker, a.k.a thread is the equivalent of a CPU thread (see Wikipedia). I’ll use these terms interchangeably. Tasks that need to be executed are assigned to a worker, which in turn is scheduled to run on the CPU. Workers can be monitored through the sys.dm_os_workers DMV.
  • A scheduler is the logical equivalent of a CPU. Workers are scheduled to actively carry out the task assigned to them, and in SQL Server most scheduling is cooperative, meaning a worker won’t cling to the CPU, but instead yield its active time (called the quantum, in 4ms chunks) to another worker waiting to execute its own task. Schedulers can be monitored through the sys.dm_os_schedulers DMV.
  • DOP (Degree of Parallelism) designates the actual number of schedulers assigned to a given request (more accurately, the set of tasks belonging to a request).
  • The MAXDOP server or database configuration, as well as the MAXDOP query hint, determine the DOP ceiling, the maximum number of schedulers that can be used during a request lifetime. It doesn’t mean they’ll all be used. For example, in a very busy server, parallel queries may execute with a DOP that’s lower than the MAXDOP, if that number of schedulers is simply not available. Hence the term “available DOP”.
  • A parallel query plan branch. If you think of a query plan as a tree, a branch is an area of the plan that groups one or more operators between Parallelism operators (a.k.a Exchange Iterators). You can see more about the Parallelism operator and other physical operators in the Showplan Logical and Physical Operators Reference.

Bringing it together

Let’s get on with the example. My SQL Server is configured with MAXDOP 8, CPU Affinity set for 24 CPUs across two NUMA nodes. CPUs 0 through 11 belong to NUMA node 0, CPUs 12 through 23 belong to NUMA node 1. I’ll be using the AdventureWorks2016_EXT database, and have enlarged the tables in the query 50 fold, to have the time to run all the DMV queries before the following query was done:

 

SELECT h.SalesOrderID, h.OrderDate, h.DueDate, h.ShipDate
FROM Sales.SalesOrderHeaderBulk AS h
INNER JOIN Sales.SalesOrderDetailBulk AS d ON h.SalesOrderID = d.SalesOrderID
WHERE (h.OrderDate >= '2014-3-28 00:00:00');

 

Here is the resulting actual execution plan, divided into its 3 branches:

 

ScheduleParallelQry_Plan.png

 

 

In this example, looking at the plan properties, we can see more information about how many threads SQL Server will reserve to execute this plan, along with the worker’s placement on the NUMA nodes:

 

ThreadReservation1.png

 

The Database Engine uses information about the plan shape (which allows for 2 concurrent branches in this example – more on this further ahead) and MAXDOP configuration (which is 8), to figure out how many threads to reserve. 2 x 8 = 16. So MAXDOP is setting overall how many parallel threads are spawned for the query (stay with me). 

 

The threads can be reserved across all NUMA nodes, or be reserved in just one NUMA node, and this is entirely dependent on scheduler load at the moment the reservation is made at runtime. In this case, the reservation was split between both NUMA nodes. But a few minutes later, when I executed the query again, thread reservations were all on NUMA node 1, as seen below from the actual execution plan:

 

ThreadReservation2.png

 

 

Back to the 1st execution: if there are 3 branches in the execution plan, why can only 2 branches execute concurrently? That’s because of the type of join in this case, and the Query Processor knows this. A hash join requires that its build input be available before starting to generate the join output. Therefore, branches 2 (build input) and 3 (probe input) can be executed sequentially (for more details on hash joins, refer to the documentation on Joins). Once the build input is complete, then branch 1 can start. Only at that point, can branches 3 and 1 be executed concurrently. So we see 2 concurrent branches.

The live execution plan gives us a view of this with branches 3 and 1 in flight, and 2 completed:

 

ScheduleParallelQry_LivePlan.png

 

Ok, so how about tasks? Didn’t I say MAXDOP limits how many tasks are spawned for each branch? Let’s query the sys.dm_os_tasks DMV and find out what’s happening:

 

 

SELECT parent_task_address, task_address,  task_state, scheduler_id, worker_address
FROM sys.dm_os_tasks
WHERE session_id = 100 -- my session ID
ORDER BY parent_task_address, scheduler_id;

 

With the following result:

 

OsTasksDMV.png

 

Notice there are 17 active tasks: 16 child tasks corresponding to the reserved threads (8 for each concurrent branch), and the coordinating task. The latter can be recognized because the column parent_task_address is always NULL for the coordinating task.

Each of the 16 child tasks has a different worker assigned to it (worker_address column), but notice that all 16 workers are assigned to the same pool of 8 schedulers (5,8,10,11,12,18,20,22), and that the coordinating task is assigned to a scheduler outside this pool.

 

So, MAXDOP set the limit of tasks per parallel branch, 8 in this case. Because each task is 1:1 with a worker, and each worker must be scheduled in a single scheduler to perform some computing work – the MAXDOP configuration/hint effectively limits the number of CPUs used in the query, just not directly as commonly perceived.

 

The fact the child tasks are on the same exact schedulers is by-design: once the first set of 8 parallel tasks on a branch was scheduled, every additional task for any branch will use that same schedulers.

The coordinating task can be scheduled on any NUMA node, even on a node where no threads were reserved. In this case it was on scheduler 3, which is in NUMA node 0.

 

If this were a normal server, where multiple requests are running, I'd likely not notice that a given parallel request is using 9 schedulers instead of 8. Because the scheduler associated with a coordinating task (through its worker) for a request 1 would be a scheduler used for parallel task execution in a request 2 for example. But the extra scheduler for request 1 is there, outside the scope of MAXDOP.

A picture is usually worth a thousand words so here it is, my SQL Server conceptually displaying several executions of the example query:

 

Concept.png

 

 

- Request 1 uses schedulers 0 through 7 for parallel task execution, and scheduler 8 for the coordinating task, which is also used by Request 2 for parallel task execution. Thread reservation was done in NUMA node 0 only, so all schedulers used for parallel tasks are in that node.  

- Request 2 uses schedulers 8 through 15 for parallel task execution, and scheduler 16 for the coordinating task, which is also used by Request 3 for parallel task execution. Thread reservation was done in NUMA nodes 0 and 1, so some parallel tasks are executed in node 0 (schedulers 8 through 11) and some on node 1 (schedulers 12 through 15).

- Request 3 uses schedulers 16 through 23 for parallel task execution, and scheduler 15 for the coordinating task, which is also used by Request 2 for parallel task execution. Thread reservation was done in NUMA node 1 only, so all schedulers used for parallel tasks are in that node.  

 

In this scenario, schedulers 8, 15, and 16 are used by a coordinating task from one request, and a parallel task from another request. If there were no bottlenecks with a coordinating task (which can do other work, but for simplicity, let's leave that aside now), then the observation is: "3 requests are using 8 schedulers each (the MAXDOP number), so MAXDOP is controlling schedulers per request." ☺

 

Note: In practice, we've seen the coordinating task doesn't have to be scheduled on the same NUMA node. And while the number of schedulers used for the parallel tasks within a given NUMA node is enforced, their placement is only contiguous if schedulers are available inside a given NUMA node (like in the picture), but the concept remains. 

 

Summary

So, we’ve seen how a single request can spawn multiple tasks up to the limit set by reserved worker threads – 16 in this case – plus one extra coordinating task. The threads reserved per branch are limited by MAXDOP – 8 in our example. 

Because each task must be assigned to a worker thread for execution, each worker to a scheduler, and all the schedulers assigned to parallel task execution are from the same pool, hence the perception that the MAXDOP limit is done at the request level (the query), but we've seen it's really not. And we established the coordinating thread is not part of the calculation where MAXDOP is used, so in the example above, with MAXDOP 8, effectively 9 schedulers were in use, not just 8 (so MAXDOP + 1).

About the perception of MAXDOP controlling schedulers per query (request), we can call it observer bias. The practical effect with multiple requests in-flight is that I see all schedulers being used, and the coordinating task may not be noticeable for the overwhelming majority of cases.

But to be accurate, we now know the MAXDOP limit is enforced at the task level, per branch, not at the request level, and that for parallel requests there will always be an extra task and thread needed, and therefore an extra scheduler in use. 

 

EDIT (7/13/2020): Does this mean that MAXDOP configuration recommendations change? No. The coordinating task/thread is not very busy for the overwhelming majority of workloads, and while it is useful to clarify the scope of MAXDOP, you shouldn't be going out to do any changes to your SQL Servers for the sake of doing changes.

 

If you want to know more, refer to the SQL Server task scheduling section in the Thread and Task Architecture Guide.

 

Pedro Lopes ( @SQLPedro ) – Principal Program Manager

 

2 Comments
Occasional Visitor

Pedro, Thanks for a great article,

Since it seems this article is strictly based on general consideration and strait-forward with regards to other assumptions, it seems this is based on having SQL Server directly installed on a physical server (with no mention of virtualization), then I would like to ask, how this article would fair against Windows Server and SQL Server running virtualized (VMWare or Hyper-V) and already have hyper-threading enabled and thin-provisioned.  What would be our implications on NUMA, soft-NUMA and general considerations for task scheduling?   

Microsoft

Hi Randy,

This is the way the engine works, so irrespective of being on bare metal, VM, or container. Having HT or not influences how many schedulers are seen by the DB engine, as does CPU affinity. The logic described here accounts for visible schedulers only (see the status column in sys.dm_os_schedulers, usable schedulers will be VISIBLE ONLINE). So whether HT or not, bare metal or VM, the DB engine sees what's VISIBLE ONLINE and works with that number. About NUMA, or Soft-NUMA - for the DB engine, there's no distinction (as we speak) between seeing a HW NUMA or a Soft-NUMA node. After Soft-NUMA is enabled and nodes are formed, the same logic I mentioned in the post is in effect: the DB Engine will always try to assign schedulers from the same NUMA node for (child) task execution, to keep NUMA locality. The coordinating worker thread may be placed in a different NUMA node. Regarding NUMA, there are other scalability-affecting considerations for having Soft-NUMA enabled on large NUMA HW, such as additional I/O Completion and Lazy Writer threads. You can see more about that here: https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/soft-numa-sql-server