SQL Server Performance Tuning Interview Questions
SQL Server Performance Tuning Interview Questions
Part 1
Posted on December 17, 2014 by uday arumilli
Memory bottleneck:
Consistently low average page life expectancy. (MSSQL$Instance: Buffer Manager\Page Life
Expectancy:)
(See Average Page Life Expectancy Counter which is in the Perfmon object SQL Server Buffer
Manager (this represents is the average number of seconds a page stays in cache). For OLTP, an
average page life expectancy of 300 is 5 minutes. Anything less could indicate memory pressure,
missing indexes, or a cache flush)
Consistently low SQL Cache hit ratio. (MSSQL$Instance: Plan Cache\Cache Hit Ratio:)
(OLTP applications (e.g. small transactions) should have a high cache hit ratio. Since OLTP
transactions are small, there should not be (1) big drops in SQL Cache hit rates or (2) consistently
low cache hit rates < 90%. Drops or low cache hit may indicate memory pressure or missing
indexes.)
IO bottleneck:
High average disk seconds per read.
(When the IO subsystem is queued, disk seconds per read increases. See Perfmon Logical or
Physical disk (disk seconds/read counter). Normally it takes 4-8ms to complete a read when there
is no IO pressure. When the IO subsystem is under pressure due to high IO requests, the average
time to complete a read increases, showing the effect of disk queues. Periodic higher values for
disk seconds/read may be acceptable for many applications. For high performance OLTP
applications, sophisticated SAN subsystems provide greater IO scalability and resiliency in handling
spikes of IO activity. Sustained high values for disk seconds/read (>15ms) does indicate a disk
bottleneck.)
Big IOs such as table and range scans due to missing indexes.
Blocking bottleneck:
High average row lock or latch waits.
(The average row lock or latch waits are computed by dividing lock and latch wait milliseconds
(ms) by lock and latch waits. The average lock wait ms computed from
sys.dm_db_index_operational_stats represents the average time for each block.)
Network bottleneck:
High network latency coupled with an application that incurs many round trips to the
database.
Network bandwidth is used up.
(See counters packets/sec and current bandwidth counters in the network interface object of
Performance Monitor. For TCP/IP frames actual bandwidth is computed as packets/sec * 1500 *
8 /1000000 Mbps)
Server Hardware:
Most slow applications are slow because of poor up front design, not because of slow hardware.
Since the application’s design can’t be changed at the time when deployed to production, about
the only thing you can try to help boost performance is to throw hardware at it.
CPU: Always purchase a server with the ability to expand its number of CPUs. Usually it
goes for larger servers with four or more CPUs. Always leave room for growth.
Memory: Try to get enough RAM to hold the largest table you expect to have, and if you
can afford it, get all the RAM your server can handle, which is often 2GB or more.
I/O Subsystem: At the very minimum, purchase hardware-based RAID for your databases.
As a rule of thumb, you will to purchase more – smaller drives, not fewer – larger drives in your
array. The more disks that are in an array, the faster I/O will be.
Network Connection: At the server, have at least one 100Mbs network card, and it should
be connected to a switch. Ideally, you should have two network cards in the server connected
to a switch in full-duplex mode.
For best performance on a server, SQL Server should be the only application running on the server,
other than management utilities. Don’t try to save a few bucks by putting your IIS server on the
same server as SQL Server.
FROM SYS.DM_OS_RING_BUFFERS
WHERE ring_buffer_type = N’RING_BUFFER_SCHEDULER_MONITOR’
One record is stored every minute up to a maximum of 256 records. Clicking on any of the XML
links will take you to the XML editor and will show an entry similar to below
<SchedulerMonitorEvent>
<SystemHealth>
<ProcessUtilization>55</ProcessUtilization>
<SystemIdle>35</SystemIdle>
<UserModeTime>228180000</UserModeTime>
<KernelModeTime>251812000</KernelModeTime>
<PageFaults>64252</PageFaults>
<WorkingSetDelta>21770240</WorkingSetDelta>
<MemoryUtilization>100</MemoryUtilization>
</SystemHealth>
</SchedulerMonitorEvent>
</Record>
Now find out the query/proc/process that is making CPU utilization High:
SELECT TOP 20
qst.sql_handle,
qst.execution_count,
qst.total_worker_time AS Total_CPU,
qst.total_worker_time/1000000,
(qst.total_worker_time/1000000) / qst.execution_count,
qst.total_elapsed_time,
qst.total_elapsed_time/1000000,
st.text AS ‘Query’,
qp.query_plan
from
sys.dm_exec_query_stats as qst
From the above script we can find the commands which are taking the most CPU time along with
the execution plan. By reviewing the execution plan you can see what additional indexes need to
be added to the database which will improve database performance and decrease the CPU load
time.
By adding missing indexes or by using the proper indexes we can decrease the load on CPU.
Other options:
Sp_monitor: Displays statistics, including CPU usage, I/O usage, and the amount of time
idle since sp_monitor was last executed. We can get the information about the “CPU Time
(Sec)”, “I/O Time (Sec)”, “Count of Input\Output Packets”, “No of logins attempted”, “Errors
in reading/writing network packets” etc.
@@CPU_BUSY / @@IO_BUSY: Returns the time that SQL Server has spent working since it
was last started. Result is in CPU time increments, or “ticks,” and is cumulative for all CPUs,
so it may exceed the actual elapsed time. Multiply by @@TIMETICKS to convert to
microseconds. But it may not the accurate value to be considered.
PerfMon
Profiler
Q. Can you tell me what the Wait Type “LAZY WRITTER” is?
Ans:
The job of the lazy writer is to find dirty pages in the buffer pool and write them out to disk and
drop those pages from cache.
In general there are three categories of waits that could affect any given request:
Q. What are the main parameters we need to check when you are dealing with memory
performance?
Ans:
There are four significant properties of sql server.
For example we have 24 GB available and the settings can be like this:
Min Memory: 1 GB
Max Memory: 16 GB
Remember total max memory of all instances should not exceeds the actual physical memory
available
Priority boost: By default, the priority boost setting is 0, which causes SQL Server to run at a
normal priority. If you set priority boost to 1, the SQL Server process runs at a high priority.
Lightweight pooling: Switch on this parameter when you want to make sql server use the fiber
mode facility. Unless there is a real need and environment (Large multi-processor servers)
available we should not use this option at production servers.
Related: