A Day in the life of SQL Mirroring

This past week I have been working with a client to setup mirroring on a SQL 2014 instance.  I realize mirroring is a deprecated feature; but many DBAs and companies still use it as a fail safe line of defense. And the fact that Mirroring has been a “deprecated feature” since SQL 2012 and is still available in SQL 2016; I think it might be around a little longer.

I am writing about this not to call out any client on the use of Mirroring or to promote or condone mirroring but to remind me of the things to look for when setting up any mirroring environment.

The Task at hand

My client was upgrading from SQL 2012 to 2014.  The current SQL 2012 instance was mirrored to a second server.  I was to reproduce the mirroring on the 2014 instance in preparation for the rollout to production.

This particular setup was problematic for me because of Windows Firewall rules and having multiple instance (heck multiple versions) of SQL installed on the same server.

Server Setup

The following version of SQL were installed on the same server:

  • SQL 2008 R2 (default instance)
  • SQL 2012 ( named instance) mirrored with a second server
  • SQL 2014 (named instance)  this was the server I needed to setup with mirroring to reproduce the SQL 2012 environment.
  • Windows Firewall was basically configured to close everything unless a rule specified otherwise.

This list of things to check may not apply to all configurations and all mirroring setups.  It is just some things that prevented this setup for succeeding.

  1. With multiple instances and connecting via Named Pipes, it is very important to have the “Named Pipes” protocol enabled in SQL Configuration Manager.
  2. With multiple instances SQL Browser becomes very important, especially if you do not want to use Port Numbers in your connection string.   Here is the important part, check the Windows Firewall rules to ensure UDP Port #1434 has both an Incoming and Outgoing rule.
  3. Mirroring requires its own Port for communication as well.  Incoming and Outgoing Windows Firewall rules will have to be created to ensure communication between the two servers.  By default this is TCP Port #5022.  However, because the SQL 2012 instance was already using this port for mirroring, I had to configure SQL 2014 with port #5023.  Again, another firewall rule. Actually, I just added the port to the existing firewall rule.
  4. The last piece that was problematic was permissions.  Because Principle and Target each had their own domain service account, each account needed to be added to the opposite server with the correct database permissions. 

Once all of this was figured out and configured correctly, the mirroring session configured and started up with no problems!


“Stretching” my SQL Skills

Philosophy has always intrigued me and René Descartes is one of my personal favorite philosophers.  Cogito ergo sum “I think, therefore I am”.  The moment we stop thinking is the moment we stop existing. So, today I decided to learn something new.  I wanted to setup a Stretch Database in SQL Server 2016.

Just about everything in SQL Server can be accomplished by either the SSMS GUI or Transact SQL commands.  Anytime, I attempt something for the first time I always use the GUI, then learn the T-SQL commands.

I began by right clicking on my [StretchTest] database and selecting TASKS >> STRETCH >> ENABLE.Image1

This will launch the “Enable Stretch Wizard.  First step in the wizard is the the Introduction.  First thing I noticed was the the statement: “Once the data is migrated to Azure, unique and primary keys will not be enforced on Azure”  Really?  I wonder what else will be “not enforced” or not compatible with Azure.  Time to look up my friendly neighborhood BOL, Limitations for Stretch Database. Wow, that’s a good number of limitations; however this is a simple test so I am not worried.

Next we have to connect to an Azure Subscription. The wizard step allows you to configure your SQL Azure database environment, select the Azure region (always pick one closest to you) and you get to choose between a preconfigured Azure server or allow the wizard to create one. I chose to allow SSMS to create a new Azure server. Next you have to create a database master key (DMK) to protect your Azure data.  And finally you need to provide your IP Address (remember your external IP, not your machine IP) so SSMS can setup firewall rules for you.



Then you get the customary SQL Summary page before you execute the commands. Now that my database has been stretched, I added a table and data using this schema:

