Inside the Database Engine SQL Saturday - Memphis Inside the Database Engine John Deardurff Website: SQLMCT.com Twitter: @SQLMCT Email: John@Deardurff.com October 5th, 2019
What does this session cover? What is the Database Engine? A Closer look at SQLOS Waits and Threads The Relational Engine The PROCESS of a Query The Storage Engine How Data is Accessed
Relational Engine SQLOS Storage Engine Query Process Relational Engine Resource Access SQLOS Storage Engine Data Access The Database Engine
Transaction Management SQLOS Provides Many Functions Thread Scheduling Memory Management Transaction Management Exception Handling Locking Framework Deadlock Detection
Preemptive vs. Non-Preemptive Scheduling 10987B 1: SQL Server Architecture, Scheduling, and Waits Preemptive vs. Non-Preemptive Scheduling Is driven by the view of prioritized computation Means that a low-priority process is pre-empted out of the processor by a high-priority process Windows uses Preemptive scheduling Means that the priority of a process doesn’t matter Is where a process is not preempted, and executes until it explicitly yields the processor SQL Server uses Non-Preemptive scheduling Try not to spend too much time here. Content in later slides is more important. Technically, Resource Governor enables some processes to be treated preemptively and take priority over a process that is running in a lower-priority resource group. However, discussing this at this point is unlikely to be helpful to students who are new to the topic. Students who have some experience of Resource Governor might raise the point.
Thread States and Queues 10987B 1: SQL Server Architecture, Scheduling, and Waits Thread States and Queues Runnable: The thread is currently in the Runnable Queue waiting to execute. (First In, First Out). Running: One active thread executing on a processor. Suspended: Placed on a Waiter List waiting for a resource other than a processor. (No specific order).
SQL Server Execution Model 10987B SQL Server Execution Model 1: SQL Server Architecture, Scheduling, and Waits Start End CPU available Quantum exhausted Resource available Resource blocked Complete Runnable List Status: runnable CPU Status: running Waiter List Status: suspended It’s worth spending some time on this slide to make sure that the students grasp these concepts because an understanding of them is critical for the rest of this course. Try to avoid discussing wait types here because the next lesson covers the topic in detail.
Dynamic Management Objects for Waits Active wait duration and type: sys.dm_os_waiting_tasks Session CPU time and memory usage: sys.dm_exec_sessions Status and session owner of request: sys.dm_exec_requests Actual query text of waiting task: sys.dm_exec_sql_text
Dynamic Management Objects for Waits 1: SQL Server Architecture, Scheduling, and Waits Dynamic Management Objects for Waits
Troubleshooting Wait Types 1: SQL Server Architecture, Scheduling, and Waits Troubleshooting Wait Types Aaron Bertrand – Top Wait Types https://sqlperformance.com/2018/10/sql-performance/top-wait-stats Paul Randal – SQL Skills Wait Types Library https://www.sqlskills.com/help/waits/
Relational Engine SQLOS Storage Engine Query Process Relational Engine Resource Access SQLOS Storage Engine Data Access The Database Engine
Parse Resolve Optimize Compile Compile Execute Execute Execute Execute Ad Hoc Query Stored Procedure First Execution or Recompile Syntax Parse Execution Context Resolve (Binding) Estimated Compile Optimize Execution Plan Procedure Cache Compile Compile Actual Run Time Execute Execute Execute Execute SQL SQL Sets Sets
How Data Is Stored in SQL Server 20462C 3: Working with Databases and Storage How Data Is Stored in SQL Server Data Row Free Space Extent: Eight contiguous 8kb pages Provide an overview of how the data is stored on SQL Server. Describe each of the three data file types and the basic internal layout of the data files. Stress that the file extension .mdf is not mandatory to use but is highly recommended. Describe the difference between uniform and mixed extents. Note that continued support for mixed extents adds complexity to the database engine for no real benefit. The size of an extent (64 KB) is so small that the original benefit from the mixed extent design is now irrelevant. Briefly explain that all transactions are written to the log file using the WAL mechanism to ensure the integrity of the database in case of a failure and to support rollbacks of transactions. Explain that data changes occur in the buffer pool and are not written immediately to the data files. Avoid discussing filegroups at this point, as they will be covered later. Primary Data File (.mdf) Secondary Data File (.ndf)
Rows stored in Data Pages Page Header (96 bytes) Header contains Page Information such a Page Number and Page Type Data Row 1 Data Row 2 Data Row 3 8060 bytes for Data Rows or Free Space Free Space Row Offset Array Displays how far from beginning of page each row is located.
DBCC IND
DBCC PAGE