First published on MSDN on Dec 13, 2012
With this meditation I attempt to explain what some of the more common concepts that get used with SQL Server thread management and scheduling are.
Parable: There was an all-powerful, but humble and benign Master, whom the workers revered and humbly served. The master accepted requests from other kingdoms and graciously agreed to grant all of them. To do so the Master assigned tasks to his workers (servants) who completed them cooperating with each – allowing each other graciously to approach the Master one at a time.
Scheduler (SOS Scheduler)– the object that manages thread scheduling in SQL Server and allows threads to be exposed to the CPU (described in sys.dm_os_schedulers ). This is the all-powerful but benign and graceful master whom everyone abides. He does not control things but lets the workers work with each other and relies on their cooperation (co-operative scheduling mode). Each scheduler /master (one per logical CPU) accepts new tasks and hands them off to workers. SOS Scheduler allows one worker at a time to be exposed to the CPU.
Task –a task represents the work that needs to be performed ( sys.dm_os_tasks ). A task contains one of the following events: query (RPC event or Language event), a prelogin (prelogin event), a login (connect event), a logout (disconnect event), a query cancellation (an Attention event), a bulk load (bulk load event), a distributed transaction (transaction manager event). A task is what the Master is about – it is what defines its existence. Note these are tracked at the SOS scheduler layer (thus dm_OS_tasks)
Worker (worker thread) – This is the logical SQL Server representation of a thread (think of it as a wrapper on top of the OS thread). It is a structure within the Scheduler which maintains SQL Server-specific information about what a worker thread does. sys.dm_os_workers . Workers are the humble servants who carry out the task assigned to them by the Master (scheduler).
Thread – this is the OS thread sys.dm_os_threads that is created via calls like CreateThread() / _beginthreadex() . A Worker is mapped 1-to-1 to a Thread.
Request is the logical representation of a query request made from the client application to SQL Server ( sys.dm_exec_requests ). This query request has been assigned to a task that the scheduler hands off to a worker to process. This represents query requests as well as system thread operations (like checkpoint, log writer, etc); you will not find login, logouts, attentions and the like here. Also, note that this is a representation at the SQL execution engine level (thus dm_EXEC_requests) not at the SOS Scheduler layer.
Sessions – when the client application connects to SQL Server the two sides establish a "session" on which to exchange information. Strictly speaking a session is not the same as the underlying physical connection, it is a SQL Server logical representation of a connection. But for practical purposes, you can think of this as being a connection (session =~ connection), see sys.dm_exec_sessions . This is the old SPID (session process id) that existed in SQL Server 2000 and earlier. In the case of system sessions (internal sessions spawned by SQL Server like LazyWriter, Checkpoint, Log Writer, Service Broker, etc), no external physical connection is mapped to the session. Only a session_id exists. Typically reserved for session IDs < 50, but can be a higher value.
You may sometimes notice a single session repeating multiple times in a DMV output. This happens because of parallel queries. A parallel query uses the same session to communicate with the client, but on the SQL Server side multiple worker threads are assigned to service this query request. So if you see multiple rows with the same session ID, know that the query request is being serviced by multiple threads. A session_id (SPID) is used to identify the work performed inside SQL Server. For example, you may want to find out which session_id is executing a query, or which session_id has its query cancelled. Some of the properties of a sesssion include login time, last request (query) time, CPU consumed , memory used, and total elapsed time used by a query or set of queries on this session, user name, SET options configured for this session.
Connections – this is the actual physical connection established at the lower protocol level with all of its characteristics sys.dm_exec_connections . There is a 1:1 mapping between a Session and a Connection. A connection has some of the following properties - protocol (Shared Memory, TCP, Named Pipes), authentication type (NTLM, Kerberos), Encryption (on or off), Network packet size, client IP address and port. These are all physical properties of the connection. A connection_id in sys.dm_exec_connections is a GUID and is used to uniquely identify that physical connection. You typically won't use a connection_id to identify which session is executing a query; a session_id is used instead.
A client application creates a physical connection to SQL Server. Then the application sends a pre-login request and a task is created and assigned to a worker to fulfill. Once the server and client finish the pre-login process, a login request is sent and another task is formed and handed off to a worker thread. Once the login is completed, SQL Server creates a session that represents this logical connection where it will exchange information with the client. When the client application sends a query request (or DTC or bulk load), the server again creates a task and assigns it to a worker thread for completion. If the query is cancelled in the middle of execution, for some reason, the server will receive an Attention request upon which the IOCP listener will mark a bit that the query is cancelled and the worker that was running the query would stop executing when it sees the bit. If the query is allowed to complete, on the other hand, and the client application is done, it can send a disconnect or logout request which again is packaged as a task and serviced by a worker.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.