CREATE TABLE [dbo].[tblUsers](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [varchar](50) NULL,
	[LastName] [varchar](50) NULL,
	[Email] [varchar](100) NULL,
	[DateEntered] smalldatetime null,

I not only wanted to test the Primary Key limitation; but I also wanted to test the Filtered Stretch on the Date Entered Column. I populated the table with 11,000 records using the website http://mockaroo.com.

After the tblUsers table was populated, I enabled the Stretch Table wizard by right clicking on the table and selecting STRETCH >> ENABLE.  After the Introduction screen you get to select which tables you would like to stretch to Azure.  The GUI screen shows you all the tables and if there will be any “warnings” about stretching, the option to filter, number of rows in the table and the size of the table. 


The first time I executed this I did not use the Stretch Filter and stretched the entire table.  It worked perfectly.  I could use SSMS to connect to my remote SQL Server and query the Azure table directly and could see there were 11,000 rows.

Executing a simple SELECT query against the local database produced this Query Execution plan: RemoteQuery

As you can see, we now have a new path in our query plan with an operator called “Remote Query”.  Basically the local server queries the remote query then using the local Primary key Concatenates them back together to produce the desired result. So can we update the data?


Nope, sure can’t.  Once the data lives in Azure, the data is READ ONLY.

Next I wanted to add the filtering on Table3the DateEntered column so I can edit more recent data.  I disabled Stretch and re-enabled it using the filters this time.  I received error after error after error.  According to BOL this query is not a valid filtering query.  According to BOL, the date parameter needs to include the format value.  At least that was the case with mine.

CREATE FUNCTION dbo.fn_example5(@column1 datetime)
RETURN  SELECT 1 AS is_eligible  
        WHERE @column1 < CONVERT(datetime, '1/1/2014')  

/*  CORRECT  */
CREATE FUNCTION dbo.fn_example5(@column1 datetime)
RETURN  SELECT 1 AS is_eligible  
        WHERE @column1 < CONVERT(datetime, '1/1/2014',101)  

After initiating the filter, I queried the remote server directly and noticed there wereRemoteQuery_AzureRows

6713 rows found in Azure


and 4287 rows found in the local database.  Exactly what I expected.  Again, the query execution plan has to use “Remote Query”  and Concatenate the results to produce the desired results.


Final Thoughts on Stretch Database

The SSMS has designed the Stretch Wizards to be very effective and very easy to use.  However, when you “Disable” a stretch database it does not delete the table/database in Azure which means you will still incur charges for storage. And if you re-enable Stretch on a database it creates a 2nd database in Azure. 

With the READ-ONLY limitation from the Azure side of Stretch database, you better make 100% sure you don’t need to edit anything in Azure. The only way to make any changes to the Azure side of the data is to Disable and “bring the data back” to the local machine and this incurs charges on the Azure side. Then you make your changes to the data, and re-enable it for Stretch.  Which again, created another database in your Azure server.

The pricing for Azure SQL Stretch database seems to be a little steep. At the lowest performance level offered, it is ~$1860 per month!  Microsoft calculates what they call DSU (Database Stretch Units) which “represents thee power of the query and is quantified by workload objectives: how fast rows are written, read and computed against.”  https://azure.microsoft.com/en-us/pricing/details/sql-server-stretch-database/

All in all, Stretch Database is a very cool feature for archiving older data that will not change.  But only if  you can afford it.  I would think using file groups with partitioning of data on separate drives could be a more cost effective solution for archiving than Azure stretch database. A very good learning experience. 

My first time, lessons learned!

This past weekend, I had the privilege of doing a presentation at SQL Saturday #514 in Houston Texas.  I had been to other SQL Saturdays before as both a participant and a “volunteer”; but I had never have spoken before.

Speaking in front of people is usually no big deal to me.  I have been designing, presenting, hosting and administrating all types of training for the Boy Scouts for many, many years.  I have taught Scouting newbies how to tie a knot to presenting on topics like “Project Planning and SMART goals” (email me if you want to know what SMART goals are).  I have even coordinated an all day training event very similar to SQL Saturday for my Council called University of Scouting.

But what was different about this experience was the topic.  I have been active in the Boy Scouts since I was eleven.  I know Scouting information, concepts, and topics backwards and forwards. I have been facilitating trainings (presenting) since I was 15. So Scouting is 2nd nature to me.  But presenting on SQL Server topics was somewhat nerve wracking.  For me it was a test of not my presenting abilities; but my SQL DBA abilities.  As an “Accidental DBA” I have always questioned my abilities with SQL Server.  Everything I have learned about computers I have learned my self, so I am always 2nd guessing myself. 

As a “First timer”, I thought it would be best to cover Beginning Level topics.  My thought was 1) I knew the material and 2) after reading Tim Ford’s #EntryLevelChallenge it motivated me to remember my fellow newbies.

