Microsoft Corporation
What We Will Cover
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
Session Prerequisites
This session assumes that you
understand the fundamentals of
Windows
®
2000 Server
SQL Server 2000
System Monitor
Level 200
Agenda
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
Lock Manager
What it does for you
Acquires and Releases Locks
Maintains compatibility Between
Lock Modes
Resolves Deadlocks
Escalates Locks
Uses 2 Locking Systems
Shared Data Locks
Internal latches for Internal data and
index concurrency
Locking
Locking Granularity for user data
Table
Page Page Page
Row
Row Row
Lock Isolation Levels
Supports all 4 ANSI and ISO
isolation levels
Serializable
Repeatable Read
Read Committed - default
Read Uncommitted
Locking
User data lock types – Shared
Acquired automatically when data is
read
Applies to Table, Page, Index Key or row.
Many processes can hold a shared lock
on the same data.
Cannot be locked exclusively while in
shared lock mode*
*Unless it is the same process that holds the shared lock
Locking
User data lock types – Exclusive
Automatically acquired when data is
modified
Only one process can hold at a time on
any data.
Held until the end of a Transaction
All other lock requests by other
processes will be refused.
Can use Query hints to decide to read
locked data.
Locking
User data lock types – Update
Hybrid of shared and exclusive
Acquired when a search is required
before any data modification
Allow others to still read while lock
applied
Needs an exclusive lock to modify data
Data can have many shared but only one
update
Locking
User data lock types – Intent
Not a real lock mode just a qualifier
e.g.
Intent Update lock
Used by SQL as resource lock
indicator
Locking
User data lock types – Special
3 special modes
Schema Stability – used when queries are
modified, prevents scheme modification
locks.
Scheme Modification – used when table
structures are being modified
Bulk update – used when the BULK INSERT
or BCP command are used.
Locking
Viewing lock information
Use sp_lock stored procedure
Use sp_lock2 stored procedure
Shows current and waiting locks
Spid Dbid Objid IndId Type Resource Mode Status
54 pubs 19723 2 TAB IS GRANT
58 pubs 19755 1 PAG 1:88 IX GRANT
52 Pubs 0 0 DB S GRANT
Locking
Viewing lock information - Type
Abbr Resource Internal
Code
Description / Example
DB Database 2
TAB Table 5 Table id 261775902
EXT Extent 8 File/ page number 1:96
PAG Page 6 File/ page number 1:104
KEY Key 7 Hashed value ac0001a10a00
AC Row 9 File/page/slot number 1:151:4
APP Application 10 Hash of the app name MYpr8dea
Locking
Viewing lock information - Mode
Abbreviation Mode Internal code
S Shared 4
X Exclusive 6
U Update 5
IS Intent shared 7
IU Intent update 8
IX Intent exclusive 9
SIX Shared with intent exclusive 11
Sch-S Schema stability 2
Sch-M Schema modification 3
BU Bulk update 13
Lock Block
Lock resource block
Grant Wait Convert
Lock Block
Lock resource block
Grant Wait Convert
Lock
Owner
Block
Lock
Owner
Block
Lock
Owner
Block
Locking Architecture
Lock hash table
All owner blocks
From same transaction
Linked together
Locking
Row Versus Page Level
Lock Overhead
Each lock – 32 bytes
Each Process holding lock – 32 bytes
Each Process waiting for lock – 32
bytes
Page Level – 8k
Row Level – dependent on Row size
Which to use depends on
application
Demonstration 1
Viewing SQL Server Locks
Agenda
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
Query Processor
Statement Compilation Process
Parse the statement
Create a sequence tree
Normalise the tree
SQL DML
Statement?
Compile the TSQL
Statement procedurally
Compile the TSQL
Statement procedurally
Create Query Graph
Optimize and create Plan
Yes
No
Query Processor
optimizing a statement
Trivial plan optimizer
Simplification
Statistics loading
Cost-based optimizer
Phases 1 to n-1
Full optimization for
Serial execution
Output plan
Full optimization for
parallel execution
Found a cheap
Plan?
1
1
Found
Plan?
Cheapest cost
Is > than parallelism
Threshold?
No
No
No
Yes
Yes
Yes
Query Processor
How the Optimizer Works
Query Analysis
Index Selection
Join Selection
Nested iteration
Hashing
Merging
Query Processor
Caching – costing a query plan
Allocate memory
From buffer cache
Set initial cost to
Creation cost
Ad-hoc &
Cheap to
Compile?
Type of
Plan?
Put plan in cache
Set initial cost
To Zero
Don’t Cache
Yes
No
Ad-hoc
Query Processor
Compilation and execution flow
Plan in
Cache?
Parse/normalize
Compile TSQL
Optimize statements
Put plan in cache
Retrieve plan from cache
Wait for memory grant scheduler
Open (active) plan
Run plan to completion
Plan need to
Be recompiled?
No
No
Yes
Yes
Demonstration 2
Query Analyzer
Obtain statistics about a query
Use the graphical showplan
Use the estimated showplan
Agenda
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
Query Tuning
Tuning – When to Start
Start at the Beginning
Consider performance before you
even write your first line of code
Be sure that you've set up a good
database structure
Create what appear to be useful
indexes
Make sure all analysis is done with
a representative workload
Query Tuning
Application and Database Design
Provides Biggest Performance
Gains
Normalize
Evaluate Your Critical Transactions
Keep Table Row Lengths and Key
Lengths Compact
Create Useful Indexes
Benchmark, Prototype and Test
SQL Server
SQL Profiler
Index Tuning
Wizard
Query tuning
Index Creation and Tuning
Query Tuning
Monitoring Query Performance
STATISTICS – Input/Output
Logical Reads
Physical Reads
Read Ahead Reads
Scan Count
STATISTICS - Time
SHOWPLAN
Showplan_Text, Showplan_All,
Graphical Showplan
Query Tuning
Query Hints
Query hints should be used for
special cases—not as standard
operating procedure
Hint Types:
Join Hints
Index Hints
Lock Hints
Processing Hints
Query Tuning
Blocking and Deadlocks – How to
Resolve Them
Keep transactions as short as
possible
Never add a pause within a
transaction for user input
When you process a result set,
process all rows as quickly as
possible
For browsing applications, consider
using cursors with optimistic
concurrency control
Query Tuning
Deadlocks – How to Resolve Them
To prevent cycle deadlocks, make
all processes access resources in a
consistent order.
Reduce the transaction isolation
level if it's suitable for the
application.
To prevent conversion deadlocks,
explicitly serialize access to a
resource.
Gather information about the application’s
behavior
Use SQL Profiler
Analyze the information
Query Analyzer
Index Tuning Wizard
Apply Changes
Index Tuning Wizard
Enterprise Manager
Query tuning
Performance Tuning
A step-by-step approach
Demonstration 3
Load Simulator
Demonstration 4
Index Tuning Wizard
Analyze workload
View reports
Apply suggested indexes
Agenda
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
System Configuration
Resource Allocation and System
File Location
Ensure that Maximize
Data Throughput for
Network Applications
is selected for File and
Print Services
Do not locate SQL
Server files on same
drive as PAGEFILE.sys
System Configuration
Configuring SQL Resources
Sp_configure
EXEC sp_configure
RECONFIGURE
With override
Enterprise Manager
System Configuration
Configuring SQL Resources
Min Server Memory
and Max Server
Memory
Set Working Set Size
Minimum Query
Memory
System Configuration
Configuring SQL Resources
Scheduling
Lightweight Pooling
Affinity mask
Priority boost
Max Worker Threads
Disk I/O Options
Recovery Interval
System Configuration
Configuring SQL Resources
Query Processing Options
Min Memory Per Query
Index Create Memory
Query Wait
Query Governor Cost Limit
Max Degree of Parallelism
System Configuration
Configuring SQL Resources
Database Options
Read Only
Single User
Autoclose
Autoshrink
Auto Create Statistics
Auto Update Statistics
Demonstration 5
SQL System Configuration
Enterprise Manager
sp_configure
Agenda
Locking
Query Processor
Query Tuning
System Configuration
Performance Monitoring
Performance Monitoring
How to Use SQL Profiler
Monitor server events
Capture SQL statements
Identify poorly-performing queries
Replicate problems for diagnosis
Performance Monitoring
When to Use SQL Profiler
Debug T-SQL or stored procedures
Monitor the performance of SQL
Server to tune workloads
Capture deadlocking scenarios
Playback events captured
Performance
Monitoring
SQL Profiler
Event Categories
Data Columns
Filters
Demonstration 6
SQL Profiler
Trace queries on a database
Create Trace Wizard
Performance Monitoring
System Stored Procedures
SQL Trace
sp_trace_create
sp_trace_setevent
sp_trace_setfilter
sp_trace_setstatus
sp_trace_generateevent
SQLDIAG
Demonstration 7
System Diagnostic Procedures
Performance Monitoring
Using System Monitor
Monitors Entire System
Performance
System Counters
SQL Counters
Performance Monitoring
System Monitor
Monitor multiple computers
simultaneously
View and change charts to reflect
current activity
Export data to spreadsheet or
database applications
Add system alerts to notify you of
possible problems
Performance Monitoring
System Monitor
Run an application when a counter
value exceeds a user-defined value
Create log files
Append log files to form a long-term
archive
View current-activity reports
Create reports from existing log files
Save chart, alert, log, or report
settings
Performance Monitoring
System Monitor - System Counters
System:
Context Switches/sec
Processor:
%Processor Time
%Privileged Time
%User Time
Processor Queue Length
Performance Monitoring
System Monitor - System Counters
SQL Server: Memory Manager:
Total Server Memory(KB)
Process:
Working Set Counter For SQL Server
Instance
SQL Server Buffer Manager:
Buffer Cache Hit Ratio
Performance Monitoring
System Monitor - System Counters
Memory:
Pages/sec
SQLServer: Databases
Transactions/sec
PhysicalDisk:
Disk Transfers/sec
Demonstration 8
System Monitor
Set Up System Monitor
Session Summary
SQL Server 2000 can use several
techniques to enhance performance
of applications
Techniques include:
Application and Database design
Query Tuning
System Configuration
Performance Monitoring
For More Information…
TechNet Web site at
www.microsoft.com/technet
Microsoft Official Curricula (MOC)
Course # 2071 - Querying Microsoft SQL Server 2000 with Transact-SQL
Course # 2072 - Administering a Microsoft SQL Server 2000 Database
Microsoft Press
―Inside SQL Server 2000‖, Microsoft Press, 2000., or:
http://mspress.microsoft.com/books/4297.htm
―Microsoft® SQL Server™ 2000 Resource Kit‖,
http://www.mspress.microsoft.com/PROD/BOOKS/4939.HTM
―Microsoft® SQL Server™ 2000 Reference Library ―,
http://mspress.microsoft.com/prod/books/5001.htm
―Microsoft® SQL Server™ 2000 Administrator's Companion‖,
http://mspress.microsoft.com/books/4519.htm
―Microsoft® BackOffice® 4.5 Resource Kit ‖, Microsoft Press, 1999, or:
http://mspress.microsoft.com/prod/books/2483.htm
Training
Training Resources for IT Professionals
Querying Microsoft SQL Server 2000 with Transact-SQL
Course # 2071—Two days—Instructor-led or eLearning
Available: through MS CTECs in your area
Administering a Microsoft SQL Server 2000 Database
Course # 2072—Five days—Instructor-led
Available: through MS CTECs in your area
To locate a training provider for this course, please access
mcspreferral.microsoft.com/default.asp
Microsoft Certified Technical Education Centers (CTECs)
are Microsoft’s premier partners for training services
Where Can I Get TechNet?
Visit TechNet Online at
www.microsoft.com/technet
Register for the TechNet Flash
www.microsoft.com/technet/register/flash.asp
Join the TechNet Online forum at
www.microsoft.com/technet/discuss
Become an TechNet Subscriber at
technetbuynow.one.microsoft.com
Attend More TechNet Events
Become A Microsoft
Certified Systems Engineer
What Is MCSE?
Premier certification for professionals who analyze
the business requirements and design and
implement the infrastructure for business solutions
based on the Microsoft server software.
How do I become a Windows 2000 MCSE?
Pass 4 Core Exams
Pass 1 Design Exam
Pass 2 Electives from a comprehensive list
Where Do I Get More Information?
For more information about certification
requirements, exams, and training options,
visit www.microsoft.com/mcp
Session Credits
Authors: Dan Keables, Joel Ware IV
Producer/Editor: Alan Le Marquand
Thanks to our Microsoft technical
specialists who reviewed this session:
John Doe