SQL SERVER INTERNALS &
ARCHITECTURE
Kevin Kline, SQL Sentry
Tech Evangelist, SQL Sentry
Microsoft SQL Server MVP since 2003
Twitter , Facebook, LinkedIn @ KEKline
Website: http://blogs.sqlsentry.com/KevinKline
DROPPING ACID - WHY DOES SQL SERVER
DO WHAT IT DOES?
• ACID properties of Transactions
o Atomic
o Consistent
o Isolated
o Durable
• Speed, scalability, and
performance; Maximize hardware
• Competitive features
OUR TOUR GUIDE
Talk nerdy
to me, baby!
OK, WE’RE
DONE
Relational Engine
Optimizer
Query
Executor
Cmd Parser
Storage Engine
Trans-
action
Manager
Buffer
Manager
Access
Methods
Protocol
Layer
SNI
Data
File
T-
Log
Buffer Pool
- - - - - - - - - - - -
Data Cache
- - - - - - - - - - - -
Plan Cache
SQL Server
Network
Interface
TDS
Language
Event
SELECT
?
Query
Tree
Query
Plan
OLE
DB
?
QUERY OPTIMIZATION
Query Processor / Relational Engine
Parser Query Optimizer
Query
Executor
• Optimization is cost-based
o Optimized for worst case scenario: “everything comes from disk”
• The cost numbers may as well be unicorn freckles
o Estimated costs may have no basis in current reality
• Multiple phases (a.ka. “searches”)
o Pre-optimization determines if the plan is “trivial”
o Phase 0: simple plans: e.g. nested loops without parallelism
o Phase 1: quick plans: plans that can be simplified
o Phase 2: full plans: complex queries, parallelism, spills & spools to tempdb
SCHEDULERS, THREADS, AND WAITS
• 1 Cash Register = 1
scheduler
• Users are assigned
to a thread
Uh oh!
Out of
soda!
No problem.
Step aside…
More syrup for
the sodas!
Goes to the waiting, i.e.
“suspended queue”
Yeah! I’m
next in line!
SQL SERVER WAITS
Suspended Scheduler 1
Runnable Scheduler 1
Running Scheduler 1
55 Running
54 CXPACKET
60 LCK_M_S
61 LCK_M_S
53 Runnable
56 Runnable
59 Runnable
Resource Waits
Signal
Waits
52 PAGEIOLATCH_SH
52 Runnable
55 PAGEIOLATCH_SH
53 Running
Relational Engine
Optimizer
Query
Executor
Cmd Parser
Storage Engine
Trans-action
Manager:
Log & Lock
Mgr
Buffer
Manager
Access
Methods
Protocol
Layer
SNI
Data
File(s)
T-
Log
Buffer Pool
- - - - - - - - - - - -
Data Cache
- - - - - - - - - - - -
Plan Cache
SQL Server
Network
Interface
TDS
Language
Event
SQL
OS
?
Query
Tree
Query
Plan
OLE
DB
Data
Write ?
Check
Point
Lazywriter
Latches
Locks
TROUBLE-
SHOOTING WAIT
STATS?
Async_Network_IO
SOS_Scheduler_Yield
Pagelatch_x, Latch_x,
Resource_Semaphore
LCK_x, LCK_M_x
Writelog, Logbuffer
PageIOLatch_x,
Async_IO_Completion,
IO_Completion
MEMORY MANAGER: BUFFER POOL
8
OS
CLR, MPA,
DWA, TS
Buffer Pool
(SPA)
SQL Server 2008 R2 & earlier
Memory
allocations
within
sqlservr
process
space
Max
server
memor
y
OS
DWA, TS
Buffer Pool
(SPA)
SQL Server 2012 & later
Memory
allocations
within
sqlservr
process
space
Max
server
memor
y
Data cache
----------------
Plan cache
----------------
Other caches
Data cache
-~-~-~-~-~-
Plan cache
-~-~-~-~-~-
Other caches
CACHES?
• How long does a page of data or
a block of code stay in cache?
• Uses a LRU algorithm
• Usually performed by the lazy-
writer, but can also be done by
any worker thread after
scheduling its own I/O
PLAN CACHE AGING
get_order
Plan Cache
reset_user
proc11 proc14
1
6
1
6
7
2
2
3
3 0
2
7
1
5
6
4
0
1
1
4
1
5
1
3
1
2
What about
buffer
cache?
BUT WAIT!
THERE’S MORE!
Relational Engine
Optimizer
Query
Executor
Cmd Parser
Storage Engine
Transaction
Manager:
Log & Lock
Mgr
Buffer
Manager
Access
Methods
Protocol
Layer
SNI
Data
File
T-
Log
Buffer Pool
- - - - - - - - - - - -
Data Cache
- - - - - - - - - - - -
Plan Cache
SQL Server
Network
Interface
TDS
Language
Event
INSERT,
UPDATE,
or
DELETE
?
Query
Tree
Query
Plan
OLE
DB
Data
Write ?
Oooh! So
dirty!
CheckPoint
Lazywriter
Relational Engine
Optimizer
Query
Executor
Cmd Parser
Storage Engine
Trans-action
Manager:
Log & Lock
Mgr
Buffer
Manager
Access
Methods
Protocol
Layer
SNI
Data
File(s)
T-
Log
Buffer Pool
- - - - - - - - - - - -
Data Cache
- - - - - - - - - - - -
Plan Cache
SQL Server
Network
Interface
TDS
Language
Event
SQL
OS
?
Query
Tree
Query
Plan
OLE
DB
Data
Write ?
Check
Point
Lazywriter
Latches
Locks
HEKATON, A.K.A. IN-
MEMORY OLTP
bit.ly/1uLrXLN - Ovw
bit.ly/1u4nODQ - WP
SUMMARY