I won’t go into the topic of my presentation today as this is more about the experience.  The audience, which I had 14 in attendance, was very attentive and I don’t think I put anyone to sleep!

I do need to work on my demos.  They all worked as expected; but switching between laptop and projector, throwing SSMS on the projector screen, changing screen settings from duplicate to extend was all too much.  There has to be a better way to switch between PowerPoint in presentation mode and SSMS to do demo. So I will practice that.

I thought I did best on time management.  The time allotted was 60 minutes, I said “Thank you for your time” at 59 minutes and 30 seconds. 

Over all the experience was well worth it. By the responses I received from the speaker evaluations, I think I did an OK job.  I had no major criticisms except to slow down, I assure you that was nerves at the beginning. 

SQL Saturday

This Saturday is SQL Saturday #514 in Houston, TX. 

And this Saturday, I will be doing my first presentation at a SQL Saturday.

Luckily my presentation is in the morning so I can get it over with and enjoy the rest of the day. Otherwise, I probably would be stressing about it all day!

Hostile Takeover is my presentation on what do you do when handed the keys to a SQL server you have never seen.  What information would you want to know and more importantly HOW do you get that information!

I will show some easy to use tools and scripts developed by people way smarter than I am.  Heck, I will even show you how to hack into a SQL Server!  As far as my website goes, I will update my slide deck and scripts repository after SQL Saturday!  And hopefully I will find the time to upload the information to the SQL Saturday website!

The End of an Era

A Legend is dead.

SQL Server 2005 is no longer supported.  Today is the first day that DBAs and companies cannot call on Microsoft to help with or support a SQL Server 2005 Instance. (of course I am sure M$FT will gladly accept some money for additional support).

But many companies will still run with this stalwart and continue to use it until it dies.  SQL 2005 has proven over time to be a stable system capable of doing a yeoman’s job.

Nine years ago, SQL Server 2005 revolutionized the way DBAs interacted with SQL Server.  SQL Server Management Studio (SSMS) brought a new method of administering and programming SQL server.  The Graphic User Interface (GUI) based program was miles ahead of the old Microsoft Management Console (MMC) plugin, SQL Enterprise Manager.

SQL Server 2005 was the first major overhaul of the SQL Engine, it introduced among other things XML data type, Common Language Runtime integration, and most importantly Dynamic Management Views (DMVs). 

While some say “death to the dreaded GUI, long live T-SQL”, I for one, as a visual person, love the GUI.  Yes, I always find myself using the “script” feature to learn the actual T-SQL and to execute the T-SQL for faster execution, but some tasks are made simpler using the GUI instead of T-SQL.

It definitely seems the development of SQL Server 2005 was directed by actual SQL developers, administrators, and users. 

Over the years and versions, many features have been added to SSMS to even make it a better application: detailed graphical execution plans, intellisense, Central Management Server, the list could go on.  I for one am excited that the Server Group has handed off SSMS as a separate tool because hopefully it can be placed on a more aggressive update schedule.  Hopefully the development of SSMS will keep with pace of our needs.

So here’s to SSMS and it continued improvements and features.Drinking-Cheers

So to recap, here is a list of SQL Server version still under main stream support:

  • SQL Server 2012 – support ends 7/11/2017
  • SQL Server 2014 – support ends 7/9/2019

That’s it, two versions!!  (I did not include SQL Server 2016 on this list, because technically it has not been released for general use.)

Extended support, which only includes security updates is a little better:

  • SQL Server 2008 & 2008 R2 – extended support ends 7/9/2019
  • SQL Server 2012 – extended support ends 7/22/2016
  • SQL Server 2014 – extended support ends 7/9/2024

Knowing how long it takes to get capital expenditures approved and how expensive SQL Server can be, you better start budgeting  to replace SQL Server 2008 & 2008 R2 machines now! 

B2B: Maintenance Plans

This post is part of the challenge by  Tim Ford (b|t) #EntryLevelBlogChallenge.  Which of course is a personal challenge to technical bloggers to write one entry-level blog post a month over the next year and tag it on twitter with #iwanttoshare

All of by #EntryLevelChallenge post are tagged under “Back-2-Basics” category.

Today, I would like to talk about Maintenance Plans. This SQL feature is designed to allow DBA to automate maintenance  tasks.  Maintenance plans can be found under the “Management” folder in SSMS once you log into SQL Server.  Right Click on the “Maintenance Plans” folder and select “New Maintenance Plan”











