Rahman Agoro, the frontline SQL DBA

Just another WordPress.com weblog

Archive for October, 2010

Could not obtain information about Windows NT group/user ‘domain\xxxx’, error code 0x5. sp_dropdistributor

Posted by rahmanagoro on October 11, 2010


I have recently ran into an issue with dropping replication, it complains about the error below.

exec master..sp_dropdistributor @no_checks = 1
Msg 15404, Level 16, State 19, Procedure sp_replcmds, Line 1
Could not obtain information about Windows NT group/user ‘DOMAIN\xxxx.xxxxxx’, error code 0x5.

Exceptions like this are often caused when the login does not have access to a resource, or maybe when one is trying to detach/attach a database in SQL 2008 whilst using different user names. It sounded quite strange that I am having the same issues whilst trying to do something as simple as dropping replication on a test server. I setup a profiler trace for the session that I am using to try and track where the exception is occurring so as to know how to tackle the problem. I realized that the stored procedure was failing when it got the section trying to run the code.

sys.sp_replcmds 0

This stored procedure runs as part of exec master..sp_dropdistributor @no_checks = 1

Upon further investigation, I was able to run the stored procedure on the database which was throwing the exception, the first thing that came to my mind was, “which login owns the database ?”. I then ran the code below to check the owners of each and every database.

select name, SUSER_NAME(sid) as DBOwner from master..sysdatabases (nolock)
where name not like '%Snap'
order by name

Some of the databases came up as “sa” which is my preferred db owner and some came up as null, the ones that came up as null were databases that I had just restored recently using my windows login, once restored the SIDS did not match that which was on the master database in the syslogins table.

In order to fix the problem, I then ran the script below that will update all the database owners on the database to sa.

   EXEC master..sp_MSforeachdb '
USE [?]
IF DB_NAME() not in (''master'',''msdb'',''distribution'',''tempdb'',''model'')
BEGIN
	IF db_name() not like ''%Snap''
		BEGIN
		   SELECT ''?''
			exec sp_changedbowner ''sa''
		END	
END'

Now whilst trying to drop the distributor, it now works without any problem.

exec master..sp_dropdistributor @no_checks = 1

As a precaution, always change the owner of the databases to SA in order to avoid any such problems. Another issue that can occur is if the windows login which owns the database is removed from active directory ? thesame problem can occur.

   EXEC master..sp_MSforeachdb '
USE [?]
IF DB_NAME() not in (''master'',''msdb'',''distribution'',''tempdb'',''model'')
BEGIN
	IF db_name() not like ''%Snap''
		BEGIN
		   SELECT ''?''
			exec sp_changedbowner ''sa''
		END	
END'
Advertisements

Posted in Management, Scripts, SQL replication, Tips | 2 Comments »

SQL server DBA development code review checklist

Posted by rahmanagoro on October 4, 2010


As the database administrator, I often have to deal with production problems. Now some of these problems can be classed under different categories, they include the followins.

  • Peformance issues.
  • Issues as a result of a release.
  • Bugs within the SQL engine
  • Faulty SQL code
  • Proactive/reactive issues

The issue of performance problems and faulty SQL code can often be attributed to bad coding, whilst I know that a job of a DBA itself can be hard, the code review process is one which is often ignored in most shops. I don’t share thesame approach, I like to know what code is being written on the database server which I manage, afterall if there are problems, the database administrator will be the one to be called at 02:45AM in the early hours of the morning. I like to avoid such problems and also ensure that my environment is as stable as possible. The art of performance tuning is not one that can be learnt from text books or just reading from the internet, its one that you gain often from experience.  I often gain such experience by dealing with production issues, attenging seminars and of course a lot of reading, but I find that some of these problems can be avoided in the first place. Thats where DBA code reviews comes into play, imagine a situation where a developer was corrected as to how to best write a SQL query, another developer is making thesame mistake, so what do you then do. Do you go to every developer and start correcting them ?

At a previous job, what I used to do is organise presentations once a month with my development team, often speaking about issues that I have either noticed or topics that the development team want me to speak about. As for code reviews, I came up with a checklist of items that I often look out for. They are as follows.

  • Consistent naming convention should be used at all times, there are loads of naming conventions out there, pick one and stick to it.
  • All objects especially stored procedures should have comments, information can include author, date, description of the object, changes etc.
  • Error handling using TRY.. CATCH. TRY..CATCH was released in SQL 2005 and is by far the best error handling method built into SQL.
  • Use of schema names when referencing tables, specify Database.schemaname.objectname. This ensures that the SQL engine doesnt start looking for the schema which holds the object.
  • Avoid the use of Select *, all column names must be explicitly specified. This will avoid unnecessary IO.
  • Ensure that all tables have a primary key (preferably of integer type), and all foreign keys are indexes with a non clustered index.
  • Avoid the placing of duplicate indexes on tables. As simple as it might sound, I have seen this in various places.
  • Developers should make efforts to ensure that databases are normalised, a relationship diagram for the database would also be useful.
  • Avoid server side cursors and maintain the use of SET based operation.  If a cursor really needs to be used, use a WHILE loop instead and specify a primary key in your table, a very easy way to do this is using an identity column.
  • Temporary tables and table variable, as a general rule of thumb use a table variable for relatively small datasets, and use a temporary table for fairly large datasets. Temp tables maintain statistics and can have indexes, but often recompile on the other hand as a downside, table variables don’t maintain statistics but they don’t force a recompilation. This was demonstrated in the SQL BITS 2009 session.
  • When using nested stored procedures, ensure that the temporary table names are unique/different. I have seen this affect query plans in a very catastrophic manner.
  • Avoid the use of functions in Joins within SQL 2005/2008, from experience performance tends to suffer as the dataset grows mianly because the SQL optimizer doesn’t know which index to use. Use a CTE or a derived table instead.
  • Embrace the use functions like OBJECT_ID to check for the existence of an object within stored procedures/script etc. This option is better then querying sysobjects where it can cause locking.
  • When using ORDER BY within SQL, avoid using ORDER BY ORDINALS for example select name,surname from tblStudents order by 1,2. When using order by, ensure that the column named are specified, alias names should also be avoided as this doesn’t work in SQL 2008.
  • Stored procedures that take in different combination of parameters should use local variables in order to avoid parameter sniffing. When having branching IF statements within stored procedures that process complicated logic, each IF statement should call another sub procedure rather than a direct SQL statement. This will allow stored procedures to make use of the procedure cache, and prevent stored procedures generating inconsistent query plans due to parameters.
  • Tables that hold parameter information or lookups should have not null constraints defined.
  • Developers should make efforts in identifying heavy queries within their applications, and review accordingly on how to optimise the query if needed.
  • New tables to be sized appropriately with respect to the use of the table and the nature of the data it will store.
  • Avoid the use of optimiser hints, in SQL 2008 hints should only be used in exceptional circumstances i.e. maintaining a consistent planguide etc. At all times, the optimiser should be left to determine the best plan for all queries.
  • Use of SET NOCOUNT ON at the beginning of stored procedures, this improves performance by reducing network traffic.
  • Developers should be careful with the use of custom data types, native SQL data types should be used where possible at all times.

