SlideShare uma empresa Scribd logo
1 de 42
Parallelism in SQL Server
Enrique Catala Bañuls
Mentor, SolidQ
ecatala@solidq.com
Twitter: @enriquecatala
Enrique Catala Bañuls

 Computer engineer
 Mentor at SolidQ in the relational engine
  team
 Microsoft Technical Ranger
 Microsoft Active Professional since 2010
 Microsoft Certified Trainer
Our Sponsors:
Volunteers:
 They spend their FREE time to give you this
  event. (2 months per person)
 Because they are crazy.
 Because they want YOU
      to learn from the BEST IN THE WORLD.
 If you see a guy with “STAFF” on their back –
  buy them a beer, they deserve it.
Paulo Matos:
Paulo Borges:
João Fialho:
Bruno Basto:
Objectives of this session

    Basics on parallelism
    Settings to adjust parallelism
    Exchange operators
    Enemies of the parallelism
    Best practices




9 | 3/20/2013 |
Parallelism

 “Parallelism is the action of executing a
  single task across several CPUs”
 It enhances performance taking advance of
  newest HW configurations
Parallelism benefits
 SQL Server uses all CPU by default
 Generally the queries that qualify for parallelism are
  high IO queries
SMP

   Symmetric multiprocessing (SMP) system
   All the CPUs share the same main memory
   No hardware partitioning for memory access
   Typically used in smaller computers

                      SMP architecture
            CPU CPU CPU CPU CPU CPU CPU CPU

                       System bus
                                    CPU       CPU
                           F
              Main
                           S
             Memory     Memory
                           B
                                    CPU       CPU
NUMA

 Non-Uniform Memory Access
 Nodes connected by shared bus, cross-bar,
  ring
 Typically used in high-end computers

   CPU CPU CPU CPU     CPU CPU CPU CPU     CPU CPU CPU CPU     CPU CPU CPU CPU

     Memory              Memory              Memory              Memory
    Controller          Controller          Controller          Controller

     Node Controller     Node Controller     Node Controller     Node Controller


                                     Shared Bus
NUMA

 SQL Server is NUMA aware
   Automatically detects NUMA configuration
 Minimizes the memory latency by using local
  memory in each node
 SQL Server must be properly configured to
  gain the best performance in NUMA systems
SQL Server Execution Model
  SQLOS
               SQLOS creates a scheduler for
Memory Node
                each logical CPU
               A scheduler is like a logical
CPU Node        CPU used by SQL Server
 Scheduler
                workers
               Only one worker can be executed
  Worker        by a scheduler at the same time
               The unit of work for a worker is a
   Task
                task
Schedulers and concurrency

 Pre-emptive scheduler (Windows)
    Windows uses pre-emptive scheduling because of its general
     operating system nature
    It uses a priority-driven architecture
    Each thread executes in a predetermined time slice
    A thread can be preempted by a higher priority thread
 Cooperative scheduler (SQL Server)
    Each task puts itself in the waiting list every time it needs a
     resource
    The same scheduler executes until the end
    This voluntary yielding by workers prevents context switching
     and improves performance
Objectives of this session

     Basics on parallelism
     Settings to adjust parallelism
     Exchange operators
     Enemies of the parallelism
     Best practices




17 | 3/20/2013 |
Settings to adjust parallelism
 Hardware level
    NUMA
 Instance level
      Soft-NUMA (affinity mask)
      Degree of parallelism
      Cost threshold for parallelism
      Max worker threads
      -P parameter
 Connection level
    Resource Governor by configuring MAXDOP
 Query level
    MAXDOP clause
    T-SQL patterns
         CROSS APPLY
         Functions…
CPU Affinity Mask
• Used to set which processor(s) can be used by the SQL
  Server instance.
• Setting a processor affinity will tie the threads to a particular
  processor
Affinity I/O Mask

 Used to affinitize the CPU usage to I/O
  operations
 Each I/O operation needs to be finalized
   Byte checksum, number of transferred bytes,
    page number okay, etc.
   CPU consumption
 Can be used to specify the lazy writer (in a
  new hidden scheduler)
         Bad                Good
