Archive for the 'Performance' Category

SQL Performance Tuning – Overview

2012/11/17

SQL performance tuning is a huge topic, and you need to know many detailed info of behavior with different version of OS and SQL. In these posts I will share my general approach of such work.

Before Tuning

The first thing you should keep in mind is end users don’t care SQL, they care the overall application response time. You have to know your application. Sometime we need to tune how application call database. One example is you should cache your blog post from dynamical page(query from database every time) to statics page.

The second thing is database performance is related to application behavior(queries). And application behavior could change from time to time. So tuning is not one time action. You should create your performance baseline and keep on monitoring your environment. The usage of performance baseline is to tell you how SQL server run when the application response time is acceptable and to shorten your time to find out what’s abnormal. Unlike other product(AD, Exchange), SQL and IIS are platform to run your application and each SQL has its own performance baseline. You can’t simply relay on one general baseline, you should create for each of the critical application. You could tune SQL without baseline but it will take you longer time to find out what’s abnormal.

The baseline should cover
1. OS level performance counter like CPU, memory, disk and network etc.
2. SQL instance level performance counter like buffer pool, access methods etc.
3. SQL instance level DMV info like top n database, top n wait stats and top n query.
4. (optional) Application level key workload count.

Load test before RTM is very important, you could build the initial baseline here. Although it’s hard to simulate all the end user query pattern, it’s useful to find out what’s different between load test and real production loading later on. We will discuss performance counter meaning in 3rd post.

The third thing is set your tuning target and prioritize the findings. My idea is no need to change code immediately if the overall application response time is acceptable. You could put the finding into potential issue list. But you need to keep on monitoring the performance data and plan your human resource to fix these potential issues before they become real problem. One example is you find out full scan number is high but database size is small and physical memory is sufficient. Most of the read is logical read than physical read. Then there is no performance issue at this moment. But you should know that logical read will increase when table size growing, and logical read will become physical read once database size being larger than memory buffer pool size.

Tuning Workflow
Typical tuning workflow is like:
1. Data collection. There are multiple information could be captured by different tools like msinfo32, eventvwr, SQL error log, perfmon and profiler. I will share the tips in 2nd post.
2. Perfmon analysis – OS. We will find out if any hardware resource is bottleneck here.
3. Perfmon analysis – SQL. We will find out how SQL Server consume hardware resource here.
4. Top N databases by bottleneck resource. Using profiler output or DMV to find out how databases consume resource here. Move them to different instance or tune their Top N queries.
5. Top N queries by bottleneck resource. Using profiler output or DMV to find out top n queries, then tune the single execution cost or reduce the execution number.
6. Wait stats. SQL Query Duration = CPU Time + Wait Time. Both duration and CPU time could be found from profiler output and DMV, then you can calc the wait time. If wait time is high, no doubt that you should dig into wait stat.
7. Check top n queries’ execution plans, tune them case by case. There are 2 performance tuning scenarios, 1st is single query slow, 2nd is concurrency related. This step is for single query slow scenario. The latter is more difficult to tune. I plan to cover wait stat and concurrency in 4th post of this tuning serial.

Faster DBCC CHECK

2011/12/21

It’s common nowaday that taking long time to run DBCC CHECK for TB level database. Now there is a way to consume more IO resource to run DBCC CHECK faster, by using flag 2562 and 2549. This is only availble for SQL 2008 R2 CU11 now.

My 2 cents, pay attention to tempdb usage when enabling these flags as the trade-off is tempdb size and io. And dont set them as startup parameter, just enable them in the session level when you are ready for the trade-off.

http://support.microsoft.com/kb/2634571

Long Running Transaction

2011/09/27

For SQL 2000, the only way to find out long running transaction is to run DBCC OPENTRAN.

Begin from SQL 2005, you can query DMV with the script below:

SET NOCOUNT ON
select db_name(database_id) as dbname,tr.transaction_id,ses.session_id,database_transaction_begin_time
from sys.dm_tran_database_transactions as tr
join sys.dm_tran_session_transactions as ses
on tr.transaction_id = ses.transaction_id
where is_user_transaction = 1
and database_transaction_begin_time < (GETDATE()-’00:05:00′)

It will help you to find out transactions which running longer than 5 min.
 

Multiple Log Files for TempDB?

2011/09/21

Recently I saw a saying about tempdb performance like "…Since you have 8 CPUs, split temp log files on 8 different physical disks to accelerate the write log performance…" This is not correct.

Data file (MDF, NDF) and log file (LDF) have different I/O behavior pattern. For data file, it is "asynchronous random read/write". For log file, it is "synchronous sequential write". Once a data page is modified, SQL Server storage engine will change the page in buffer pool first, and refresh it to disk later. But for log file, SQL Server uses a write-ahead log, which guarantees that no data modifications are written to disk before the associated log record is written to disk. So data file IO pattern is asynchronous, and log file IO pattern is synchronous.

As SQL Server writes log sequentially, even there are more than one log file, SQL Server will use them one by one. The benefit of multiple log files is providing pre-allocated disk place and avoiding single log file size becoming too huge. But for tempdb, the default recover mode is "simple", which means the log space will be used circularly. For those databases with "full" recover mode, backing up log file regularly can also free the log space.

The conclusion is no need to add multiple log files from performance point of view.
 

Does Your CPU Run in Full Speed?

2011/07/30

This issue is not new, but I hit it 3-4 times since last Sept. You can download CPU-Z from http://cpuid.com/. Run it and check the number you read from “Core Speed” is same as “Specification”. If not, you should modify server BIOS setting, making the server runs in full speed.