Wednesday, January 26, 2011

UNIQUEIDENFIER and a primary key constraint..

I would lke to share some tips which can affect performance  big way.
 
This is about "uniquifiers " datatype...although i hope it wont be favourite for many after reading this.
 
When you chose your cluster index which is variable length column then you are adding 4 bytes to every row which you is an overhead.
 
Now interesting point is that UniqueIdentifier are also stored in variable blocks, which means you are adding overhead of 4 bytes every time to your row.
 
Now you can imagine what I am trying to tell you :) ......Ofcourse you are performing more IO unneccesary by adding those extra 4 bytes.
 
Also uniqueidentifier is not sequential, which means sql server has to work more harder when you make it as a clustered index(which works best sequentially).
 
In a nutshell, UniqueIdentifier  is very big data type and when you are inserting large number of rows this cause huge fragmentation and consequently the size of your cluster index grows huge as well.
 
Please use data type like  int and  make your cluster index narrow, sequential(uniqueidntifier is non sequential) and with fixed size columns.
 
Believe me this will make your life much easier :)
 

Does backup records any changes happening at the time backup job is running

I was at one of my client site and very simple though very smart question was thrown at me by one of the developer:
The question was "Does backup records any changes happening at the time backup process is running?"

The answer is "Yes". You can perform small exercise where you can insert many rows into a single table, Kick off the database backup process and open other session and run few DDL statements against the same database.

Oncebackup is finish try restoring the database with the .bak file you just took and you will see those new objects(tables in my experiment) were captured by the  process although i ran those statments after i kicked off backup.

:) :) try it yourself

Wednesday, January 12, 2011

Is recompilation good or bad?

I am assuming you all know what recompilation means here. In simple words for those who don't know, SQL Server recompiles the statement (in sql server 2008) or procedure when it realises that Plan existing in cache is no longer beneficial or aged out due to specific reasons.
Recompilation is useful when new execution strategy is require to  process the query but as recompilation involves cost of extra CPU cycle, too often recompilation of a store procedure can be expensive.
You have to make sure you know the cause of recompilation of store procedure. There can be various reasons behind this .Few of them are:
·         Schema changes to the temporary tables define inside the body of procedure
·         General DDL changes to the local tables
·         Change in statistics of table due to lot of insertions or distribution of data
·         Force recompilation by using recompile hint
·         Changing the SET option

If you know the cause of too much recompilation (I am using the word too much as occasional recompilation is not concern here) then you should look to rectify that cause. For example if the frequent recompilation is happening due to change in distribution of data or statistic change then you can use Keepfixed plan hint to avoid so.
If you are not dealing with large amount of data sets then use table variables instead of temporary tables. Unlike temporary tables, table variables do not create statistics hence you can save unnecessary recompilations caused because of temporary tables.
Using brilliant tool like profiler, you can capture the statements causing recompilation. Once you know which statements are causing recompilation, you can dig into the cause and work on it.

So next time if your trace shows lot of recompiles you know what to look for :)

Tuesday, January 11, 2011

Why my Buffercache hit ratio is High but Page Life expectancy is very low?

Buffer cache it ratio and Page Life expectancy are the two important counters which helps to identiy SQL Server memory issue.I have often seen the scenarios where these two counters conflict each other.

Recently I was monitoring one production SQL Server using spotlight and constantly I was getting pagelifeexpectancy alert but when i sneaked into the BufferCacheHit ratio counter, it was 99.6%.This is especially in the case of 32 bit SQL Server.

So the question came to my mind was "is their really Memory bottleneck?"

Answer is "Yes". What this mean is logical reads are still using buffer  but the pages are not staying their long enough also.

I would prefer PageLifeExpctancy alert over BufferCachHitRatio.Ideally only buffer cache hit ratio is not enough to conclude memory bottleneck.

Also in this kind of scenario i would look at IO wait as well.

Sunday, October 17, 2010

Which counters I should use to troubleshoot my sql server hardware resources?

Performance monitor is one of the best tools you can use to look how your sql server is coping up with the resources.
If you type and run perfmon.exe from your windows OS, you have already taken a first-rate step to dive into your monitoring process.
Question normally comes to our mind is what counters should we use to monitor usage of resources for such a powerful application like SQL Server?
Whether you are a DBA or operation system engineer who is helping DBA, it is always handy to keep a user defined data collector set to monitor your sql server resources.
You can use this set to create your benchmark or you can also use this to troubleshoot performance from time to time.
I normally keep this collector set handy, so that whenever I visit the client site and if required I can just upload these counters and start collecting the data. I keep the collection interval to the default of 15 seconds.
I am allotting the performance counters as for each hardware resources here. When we think from hardware perspective, we think of Memory, CPU and Storage (IO). I do not want to go to Networking here, Each of these counters can give you quick insight of any potential bottlenecks.
MEMORY
Object
Counter
Threshold
Memory
Available Bytes
Set according to your system
Memory
Pages/sec
< 50
SQL Server: Buffer Manager
Checkpoint Pages/sec
< 30 pages per second
SQL Server: Buffer Manager
Buffer cache hit ratio
>= 90%(normally it should be 99-100% for healthy system)
SQL Server: Buffer Manager
Page Life Expectancy
>300 sec
SQL Server: Buffer Manager
Lazy Writes/sec
< 20



CPU
Object
Counter
Threshold
Processor(_Total)
% Processor time

< 80%
Processor(_Total)
% Privileged Time

< 10%
System
Context Switches/sec

<1000
SQL Server:Statistics
Batch Requests/sec

Set according to your system and compare with the benchmark value
SQL Server: Statistics
SQL Compilation/sec
>100


STORAGE
Object
Counter
Threshold
Physical Disk
Avg. Disk Sec/Read

<= 10 ms is good
>10 ms and <=15 ms is tolerable
>20 ms is very bad
Physical Disk
Avg. Disk Sec/Write

<= 10 ms is good
>10 ms and <=20 ms is tolerable
>20 ms is very bad
Physical Disk
% Disk Time

<85%


enjoy! :) :)

Tuesday, October 12, 2010

Issue with Replicating spatial data type in sql server 2008 and R2 version

Few weeks before one of my client had an issue with replicating geometry data type in sql server 2008 r2 version.
If you try to set up very simple merge replication between two instances and try to synchronize geometry data type, Replication fails with the below error:

The Merge Agent failed because the schema of the article at the Publisher does not match the schema of the article at the Subscriber. This can occur when there are pending DDL changes waiting to be applied at the Subscriber. Restart the Merge Agent to apply the DDL changes and synchronize the subscription. (Source: MSSQL_REPL, Error number: MSSQL_REPL-2147199398)
Get help: http://help/MSSQL_REPL-2147199398
Error converting data type varchar to geometry. (Source: MSSQLServer, Error number: 8114)
Get help: http://help/8114

I spent quit a time resolving this issue but at the end i approached Microsoft sql engineers. They confirm this as a bug in the current version and gave me the workaround.

Below is the reply from Mirosoft and applying this fix resolved the above issue:


Error and Symptom:
------------------------------
We got the below detailed error message in the profiler trace.
exec sp_MSadd_repl_error 3109,0,5,N'USER-PC\SQLEXPSP1',N'8114',N'Error converting data type varchar to geometry.',50,1,N'
SQL Merge Agent encountered an error.

This error occurs on the subscriber side (SQLEXPSP1), while the merge agent is applying the changes to the subscription. The data type in the published article is geometry, but it seems to be converted to "varchar" by some unknown reason. And then, conversion error occurs when the merge agent wants to convert the "varchar" back to "geometry" to match the schema on the subscriber.