Network affinity

                    8000


                   8001



                   8002




                    8003
Threshold for parallelism

 Instance level configuration
 Change statistically the parallel execution
   Changes the boundaries of when a serial plan should be
    changed to parallel plan


 if(best_plan_for_now.cost<1) return(best_plan_for_now)
 else if(MAXDOP>0
        and best_plan.cost > threshold for parallelism)
 return(MIN(create_paralel_plan().cost, best_plan_for_now))
Demonstration 1




  Affinity mask, cost threshold for
             parallelism
Degree of parallelism (DOP)

 Max degree of parallelism
   o Instance setting that affects the whole instance
   o Can be configured at resource governor´s
     workload level
   o Enforces the maximum number of CPUs that a
     single worker can use
 MAXDOP hint
   o Can be used at query level
Demonstration 2




             MAXDOP
Objectives of this session

     Basics on parallelism
     Settings to adjust parallelism
     Exchange operators
     Enemies of the parallelism
     Best practices




26 | 3/20/2013 |
Exchange operators

 Operators dedicated to moving rows between
  one or more workers, distributing individual
  rows among them
Distribute streams operator
 Row distribution based on
    Hash
        Each row computed a hash and each thread Works only with the rows that have
         the same hash
    Round-robin
        Each row is sent to the following thread of a round-robin
    Broadcast
        All rows are sent to all threads
    Range
        Each row is sent to a thread based on a range computation over a column
        Rare and used in some parallel index creation operations
    Demand
        Pull mode
        It SENDS the row to the operator is calling
        It appears on partitioned tables
Repartition streams operator

 Takes rows from multiple sources and send rows
  to multiple destinations
 Doesn´t update any row
Gather streams operator

 It takes rows from multiple sources and send
  to a single destination (thread)
 Tipically increases CXPACKETS
Demonstration 3




           OPERATORS
Objectives of this session

     Basics on parallelism
     Settings to adjust parallelism
     Exchange operators
     Enemies of the parallelism
     Best practices




32 | 3/20/2013 |
Enemies of the parallelism
 Makes the whole plan serial
    Modifying the contents of a table variable (reading is fine)
    Any T-SQL scalar function
    CLR scalar functions marked as performing data access (normal ones
     are fine)
    Random intrinsic functions including OBJECT_NAME,
     ENCYPTBYCERT, and IDENT_CURRENT
    System table access (e.g. sys.tables)
 Serial zones
      TOP
      Sequence project (e.g. ROW_NUMBER, RANK)
      Multi-statement T-SQL table-valued functions
      Backward range scans (forward is fine)
      Global scalar aggregates
      Common sub-expression spools
      Recursive CTEs
Demonstration 4




 ENEMIES OF THE PARALLELISM
CXPACKET
Serial     Parallel   Serial
Demonstration 5




            CXPACKET
Objectives of this session

     Basics on parallelism
     Settings to adjust parallelism
     Exchange operators
     Enemies of the parallelism
     Best practices




37 | 3/20/2013 |
Best practices
 Never trust the default configuration for the
  degree of parallelism
    By default, MAXDOP = 0
 As a general rule
    Pure OLTP should use MAXDOP = 1
    MAXDOP not to exceed the number of physical cores
    If NUMA architecture,
     MAXDOP <= #physical_cores_numa_node

                    wait type name          wait time (ms) requests
                    CXPACKET                       786556034 128110444
                    LATCH_EX                       255701441 155553913
                    ASYNC_NETWORK_IO               129888217 19083082
                    PAGEIOLATCH_SH                  83672746   2813207
                    WRITELOG                        70634742 48398646
                    SOS_SCHEDULER_YIELD             47697175 176871743
Best practices

 When to apply MAXDOP?
   ALTER INDEX operations
   Typically set MAXDOP = #_physical_cores
 When to set max degree of parallelism?
   When you see high CXPACKET waits
   OLTP pure systems should set its value to 1
 When to set cost threshold for parallelism?
   When you want to change the number of parallel
    operations statistically
Objectives of this session

     Basics on parallelism
     Settings to adjust parallelism
     Exchange operators
     Enemies of the parallelism
     Best practices