UG-SQL-Server-Internals-Architecture.pptx

  • 1.
    SQL SERVER INTERNALS& ARCHITECTURE Kevin Kline, SQL Sentry Tech Evangelist, SQL Sentry Microsoft SQL Server MVP since 2003 Twitter , Facebook, LinkedIn @ KEKline Website: http://blogs.sqlsentry.com/KevinKline
  • 2.
    DROPPING ACID -WHY DOES SQL SERVER DO WHAT IT DOES? • ACID properties of Transactions o Atomic o Consistent o Isolated o Durable • Speed, scalability, and performance; Maximize hardware • Competitive features
  • 3.
    OUR TOUR GUIDE Talknerdy to me, baby!
  • 4.
    OK, WE’RE DONE Relational Engine Optimizer Query Executor CmdParser Storage Engine Trans- action Manager Buffer Manager Access Methods Protocol Layer SNI Data File T- Log Buffer Pool - - - - - - - - - - - - Data Cache - - - - - - - - - - - - Plan Cache SQL Server Network Interface TDS Language Event SELECT ? Query Tree Query Plan OLE DB ?
  • 5.
    QUERY OPTIMIZATION Query Processor/ Relational Engine Parser Query Optimizer Query Executor • Optimization is cost-based o Optimized for worst case scenario: “everything comes from disk” • The cost numbers may as well be unicorn freckles o Estimated costs may have no basis in current reality • Multiple phases (a.ka. “searches”) o Pre-optimization determines if the plan is “trivial” o Phase 0: simple plans: e.g. nested loops without parallelism o Phase 1: quick plans: plans that can be simplified o Phase 2: full plans: complex queries, parallelism, spills & spools to tempdb
  • 6.
    SCHEDULERS, THREADS, ANDWAITS • 1 Cash Register = 1 scheduler • Users are assigned to a thread Uh oh! Out of soda! No problem. Step aside… More syrup for the sodas! Goes to the waiting, i.e. “suspended queue” Yeah! I’m next in line!
  • 7.
    SQL SERVER WAITS SuspendedScheduler 1 Runnable Scheduler 1 Running Scheduler 1 55 Running 54 CXPACKET 60 LCK_M_S 61 LCK_M_S 53 Runnable 56 Runnable 59 Runnable Resource Waits Signal Waits 52 PAGEIOLATCH_SH 52 Runnable 55 PAGEIOLATCH_SH 53 Running
  • 8.
    Relational Engine Optimizer Query Executor Cmd Parser StorageEngine Trans-action Manager: Log & Lock Mgr Buffer Manager Access Methods Protocol Layer SNI Data File(s) T- Log Buffer Pool - - - - - - - - - - - - Data Cache - - - - - - - - - - - - Plan Cache SQL Server Network Interface TDS Language Event SQL OS ? Query Tree Query Plan OLE DB Data Write ? Check Point Lazywriter Latches Locks TROUBLE- SHOOTING WAIT STATS? Async_Network_IO SOS_Scheduler_Yield Pagelatch_x, Latch_x, Resource_Semaphore LCK_x, LCK_M_x Writelog, Logbuffer PageIOLatch_x, Async_IO_Completion, IO_Completion
  • 9.
    MEMORY MANAGER: BUFFERPOOL 8 OS CLR, MPA, DWA, TS Buffer Pool (SPA) SQL Server 2008 R2 & earlier Memory allocations within sqlservr process space Max server memor y OS DWA, TS Buffer Pool (SPA) SQL Server 2012 & later Memory allocations within sqlservr process space Max server memor y Data cache ---------------- Plan cache ---------------- Other caches Data cache -~-~-~-~-~- Plan cache -~-~-~-~-~- Other caches
  • 10.
    CACHES? • How longdoes a page of data or a block of code stay in cache? • Uses a LRU algorithm • Usually performed by the lazy- writer, but can also be done by any worker thread after scheduling its own I/O
  • 11.
    PLAN CACHE AGING get_order PlanCache reset_user proc11 proc14 1 6 1 6 7 2 2 3 3 0 2 7 1 5 6 4 0 1 1 4 1 5 1 3 1 2 What about buffer cache?
  • 12.
    BUT WAIT! THERE’S MORE! RelationalEngine Optimizer Query Executor Cmd Parser Storage Engine Transaction Manager: Log & Lock Mgr Buffer Manager Access Methods Protocol Layer SNI Data File T- Log Buffer Pool - - - - - - - - - - - - Data Cache - - - - - - - - - - - - Plan Cache SQL Server Network Interface TDS Language Event INSERT, UPDATE, or DELETE ? Query Tree Query Plan OLE DB Data Write ? Oooh! So dirty! CheckPoint Lazywriter
  • 13.
    Relational Engine Optimizer Query Executor Cmd Parser StorageEngine Trans-action Manager: Log & Lock Mgr Buffer Manager Access Methods Protocol Layer SNI Data File(s) T- Log Buffer Pool - - - - - - - - - - - - Data Cache - - - - - - - - - - - - Plan Cache SQL Server Network Interface TDS Language Event SQL OS ? Query Tree Query Plan OLE DB Data Write ? Check Point Lazywriter Latches Locks HEKATON, A.K.A. IN- MEMORY OLTP bit.ly/1uLrXLN - Ovw bit.ly/1u4nODQ - WP
  • 14.