Reproduce and Analysis:
-----------------------------------------
1.       Repro: I spent quite some time to reproduce the same error in my local labs. This error can be always reproduced in SQL Server 2008 and 2008 R2. Below is the error that is captured from the subscriber profiler trace. (You could also capture a similar trace if you run profiler against the SQLEXPSP1 instance.)
exec [dbo].[MSmerge_ins_sp_8F98318B5A5D4AD92A55CAA41E884BDE]
'7E976B77-94C4-DF11-9867-000FFEE4266F',
2,
0x881E2A55A4CA01000000FF,
0x00,
2005,
'',   ----this is the null char
'7E976B77-94C4-DF11-9867-000FFEE4266F',
0,
0x00,
90
2.       Cause: This SP failed with error "Error converting data type varchar to geometry." The error occurs because this procedure get a null char for the 6th input parameter, which couldn't be converted to a geometry value. By definition, this SP expects a geometry data here.
3.       Conclusion: I involved our production team for further clarification. And they confirmed this is a known issue in current builds of SQL Server 2008 SP1 and 2008R2. This is intended to be fixed in next Service Pack release.

Workaround:
------------------------
To work around this issue, we can use the below modified script to manually create the articles with two modified parameters.
exec sp_addmergearticle @publication = N'GeoMetryPub', @article = N'SpatialTable', @source_owner = N'dbo', @source_object = N'SpatialTable', @type = N'table', @description = N'', @creation_script = N'', @pre_creation_cmd = N'drop', @schema_option = 0x000000000C034FD1, @identityrangemanagementoption = N'auto', @pub_identity_range = 10000, @identity_range = 1000, @threshold = 80, @destination_owner = N'dbo', @force_reinit_subscription = 1, @column_tracking = N'false', @subset_filterclause = N'', @vertical_partition = N'false', @verify_resolver_signature = 1, @allow_interactive_resolver = N'false', @fast_multicol_updateproc = N'true', @check_permissions = 0, @subscriber_upload_options = 0, @delete_tracking = N'true', @compensate_for_errors = N'false', @stream_blob_columns = N'false', @partition_options = 0

The @schema_option was 0x000000010C034FD1, and we changed it to 0x000000000C034FD1.
The @stream_blob_columns was N'true' and we changed it to N'false'.

For more information about the sp_addmergearticle script, we can refer to

http://msdn.microsoft.com/en-us/library/ms174329.aspx
Note: You may need export the articles' creation script before dropping it. Then modify the schema_option and stream_blob_columns parameters, execute it. Then re-sync the replication. It should work after the modification.
Enjoy :) :)  and thanks to Microsoft for their excellent support here.
--Puneet

Monday, October 11, 2010

What is before Analysing execution plan and Indexes?

What is before Analysing execution plan and Indexes?
Now I know which query is most expensive in terms of CPU and execution time. Now what’s next??Many of us will jump into the execution plan and start looking at the key operator’s immediately.
Before we go there, we should consider outer factors which can also affect the query performance. By outer factors I do not mean "hardware resources", but components within the SQL Server RDBMS itself.
These are:

Database Object StatisticsAny object referred inside the query has statistics which optimizer uses to decide the execution plan. All execution plans are based upon these statistics.
This means if the database object statistics are not up-to-date, then the execution plan will be inefficient also.
You can check the statistics of the table and its indexes by running classic DBCC SHOW_STATISTICS command.


Index defragmentation
If the database objects referred inside the query are fragmented heavily then this will cause query to read more pages then required.
Make sure your indexes are not heavily fragmented. In sql server 2005 and onwards you can check this by using the DMV sys.dm_db_Index_physical_stats.
I normally look for any fragmentation level of more than 30% and also look at number of pages of the indexes. If index with high number of pages is fragmented heavily then that is definitely going to affect performance.


Connection level options
Setting connection level options different then default value inside the query can cause operations like recompilation. Make sure your Connection level options are set properly inside the body
of TSQL.