41 | 3/20/2013 |
Thank you!
Parallelism in SQL Server
Enrique Catala Bañuls
Mentor, SolidQ
ecatala@solidq.com
Twitter: @enriquecatala

Mais conteúdo relacionado

Mais procurados

Los modelos de datos y el modelo objeto relacional
Los modelos de datos y el modelo objeto relacionalLos modelos de datos y el modelo objeto relacional
Los modelos de datos y el modelo objeto relacional
omarib
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
emailharmeet
 
Database, 3 Distribution Design
Database, 3 Distribution DesignDatabase, 3 Distribution Design
Database, 3 Distribution Design
Ali Usman
 
Network Attached Storage (NAS)
Network Attached Storage (NAS)Network Attached Storage (NAS)
Network Attached Storage (NAS)
sandeepgodfather
 
Types of databases
Types of databasesTypes of databases
Types of databases
PAQUIAAIZEL
 

Mais procurados (20)

Dbms schema &amp; instance
Dbms schema &amp; instanceDbms schema &amp; instance
Dbms schema &amp; instance
 
Los modelos de datos y el modelo objeto relacional
Los modelos de datos y el modelo objeto relacionalLos modelos de datos y el modelo objeto relacional
Los modelos de datos y el modelo objeto relacional
 
DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)
 
Relational database
Relational database Relational database
Relational database
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
 
The NFS Version 4 Protocol
The NFS Version 4 ProtocolThe NFS Version 4 Protocol
The NFS Version 4 Protocol
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
What is difference between dbms and rdbms
What is difference between dbms and rdbmsWhat is difference between dbms and rdbms
What is difference between dbms and rdbms
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Caching
CachingCaching
Caching
 
Database, 3 Distribution Design
Database, 3 Distribution DesignDatabase, 3 Distribution Design
Database, 3 Distribution Design
 
Network Attached Storage (NAS)
Network Attached Storage (NAS)Network Attached Storage (NAS)
Network Attached Storage (NAS)
 
CDW: SAN vs. NAS
CDW: SAN vs. NASCDW: SAN vs. NAS
CDW: SAN vs. NAS
 
Performance Considerations in Logical Data Warehouse
Performance Considerations in Logical Data WarehousePerformance Considerations in Logical Data Warehouse
Performance Considerations in Logical Data Warehouse
 
Types of databases
Types of databasesTypes of databases
Types of databases
 
Raid Technology
Raid TechnologyRaid Technology
Raid Technology
 
Codds rules & keys
Codds rules & keysCodds rules & keys
Codds rules & keys
 
Diccionario de datos
Diccionario de datosDiccionario de datos
Diccionario de datos
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
 
Real time data integration best practices and architecture
Real time data integration best practices and architectureReal time data integration best practices and architecture
Real time data integration best practices and architecture
 

Semelhante a Parallelism in sql server

Semelhante a Parallelism in sql server (20)

CPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performanceCPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performance
 
Concurrent Replication of Parallel and Distributed Simulations
Concurrent Replication of Parallel and Distributed SimulationsConcurrent Replication of Parallel and Distributed Simulations
Concurrent Replication of Parallel and Distributed Simulations
 
Your Linux AMI: Optimization and Performance (CPN302) | AWS re:Invent 2013
Your Linux AMI: Optimization and Performance (CPN302) | AWS re:Invent 2013Your Linux AMI: Optimization and Performance (CPN302) | AWS re:Invent 2013
Your Linux AMI: Optimization and Performance (CPN302) | AWS re:Invent 2013
 
Migration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming ModelsMigration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming Models
 
Parallel Processing (Part 2)
Parallel Processing (Part 2)Parallel Processing (Part 2)
Parallel Processing (Part 2)
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Assisting User’s Transition to Titan’s Accelerated Architecture
Assisting User’s Transition to Titan’s Accelerated ArchitectureAssisting User’s Transition to Titan’s Accelerated Architecture
Assisting User’s Transition to Titan’s Accelerated Architecture
 
Balancing Power & Performance Webinar
Balancing Power & Performance WebinarBalancing Power & Performance Webinar
Balancing Power & Performance Webinar
 
Approximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithmsApproximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithms
 
3rd 3DDRESD: ReCPU 4 NIDS
3rd 3DDRESD: ReCPU 4 NIDS3rd 3DDRESD: ReCPU 4 NIDS
3rd 3DDRESD: ReCPU 4 NIDS
 
Deep Dive on Amazon EC2
Deep Dive on Amazon EC2Deep Dive on Amazon EC2
Deep Dive on Amazon EC2
 
Cc module 3.pptx
Cc module 3.pptxCc module 3.pptx
Cc module 3.pptx
 
Nbvtalkataitamimageprocessingconf
NbvtalkataitamimageprocessingconfNbvtalkataitamimageprocessingconf
Nbvtalkataitamimageprocessingconf
 
Real-Time Analytics with Kafka, Cassandra and Storm
Real-Time Analytics with Kafka, Cassandra and StormReal-Time Analytics with Kafka, Cassandra and Storm
Real-Time Analytics with Kafka, Cassandra and Storm
 
Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)
 
Fast switching of threads between cores - Advanced Operating Systems
Fast switching of threads between cores - Advanced Operating SystemsFast switching of threads between cores - Advanced Operating Systems
Fast switching of threads between cores - Advanced Operating Systems
 
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
 
No sql
No sqlNo sql
No sql
 
Low Latency Execution For Apache Spark
Low Latency Execution For Apache SparkLow Latency Execution For Apache Spark
Low Latency Execution For Apache Spark
 
Routing simulator
Routing simulatorRouting simulator
Routing simulator
 

Mais de Enrique Catala Bañuls

Mais de Enrique Catala Bañuls (20)

Sql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql serverSql server ha muerto, larga vida a sql server
Sql server ha muerto, larga vida a sql server
 
Capas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL ServerCapas de acceso a datos .net escalables de verdad contra SQL Server
Capas de acceso a datos .net escalables de verdad contra SQL Server
 
Paralelismo en SQL Server
Paralelismo en SQL ServerParalelismo en SQL Server
Paralelismo en SQL Server
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidor
 
Técnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql serverTécnicas avanzadas para resolver tus problemas de sql server
Técnicas avanzadas para resolver tus problemas de sql server
 
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
Capas de acceso a datos .NET escalables de verdad: el batido perfecto para el...
 
Planes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v nextPlanes de ejecución 3.0 sql 2016 y v next
Planes de ejecución 3.0 sql 2016 y v next
 
Paralelismo en sql server
Paralelismo en sql serverParalelismo en sql server
Paralelismo en sql server
 
Aplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidorAplicando R al análisis de rendimiento de un servidor
Aplicando R al análisis de rendimiento de un servidor
 
Query store
Query storeQuery store
Query store
 
Planes de ejecucion 2016
Planes de ejecucion 2016Planes de ejecucion 2016
Planes de ejecucion 2016
 
Sql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladoresSql server 2016 novedades para desarrolladores
Sql server 2016 novedades para desarrolladores
 
Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)Dawarehouse como servicio en azure (sqldw)
Dawarehouse como servicio en azure (sqldw)
 
Query store
Query storeQuery store
Query store
 
Planes de ejecucion 2
Planes de ejecucion 2Planes de ejecucion 2
Planes de ejecucion 2
 
Planes de ejecucion 1
Planes de ejecucion 1Planes de ejecucion 1
Planes de ejecucion 1
 
