JRandall - Build High Perf SQL Server

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

SQL Server Performance Fundamentals

Building and Maintaining a Solid Foundation


Justin Randall
Senior Consultant, SentryOne
jrandall@sentryone.com
http://blogs.sentryone.com/author/JustinRandall
@jh_randall

Professional Services
Agenda
• Windows Server
• SQL Server Instance
• SQL Server Database
• Virtualization
Windows Server
OS Configuration Options
• Partition disk volumes hosting database files with
64 KB allocation unit size
• Exclude SQL Server files from AntiVirus scans
• Lock Pages in Memory
• Set Power Savings setting to High Performance
• Instant File Initialization
Hardware Power Management
Server BIOS Processor Specs & More

www.cpuid.com/softwares/cpu-z.html
Power Management
Windows Power Plans

VMware Power Management


Instant File Initialization
• Improves performance of database data file creation,
restores, and growth
• Causes file allocation requests to skip zero initialization
• Data files (*.mdf/.ndf) only. Not applied to tlog files
• Does not work with Transparent Data Encryption (TDE)
• Does not work on sparse file (i.e.– database snapshots).
Enable Instant File Initialization
Grant the SQL Server service Grant permission during SQL
account "perform volume Server install process – SQL Server
maintenance tasks" permission in 2016+
Windows Security Policy
(secpol.msc),
Storage
Storage Performance
• Slowest compute resource, even high performance
storage appliances
• Traditional rules of thumb
• place data files and tlog files on separate disk volumes
• raid 10 for data files, raid 1 or 5 for tlog files
• do not exceed disk max iops
• SANS, SSDs and virtualization change the rules
• managed performance
• consolidation of mixed workloads
• access paths (network) may become a bottleneck
Storage Performance
• There is no one "best practice" here!
• Follow storage vendor guidance, until it proves wrong for
your workload
• Configure for Performance, not Capacity
• Benchmark!
• Storage Benchmarking with DiskSpd (David Klee) : http://bit.ly/2UZG6GA
• Using Microsoft DiskSpd to Test Your Storage Subsystem (Glenn Berry) :
http://bit.ly/2U0YUJh
• Getting Started with DiskSpd (Brent Ozar): http://bit.ly/2HXauh5
• On github : http://bit.ly/2OsXP6P
SQL Server Instance
SQL Server Instances
• Multiple Instances per Windows Server
• TempDB
• CPU Impacting Settings
• Memory Impacting Settings
• Other Settings
Multi-Instance Installations
• CPU & Memory are shared resources
• SQL Server instances are not "aware" of each other and will
compete for resources.
• Significant manual effort to manage more than a few
instances.
• Complicates troubleshooting performance issues.
• Some amount of resource isolation is possible
TempDB
• A system database that functions as a work space for a
wide variety of user and system processes
• One per SQL Server instance: a shared resource, like CPU &
Memory
• Objects created in TempDB
o User objects
o Internal objects
o Row Versions
Important TempDB Facts
• TempDB does not persist between SQL Server restarts
• Operations with TempDB are minimally logged
• Uses the same collation as the SQL Server instance
• Always uses Simple Recovery model
• Always in MULTI_USER mode
• Cannot be backed up or restored
• Restart after property and file configuration changes
• File size and Max File Size
• Autogrow
• Number of data files and file locations
TempDB Configuration
• Use fastest storage available with least likely contention
• File Size
• Pre-size to accommodate the workload on that instance
• Account for user objects, internal objects, and version store

• Multiple Data File Guidelines


• < 8 cores : same number of files as number of cpu cores
• 8 or more cores : start with 8 files plus one file per four cores
• Ensure File Properties are identical for all data files
• Size
• Max Size
• Autogrow
CPU Impacting Settings: Parallelism
• cost threshold for parallelism
• default setting = 5

• max degree of parallelism


• default setting = 0 (unlimited)

• parallelism related wait types:


• CXPACKET
• CXCONSUMER
- SQL Server 2016 sp2
- SQL Server 2017 CU3
CPU Impacting Settings: Parallelism
Why does parallelism affect CPU?
• Spreads workload across multiple cores – this can be good, or bad
• The processes of splitting and combining the workload requires CPU
• Skewed Parallelism

Adjust the settings:


• max degree of parallelism
• same as number of cores, up to 8, or
• ¼ to ½ of available cores when more than 8
• cost threshold for parallelism
• Easy button: change from default setting to something like 50
• Better: analyze the workload, set a balance between resource intensive queries
and low cost queries
CPU Impacting Settings
Processor Affinity
• normally left at default (ALL)

Use cases:
• # of cores exceeds SQL Server limits
• Multiple instances on a server

How to:
• SSMS GUI
Server Properties: Processors
• T-SQL
ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY CPU = 0 TO 7,16 TO 25
Memory Impacting Settings
min server memory
max server memory
ALWAYS change from default setting

optimize for ad hoc workloads


always* enable
Max Server Memory Factors
• Total Physical Memory Available
• Memory set aside for the OS
• What SQL Server components are installed &
running (SSRS, SSIS, Full Text Search, etc.)
• SQL Server version & edition differences
• SQL Server Failover Cluster or multiple instances
• Other applications
Calculate Max Server Memory
Non-clustered, single instance
Basic: reserve 2 GB for OS + memory for non-SQL Server apps
Alternative (J. Kehayias): reserve at least 1 GB (usually 2 GB) for OS
+ 1 GB for every additional 4 GB of RAM up to 16 GB
+ 1 GB for every additional 8 GB of RAM over 16 GB
Examples: Physical RAM = 8 GB, max server memory = 5 GB
Physical RAM = 24 GB, max server memory = 18 GB

