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
4. 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
?
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, 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!
8. 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
9. 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
10. 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
11. 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?
12. 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
13. 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