Migración a sql server 2016
Migración a sql server 2016Migración a sql server 2016
Migración a sql server 2016
 
Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)Datawarehouse como servicio en azure (sqldw)
Datawarehouse como servicio en azure (sqldw)
 
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
Como hacer tuning a capas de acceso a datos en .NET (dotNetConference2016)
 
Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015Como leer planes de ejecución - edición 2015
Como leer planes de ejecución - edición 2015
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Parallelism in sql server

  • 1. Parallelism in SQL Server Enrique Catala Bañuls Mentor, SolidQ ecatala@solidq.com Twitter: @enriquecatala
  • 2. Enrique Catala Bañuls  Computer engineer  Mentor at SolidQ in the relational engine team  Microsoft Technical Ranger  Microsoft Active Professional since 2010  Microsoft Certified Trainer
  • 4. Volunteers:  They spend their FREE time to give you this event. (2 months per person)  Because they are crazy.  Because they want YOU to learn from the BEST IN THE WORLD.  If you see a guy with “STAFF” on their back – buy them a beer, they deserve it.
  • 9. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 9 | 3/20/2013 |
  • 10. Parallelism  “Parallelism is the action of executing a single task across several CPUs”  It enhances performance taking advance of newest HW configurations
  • 11. Parallelism benefits  SQL Server uses all CPU by default  Generally the queries that qualify for parallelism are high IO queries
  • 12. SMP  Symmetric multiprocessing (SMP) system  All the CPUs share the same main memory  No hardware partitioning for memory access  Typically used in smaller computers SMP architecture CPU CPU CPU CPU CPU CPU CPU CPU System bus CPU CPU F Main S Memory Memory B CPU CPU
  • 13. NUMA  Non-Uniform Memory Access  Nodes connected by shared bus, cross-bar, ring  Typically used in high-end computers CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU CPU Memory Memory Memory Memory Controller Controller Controller Controller Node Controller Node Controller Node Controller Node Controller Shared Bus
  • 14. NUMA  SQL Server is NUMA aware  Automatically detects NUMA configuration  Minimizes the memory latency by using local memory in each node  SQL Server must be properly configured to gain the best performance in NUMA systems
  • 15. SQL Server Execution Model SQLOS  SQLOS creates a scheduler for Memory Node each logical CPU  A scheduler is like a logical CPU Node CPU used by SQL Server Scheduler workers  Only one worker can be executed Worker by a scheduler at the same time  The unit of work for a worker is a Task task
  • 16. Schedulers and concurrency  Pre-emptive scheduler (Windows)  Windows uses pre-emptive scheduling because of its general operating system nature  It uses a priority-driven architecture  Each thread executes in a predetermined time slice  A thread can be preempted by a higher priority thread  Cooperative scheduler (SQL Server)  Each task puts itself in the waiting list every time it needs a resource  The same scheduler executes until the end  This voluntary yielding by workers prevents context switching and improves performance
  • 17. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 17 | 3/20/2013 |
  • 18. Settings to adjust parallelism  Hardware level  NUMA  Instance level  Soft-NUMA (affinity mask)  Degree of parallelism  Cost threshold for parallelism  Max worker threads  -P parameter  Connection level  Resource Governor by configuring MAXDOP  Query level  MAXDOP clause  T-SQL patterns  CROSS APPLY  Functions…
  • 19. CPU Affinity Mask • Used to set which processor(s) can be used by the SQL Server instance. • Setting a processor affinity will tie the threads to a particular processor
  • 20. Affinity I/O Mask  Used to affinitize the CPU usage to I/O operations  Each I/O operation needs to be finalized  Byte checksum, number of transferred bytes, page number okay, etc.  CPU consumption  Can be used to specify the lazy writer (in a new hidden scheduler) Bad Good
  • 21. Network affinity 8000 8001 8002 8003
  • 22. Threshold for parallelism  Instance level configuration  Change statistically the parallel execution  Changes the boundaries of when a serial plan should be changed to parallel plan if(best_plan_for_now.cost<1) return(best_plan_for_now) else if(MAXDOP>0 and best_plan.cost > threshold for parallelism) return(MIN(create_paralel_plan().cost, best_plan_for_now))
  • 23. Demonstration 1 Affinity mask, cost threshold for parallelism
  • 24. Degree of parallelism (DOP)  Max degree of parallelism o Instance setting that affects the whole instance o Can be configured at resource governor´s workload level o Enforces the maximum number of CPUs that a single worker can use  MAXDOP hint o Can be used at query level
  • 25. Demonstration 2 MAXDOP
  • 26. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 26 | 3/20/2013 |
  • 27. Exchange operators  Operators dedicated to moving rows between one or more workers, distributing individual rows among them
  • 28. Distribute streams operator  Row distribution based on  Hash  Each row computed a hash and each thread Works only with the rows that have the same hash  Round-robin  Each row is sent to the following thread of a round-robin  Broadcast  All rows are sent to all threads  Range  Each row is sent to a thread based on a range computation over a column  Rare and used in some parallel index creation operations  Demand  Pull mode  It SENDS the row to the operator is calling  It appears on partitioned tables
  • 29. Repartition streams operator  Takes rows from multiple sources and send rows to multiple destinations  Doesn´t update any row
  • 30. Gather streams operator  It takes rows from multiple sources and send to a single destination (thread)  Tipically increases CXPACKETS
  • 31. Demonstration 3 OPERATORS
  • 32. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 32 | 3/20/2013 |
  • 33. Enemies of the parallelism  Makes the whole plan serial  Modifying the contents of a table variable (reading is fine)  Any T-SQL scalar function  CLR scalar functions marked as performing data access (normal ones are fine)  Random intrinsic functions including OBJECT_NAME, ENCYPTBYCERT, and IDENT_CURRENT  System table access (e.g. sys.tables)  Serial zones  TOP  Sequence project (e.g. ROW_NUMBER, RANK)  Multi-statement T-SQL table-valued functions  Backward range scans (forward is fine)  Global scalar aggregates  Common sub-expression spools  Recursive CTEs
  • 34. Demonstration 4 ENEMIES OF THE PARALLELISM
  • 35. CXPACKET Serial Parallel Serial
  • 36. Demonstration 5 CXPACKET
  • 37. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 37 | 3/20/2013 |
  • 38. Best practices  Never trust the default configuration for the degree of parallelism  By default, MAXDOP = 0  As a general rule  Pure OLTP should use MAXDOP = 1  MAXDOP not to exceed the number of physical cores  If NUMA architecture, MAXDOP <= #physical_cores_numa_node wait type name wait time (ms) requests CXPACKET 786556034 128110444 LATCH_EX 255701441 155553913 ASYNC_NETWORK_IO 129888217 19083082 PAGEIOLATCH_SH 83672746 2813207 WRITELOG 70634742 48398646 SOS_SCHEDULER_YIELD 47697175 176871743
  • 39. Best practices  When to apply MAXDOP?  ALTER INDEX operations  Typically set MAXDOP = #_physical_cores  When to set max degree of parallelism?  When you see high CXPACKET waits  OLTP pure systems should set its value to 1  When to set cost threshold for parallelism?  When you want to change the number of parallel operations statistically
  • 40. Objectives of this session  Basics on parallelism  Settings to adjust parallelism  Exchange operators  Enemies of the parallelism  Best practices 41 | 3/20/2013 |
  • 42. Parallelism in SQL Server Enrique Catala Bañuls Mentor, SolidQ ecatala@solidq.com Twitter: @enriquecatala