The quickest and easiest way to set this up is to use the “Maintenance Plan Wizard”.  This is a step-by-step, check-box tool to setup everything possible with a database.  The First Step allows you to name your maintenance plan and allows you to create the schedule you want to plan to execute on.  This can either be one schedule for all jobs or individual schedules for each task.  I would recommend individuals schedules because of the maintenance tasks can interfere with other tasks.

The next step to the wizard is where you actually pick which tasks you would like to run.  Each task is given a brief description on the bottom in yellow.

  1. Check Database Integrity – uses the DBCC CHECKDB command to check the integrity of the database
  2. Shrink Database – AVOID USING THIS AT ALL COSTS.  Shrinking databases on a regularly schedule actually does more harm than good to a database.  If you are running out of drive space and shrinking databases must be done, please do it manually.
  3. Reorganzie Index – This task will defragment and compact your indexes on tables and views.  There can be a performance gain, but the down side to this task is it is an “all-in” mentality.  It will perform the task on an index whether it needs it or not.
  4. Rebuild Index – this task actually complete wipes index out and rebuild them so they are 100% new again.  This task is also “all-in” approach and can be very I/O intensive.
  5. Update Statistics – statistics are the back-bone of the how the SQL Engine finds data. The SQL Shot3Engine uses statistics to determine the distribution of data in the data files.  If statistics are out of date SQL may have to work harder to find data.
  6. Clean Up History – the execution and results of SQL Maintenance plans are stored in the MSDB system database.  If this database is not kept in check it is feasible that it can grow uncontrollably.
  7. Execute SQL Agent Job – if you have built your own SQL Agent Job to do maintenance then you can actually execute it during this plan.
  8. Back Up Database (Full) – this does exactly what it says.  This will perform a SQL Server FULL backup of the databases. This should be done on all databases including system databases.
  9. Back Up Database (Differential)—Differential backups are taken using this tasks.  This can only be used for databases in FULL or BULKED LOGGED recovery model.
  10. Back Up Database (Transaction Log) – transaction log backups are crucial to “point-in-time” recovery of your database.  It can only be used against databases in FULL or BULKED LOGGED recovery model.
  11. Maintenance Cleanup Task – this task should always be selected because it will cleanup any files left over by the execution of the plan.

The next step of the Wizard allows you to set the order of execution for the individual tasks you have selected.  I prefer to always do Database Integrity Checks first, then indexes, then backups.

Finally you get to select which databases you would like this maintenance plan will execute against.

Depending on what tasks you select, there maybe follow up secondary tasks that you need to complete.  Where to store reports, what files to clean up, etc.  And then, click FINISH!

The Maintenance Plan Wizard is a simple way to setup maintenance plan.  But do not fall into the trap of the “click-and-go” configuration.  This is where you click all the check boxes and just keep clicking NEXT.

Please follow up and learn what each setup does and determine if you really need all the tasks.  Many experienced DBAs will use custom scripts and custom maintenance plans for each type of database to ensure the SQL Engine does as little work as possible.  The less time your SQL Server does performing maintenance tasks, the more time and resources it has to perform data tasks.

Newer is not always better!

The other day working with a client, I was attempting to move the TEMPDB data files to a new Solid State Drive (SSD) drive!  I was stoked to see the performance gain with this because I have yet to work with SSD in a SQL Environment.

Of course I hit a problem or I wouldn’t be writing a blog post about it!

it is on a volume with sector size 8192. SQL Server supports a maximum sector size of 4096 bytes

Excuse me?  SQL Server can’t support a drive? Well needless to say this was problematic.  After extensive research, the client purchased the latest and greatest SSD.  But unfortunately neither of us paid attention to the details.  As always, the Devil is in the Details.


Apparently MS SQL Server cannot use a physical sector size larger than 4096.  It can use smaller, but not larger!  OK, next step what exactly are we working with.

Using FSUTIL command (fsutil fsinfo sectorinfo) I did confirm the physical sector size for the SSD was 8192.  Repeated attempts to re-partition and/or re-format was not successful and it should not have been because the “physical” sectors are 8192, not the logical sectors.

Physical sector size is determined upon manufacturing and firmware. My client had to go back to the vendor to see if firmware was available to change the physical sector to 4096! 

Once again, lesson learned!  Be specific and diligent in your hardware details when purchasing SQL Server hardware.