Clustered instances
Same numbers as above divided by the number of nodes in the cluster
SQL Server Database
Database settings
• auto close : off
• auto create incremental statistics : depends
• auto create statistics : on
• auto shrink : off
• auto update statistics : on
• auto update statistics asynchronously : depends
Database settings
• compatibility level: match to instance compatibility when possible
• file autogrowth: use MB/GB, not Percent
pre-size files to minimize autogrow events

• max size: sum for all db files not to exceed drive size

• parameterization: Simple or Forced

• delayed durability
commit data changes before they are written to the transaction log
ALTER DATABASE dbname
SET DELAYED_DURABILITY = DISABLED | ALLOWED | FORCED;
Database Scoped Configuration
• Manage optional configuration values to control performance and
behavior at database level
• Available in SQL Server 2016 and later (and Azure SQL DB)
• Benefits:
• Define configuration options at the database level
• Isolate database level behaviors within an instance
• Grant database level permissions to manage configurations
• Set configuration options separately for primary and secondary
replicas in an availability group
Database Scoped Configuration
• MAXDOP
• LEGACY_CARDINALITY_ESTIMATION (TF 9841)
• PARAMETER_SNIFFING (TF 4136)
• QUERY_OPTIMIZER_HOTFIXES
• CLEAR PROCEDURE_CACHE
• IDENTITY_CACHE (SQL Server 2017)
• ELEVATE_RESUMABLE (SQL Server 2019)
• DISABLE_INTERLEAVED_EXECUTION_TVF (SQL Server 2017)
• DISABLE_BATCH_MODE_ADAPTIVE_JOINS (SQL Server 2017
Database Scoped Configuration
Examples:
GRANT ALTER ANY DATABASE SCOPED CONFIGURATION TO [Joe];
ALTER DATABASE SCOPED CONFIGURATION
SET BATCH_MODE_ON_ROWSTORE = ON;
ALTER DATABASE SCOPED CONFIGURATION
SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY
SET MAXDOP = 8;
ALTER DATABASE SCOPED CONFIGURATION
SET ELEVATE_RESUMABLE = WHEN SUPPORTED;
Database Maintenace & Performance
• Backup and Restore Operations
• Minimize Recovery Time
• Index Rebuild and Reorganization
• Update Statistics
Virtualization
Virtualization
• Right-sizing
• Power Management
• CPU Configurations
• Memory Options
• Storage Considerations
Virtualization Considerations (1)
• Don't over allocate CPUs
• Do not enable CPU Hot Add
• Memory
• Hyper-V: disable dynamic memory
• VMware: do not disable the ballon driver - monitor

• Use Virtual Disks


Virtualization Considerations (2)
Key: Properly Distributed Workload
• Do not share resources with other applications
• Separate SQL Server disks from other workloads
• Benchmark SQL Server Workload
Checklist
❑ Set Power Management settings
❑ Enable Instant File Initialization
❑ Set and monitor SQL Server CPU and memory related
settings
❑ Set and monitor database settings
❑ Test before deploying upgrades, SPs or CUs
Checklist
❑ Adjust SQL Server Memory related settings
❑ Optimize TempDB configuration
❑ VM Configuration
avoid memory ballooning and cpu over-allocation

❑ Check Database Configuration


file size and location, autogrow in mb, autoclose off

❑ Automate Database Maintenance


Resources
1. View or Change Server Properties
https://bit.ly/2SuMnrS

2. Alter Server Configuration (Transact-SQL)


https://bit.ly/2T6oDiR

3. Operating System Best Practice Configurations for SQL Server


https://bit.ly/2XwAaH2

4. Instant File Initialization


How and Why to Enable IFI (SQL Server Premier Field Engineer Blog) : https://bit.ly/2GRjtkw
What, Why and How? (Kimberly Tripp): https://bit.ly/2EFFMY5

5. Database Scoped Configuration


Microsoft Docs: https://bit.ly/2T3yk1H (updated for SQL Server 2019 preview)
Niko Neugebauer Blog: https://bit.ly/2SBccqh
Resources
6. How to Document & Configure SQL Server Instance Settings
https://bit.ly/2EE19bW

7. TempDB Capacity Planning & Concurrency Considerations for Index Create and
Rebuild http://bit.ly/2CEgyaO
8. TempDB Database: SQL Docs
http://bit.ly/2FCvyrO

9. How to Make SQL Server Backups Go Faster


https://bit.ly/2BVhBTC

10. Recommended Updates & Configuration Options for SQL Server 2012 &
SQL Server 2014 with high-performance workloads https://bit.ly/2tJ9swX
**many trace flags mentioned in this article are default behavior for SQL Server 2016 and later
Resources
11. Proactive SQL Server Health Checks, Part 3: Instance and Database Settings
https://bit.ly/1KQE6E3

12. SQL Server on VMware


SQL Server and VMware Virtual Infrastructure: https://bit.ly/2tLuE5D
Architecting SQL Server on VMware Vsphere: https://bit.ly/2mXT32R

13. Virtual Machine vCPU and vNUMA Rightsizing – Rules of Thumb


https://bit.ly/2ncvSlV

14. Decoupling of Cores per Socket from Virtual NUMA Topology in vSphere 6.5
https://bit.ly/2ELMgo8

15. Does Core Per Socket Affect Performance?


http://bit.ly/2EKnN2r
Shameless Plug
Engage with our community:
Website: http://www.SentryOne.com
Facebook: https://www.facebook.com/SentryOne
Twitter: @SQL_Sentry or @SentryOne

Expert SQL Server Performance Tips: http://www.sqlperformance.com


Our Professional Services Group: https://www.sentryone.com/pro-services
Download Plan Explorer for free: https://www.sentryone.com/plan-explorer
Thank You!

You might also like