Notas do Editor

  1. Thereis a lot of topicsonthis área and i tryedto concéntrate some of themostimportantparts in anhoursession
  2. A quickexample:If i have 200 differentcoins and Iwanttogethowmuchmoney Ihave, i can addonebyone , I can give 100 coinstomypartnertoget a partialresult, orforexample Split mycoinsbetween 10 partnerstoget 10 partialresults and thenobtainhowmuchmoney I have … aftersome “specialfee” youknowThe real time expended gettingtheresultswillnot be thesame and obviouslythemuchpartners i use togetpartialresults, the more quicklyi´llgettheresult….butthisisnotalways true.
  3. Typicalbennefit: the more CPU, the more performance…butitsthat true?It´stipicallon REBUILDING indexes, aggregations, tablescans,…CHART, GRAPHIC
  4. The server iscomposedonmultiple NUMA nodes 2-4 typicallyonthe standard configurationsEach NUMA node has itsown CPU and memoryThe server seesthe sum of CPU and memory and all are accesible from SQL Server
  5. The images show the detection of three-node NUMA hardware by SQL Server and the three lazy writer threads (one per each NUMA node).SQL Server is able to get the best performance in NUMA hardware by doing some special automatic configurations, such as having special threads for some internal components in each NUMA node. Note: Mention that as a common rule, you must configure the MAXDOP value lower than the number of physical cores per each NUMA node. With this configuration, if a query is executed in parallel, all the threads will be in the same node.
  6. the SQLOS is a thin user-mode layer that sits between SQL Server and Windows. It is usedfor low-level operations such as scheduling, I/O completion, memory management, and resourceManagementWhen an execution request is made within a session, SQL Server divides the work into one or moretasks and then associates a worker thread to each task for its duration. Runs in user modeReduces context switchingBetter resource usageMultiprocessing is enhancedA task uses the same scheduler most of the timeMultiple tasks can be executed at the same timeData locality is enforcedBetter scalability on NUMA hardwareSQLOS works the same in each OS host (w2k3, w2k8r2, w2k12, etc.)
  7. why would i do that?
  8. This configuration is mainly for:Systems with more than one SQL Server instanceSystems with more than 32 heavily used CPUs on which you detected specific I/O congestion problemsWhen you don&apos;t use IO affinity the SQL Server worker handles (posts) the IO and takes care of the IO completion on the scheduler the worker was assigned to.The SQL Server GUI on SQL Server 2012 don´tletyoumakemistakesQUESTION: Whyiswrongtheconf “Bad”?REASON: By setting both at the same scheduler they will compete for resources, that is just what you want to avoid.
  9. ENHANCE DATA LOCALITYOnlargesystems, bydoingthiskind of affinity, you can obtain a performance gain of 20%. QUESTION: Why?ANSWER: Becausestatistically, when a scheduler “touches” a datapage, the page isstored at NUMA memory X. if a schedulercommingfromanother NUMA nodeneedstoreadthatspecific page, ittakes 3-4 times the time togetthat page fromoutsideits NUMA node. So bydoingthis, we can forcé specificaplicationstoworkwithspecific NUMA nodes and doingthis, toincreasethepossibilitytoread-write data pagesonthesame NUMA nodes.
  10. 26’
  11. Degree of parallelism (DOP) is assigned at each parallel step of the execution planAll CPUs can be used by the schedulers, so threads can use all available CPUsNo special consideration for hyperthreaded CPUsBy limiting DOP, you can limit the number of available threads to solve a query DOP is determined when execution plan is retrieved from the plan cache
  12. 28
  13. This operator takes a single input stream of records and produces multiple output streams. The record contents and format are not changed. Each record from the input stream appears in one of the output streams. This operator automatically preserves the relative order of the input records in the output streams. Usually, hashing is used to decide to which output stream a particular input record belongs.
  14. operator consumes multiple streams and produces multiple streams of records. The record contents and format are not changed. If the query optimizer uses a bitmap filter, the number of rows in the output stream is reduced. Each record from an input stream is placed into one output stream. If this operator is order preserving, all input streams must be ordered and merged into several ordered output streams. El cálculo si se mira el plan de eejcución en detalle, viene dado por una expresión que se puede obtener ya en el hash match. En el momento del gather se obtiene el valor 6.En la demo 2-exchange_operators.sql se puede ver con detenimiento
  15. consumes several input streams and produces a single output stream of records by combining the input streamsPARALLEL PAGE SUPLIER to divide rowsacrossthreads in batches
  16. 45
  17. There are someenemies of theparalleliam and those are some of them
  18. Paralleloperationsmust be synchronizedbeforeserializyng. So ifsomeworkerendsitsexecution and someotherisstillexecuting, he throws a CXPACKET signalto SQL Server announcingthat he iswaiting and finisheditsexecution. CXPACKET isnot a problemitselfbutitsanindicator of badparallel SQL Server configurationifweseelots of waitsignals of thistype
  19. 50
  20. Here are typical scenarios involving CXPACKET wait statistics.Note: It is very unusual to have a pure OLTP system because most customers uses their SQL Server instances for applications, reports, BI data loading solutions, and more.In the example at the bottom, note that 9 days of CPU time is wasted by CXPACKET (786556034 ms = 13109 minutes = 218 hours = 9 days) in threading synchronization due to a bad configuration. (This is a real example from one of the SolidQ’s customers.)Important: It is very important that the students really understand the degree of parallelism setting. It is very common for students to confuse MAXDOP with CPU AFFINITY. Furthermore, make sure that students understand what is a pure OLTP system.