SQL Server Performance Tuning Imp Points
SQL Server Performance Tuning Imp Points
SQL Server Performance Tuning Imp Points
Performance Tuning-1:
## Day One
Performance Tuning-2:
- RECOMPILE hints will cause the sproc to not show up in the DMV (Dynamic Management Views)
- DMVs and profiler will only catch perf issues for a window of time. They are passive tools that have to be running when the issue
occurs
- `sp_Blitz` will look across a wider window of time.
- ClearTrace as scriptable (and more powerful) version of SQL Server Profiler.
- Recommended practice is to create a Tools database to store scripts like ClearTrace and the BrentOzar scripts. May shops will not
allow anything to be installed to master. Plus, master won't be restored typically during a rebuild
- Extended events
- Can save to Disk, Activity Counters, or Histograms
- Very flexible filtering mechanism and supports random sampling
- Extended events are lighter weight than profiler
- However, extended events only works (well) on 2012+. Sorry 2008R2
- DMV
- Collect data since the sql server started up
- Metrics are very coarse. Anything finer requires active sampling
- `sys.dm_exec_procedure_stats`
- `sys.dm_exec_trigger_stats`
- `sys.dm_exec_query_stats`
- `CROSS APPLY` is an inner join to a function
- `sp_BlitzCache` - Batteries for DMVs
- Does not have sampling, so it's looking across all time.
- For 2008R2 and earlier you'll need to set a flag to pull back trigger stats b/c there's a bug in the way microsoft maintains the
counters
- Profiler and Server Side Trace are really the only tools availalbe for 2008R2 and earlier. ClearTrace would be the preferred way to
work with this trace info in these environments.
- Activity Monitor can conflict with SQL Server internals. Avoid.
- Also shows a limited set of information
- SqlServer:SqlStatistics:Batch Requests/sec
- Processor:% Processor Time
- SqlServer:Sql Statistics:SQL Compilations/sec
- `SET PARAMETERIZATION FORCED WITH NO WAIT` database level switch which will force parameterization across all simple
queries even if the queries aren't using parameters. `SIMPLE` would be the normal option.
- Template plan guides can force queries to act as if they have parameters
- Hint plan guides allow you to force OPTIONS or Hints
- Freezing plan guides. Forces a plan in cache to be used. SQL engine will have to do exactly what the frozen version indicates.
- Read metrics - look at logical reads, CPU time, duration, and query cost
- Identify the biggest problem: reads, CPU, or duration?
- Run sp_Blitz
- End user requirements gathering
- What's the target
- CREEPY Method
- Capture query metrics
- Read the metrics and plan
- Experiment with query cost:
- Remove the ORDER BY
- Change the list of fields in SELECT to just be SELECT 1
- This makes a big difference in how the query optimizer will look at indexes
- Switch table variables to temp tables
- Question any non-INNER joins
- Execution plan review
- Look at big differences between estimated and actual row counts
- Tune off the actual execution plan rather than the estimated
- Are stats out of date
- Parameter sniffing issues?
- Identify implicit conversions or SARGability issues
- Split query into temp tables and replace joins
- Parallelism opportunities
- Index improvements
- Always question `ORDER BY` clauses
- Focus on logical reads first and get that number down. Stats will give you this information. Plug into statisticsparser.com to help
with output.
- Statistics get updated by SQL Server when the amount changes by ~20%
- GROUP BY can perform better than COUNT(DISTINCT(x))
- 4 metrics to measure performance improvements (show % change of each)
- Logical reads
- Duration
- CPU time
- CXPACKET
- `SET STATISTICS TIME, IO ON` this will dump run stats to the messages window. You can then pipe this to the statisticsparser.com
site for easier to read details
- SQL Server always thinks that 1 record will come back from a table variable, UNLESS you use `OPTION (RECOMPILE)`. However,
that spikes CPU. This is why temp tables are preferred over table variables.
- You'll get better statistics if you use temp tables
- CTEs can give good statistics, however they will get rerun every time they're referenced
Performance Tuning-3:
Performance Tuning-4:
## Reporting in Production
- You need more RAM than data. If you have 128GB of data then you should have roughly 192GB of RAM
- If you can fit data in memory, then most of your tuning problems go away
- By default, SQL Server will allow one query to allocate up to 25% of available memory. 4 users with aweful reports could swamp a
server
- Can use filtered indexes to get best performance for recent history
- Indexes views are a view with a where clause - has a clustered index
- `DBCC TRACEON(610)` minimal logging - useful for DW transfers to significantly drop logging
- Almost impossible to run if you are running FULL recovery. Need to be running in SIMPLE recovery mode
- Use SIMPLE recovery model for datawarehouse databases. Pair this with `DBCC TRACEON(610)` to get super minimal logging and
better speed for writes
- `WITH INDEX(1)` will force the clustered index to be used
- Content Addressable Storage as a replacement for storing images/blobs in a SQL Server. http://www.emc.com/data-
protection/centera.htm
- ElasticSearch - full text search solution in use by Stack Overflow
- Redis for an open source caching layer
- ostress.exe - simple tool for scripting queries to hit a dev SQL Server. Similar to hammerdb
- Set autoshrink to off in sys.databases
- Stats updates happen synchronously when the next read comes through after writes cross the registered threshold
- THREADPOOL waits are an example of a poison wait. Shouldn't ever really have these.
- SQL Server memory ools:
- Execution Plan Cache
- Buffer Pool Cache (aka Data Cache)
- Query Workspace Memory (aka Memory Grant)
- RESOURCE_SEMAPHORE - Queries are asking for workspace memory and SQL server is unable to grant
- Scanning large tables can lead to big memory grants
- For 2012/2014 you need to set memory higher than max memory of 128 on Standard.
- Make sure that SQL Server is under external memory pressure
- sp_Blitz checks for the poison wait types
- The memory limits for SQL Server apply to the Data Cache only. So, add 15% of so to the max edition limit and set the max sql
server memory to this level.
- Leave 10% of 4GB (whichever is higher) for the OS