Posted in Database development, Professional Development, SQL Administration | Leave a Comment »

Field notes from SQL BITS 7 full conference

Posted by rahmanagoro on October 4, 2010


I recently attended the SQL bits 7 full conference in the UK, I will be sharing tips and other information that I was able to pick up on the day. I will divide this into the different days and all the sessions that I was able to attend.

Thursday Session

I attended Rob Farleys Advanced Query Tuning training on Thursday, it was a tough start to the day as my train to York got canceled as soon as I got to the station, ok its not too bad I was offered another ticket to get on the train leaving in about 3 minutes, hooray !! It was only the beginning of a tough start, I mistakenly got into a fancy steam train to Newcastle, it looked like one of the Harry Potter trains, something just didn’t feel right when I got into it. Luckily for me, I was able to get out of the train in time before it departed as it would have been a slow steam train all the way to Newcastle and there are no stops before then.

Ok finally, I missed my train and got on the next one 30 minutes later. I only missed about 30 minutes of the training and to be honest I choose to attend this course because I believe that query tuning is an art that one needs to master by constantly learning and constantly being challenged to tune queries, so my expectations were that I will learn something that I have not seen or heard of before.

Rob Farleys Session

During this session I was expecting to be thought some advanced SQL topics, below are the notes that I took during the session.

  • Investigate the use of a full join
  • Always check on the execution plan properties as it does have some useful information on it.
  • Do some more research on join types, nested loops, merge join, hash join etc.
  • Using a distinct and group by with a scalar function, the thing to look out for here is that if one uses the distinct keyword, all the records are passed to the function but if a group by is used, it reduces the number of records that is passed to the scalar function.
  • One should not use the between clause because of the issue with 23:59:999, one should use < @date and >@date instead. 
  • Indexes can sometimes use a different collation and one should be wary of this.

 

Brian McGeHee

Health Checks for SQL server. 

This session sounded useful as I have a set of checks that I would normally carry out on database servers that I manage every morning. These includes the following. 

  • Checking connectivity to the server.
  • Checking the error log
  • Checking the backups
  • Checking SQL replication
  • Checking waits on the database

The point of attending this session was to see if there are any other checks that I may be missing out and ones that I can easily automate. What I have done is automated this checks using a combination of SSIS for collecting the data from multiple servers and using SSRS for the web presentation for the reports.

Notes that I took down on the day includes the following. 

  • Check Brian McGehess’s website for more information about the daily checks.
  • Extract baseline information about your servers.
  • Create a best practice manual
  • When commissioning a new server, ensure that firmware is always up to date.
  • Look at collecting event log across multiple servers (I want to automate this using powershell)
  • Using PAL (Nice handy tool)
  • Download SQL server tacklebox
  • Download Brent Ozar’s blitz script

Briand McGeHee’s website can be found on: www.BrianMcGehee.com/healthcheck.zip

Brant Ozar’s SQL Server and SAN session

This session was to discuss managing a SQL server instance on a SAN from a storage perspective, things that a DBA should look out for and other tips. I have been to several of Brent’s presentation, and he generally a good speaker. So far its always been fun. 

Notes that I took down on the day includes the following.

  • Windows task manager on virtual machines should not be relied upon, in fact he said its in a mess.
  • Run CPU-Z to get some visibility on physical hardware, check www.cpuid.com
  • Minimise the number of virtual cores
  • Strip out unnecessary virtual hardware, i.e. floppy drive, CD drive etc.
  • Remove all background services
  • Monitor for throttling.
  • Avoid affinity masking

 

Brent also touched on some VMware memory best practices

They include the following

  • Setting memory reservation
  • Set minimum and maximum memory allocation appropriately

HyperV 

  • Avoid dynamic memory for SQL server
  • VMware counters to check out include the following.
  •  % processor time
  • Host processor speed
  • Limit (mhz)
  • Reservation (Mhz) – ask your sysadmins about reservation on vmware hosts.

VMware memory counters

  • Memory limit
  • Emory reservation (MB)
  • Memory ballooned
  • Memory swapped (This should always be 0)

Posted in Professional Development, SQL 2008, Tips | Leave a Comment »