Friday, 5 May 2017

Compress your database tables

SQL Server has offered data compression since SQL 2008. It is a very good feature but, as with almost everything SQL Server, it needs to be deployed judiciously. Data compression has been written about and reviewed by many SQL experts who are much more qualified and knowledgeable than me, so I won't go into it too much.

First, a quick summary of what the feature does as well as its benefits and effects:
As the feature's name suggests, when it is enabled on a table and/or its index(es), SQL server compresses (or shrinks) the data before it is written to the database. There are two types: ROW and PAGE.

  • Benefits
    • Disk space savings
      • Higher space saving for PAGE compression
      • Lower space saving for ROW compression
    • Memory usage efficiency gains
    • Logical and physical read performance: more data fits in fewer pages

  • Considerations
    • Increased CPU cost
      • Higher CPU cost for PAGE compression
      • Lower CPU cost for ROW compression

    Personally, I feel (and so does Brent Ozar) that the benefits outweigh the costs. Most of the SQL Server instances I have managed were much more disk bound than CPU bound. So if your server is running at an average of 20%-30% CPU and your I/O wait stats are high, it's a no-brainer in my mind.

    Additional resources:

    Now that we know about what data compression is, let's talk about how to implement it. The second link above describes how to compress tables and indexes. But what if you'd like to evaluate and compress your whole database. Well, I stumbled across a stored procedure by C. Meuleman ([dbo].[_Compress_Database]), which I downloaded from Glenn Berry has one as well, but it doesn't do quite what I want: Estimating Data Compression Savings in SQL Server 2012

    It is a very nice script which loops through all the tables and indexes in your database and, based on the read/write thresholds you set (or just use the defaults) gives recommendations for which compression type to use for each object. I've used this stored proc many times. It is a real time saver and a good way of quickly getting an idea of what tables will get the best storage benefit based on their usage profile. It prints out the ALTER TABLE/ALTER INDEX scripts and there is even a parameter to state whether you want to immediately run the compression.

    The one thing I didn't like about the stored proc was that it needed to be created in the database that was being evaluated. It also created tables in that database. I prefer to leave application databases alone. Even if you are carrying out the evaluation on a non-production server (which would be the preferred method) you are adding objects to a database that may have ongoing development being carried out against it. And you then need to be careful about cleaning up after yourself and so on. 

    My database team have our own DBA databases on both production and non-production databases. We prefer to put any tools like this stored procedure on this database. That way, if we create a mess, it's not affecting anyone else.

    With that in mind, I altered the above mentioned stored proc so that you can create and run it on a maintenance database and specify which database you want to evaluate. You can download the script from Google Drive.

    I hope you find this useful.

    Friday, 2 September 2016

    Query To Find SQL Audit Details

    I've been deploying a SQL Server auditing solution for our new SQL 2014 estate. I know, we're a few years behind. But if you saw some of the legacy systems I work with you'd understand that migrating to SQL 2014 is quite a coup!

    Anyway, I found a really great audit solution that I have used as a base on Colleen Morrow's blog. I will detail what I have added to her solution in a later post. But, for now, I would like to share a query that displays some basic information about existing audits, both at the server and database level.

    I put this query together for the purposes of documentation. Our 2014 estate is getting large fast. And with so many installations, we need to keep track of what is installed and configured on all our servers.

    The script below UNIONs two queries:
    • One for the Server audit specifications
    • One for the database audit specifications
    The database audit specification is a dynamic SQL statement due to the fact that it needs to interrogate the system tables of individual databases for the audit details. I have put in explicit COLLATE hints to cater for servers where databases are not all in line with the server's collation.

    Here's a sample result set:

    Monday, 1 February 2016

    Restarting a service with PowerShell

    We use Redgate's SQL Monitor to keep an eye on our mission critical servers. I like the product. It is reasonably user-friendly, reliable and it helps my team monitor our servers efficiently. I've used other products before and I think SQL Monitor compares favourably. No, I'm not paid by RedGate!!!

    With that in mind, my team has been struggling a bit with an ongoing issue: every Monday morning one of the SQL Monitor processes was running amok and maxing out CPU! This was strange, as the product is generally pretty stable. After some investigation, we realised that the security team had started performing regular vulnerability scans over the weekends. Part of this test involved intentional failed login attempts. SQL Monitor can't seem to handle this, and the web server, specifically the "xsp4" process goes haywire and trashes the CPU.

    Monday, 7 December 2015

    Databases With No Recent Backups

    I've written the script below to easily identify databases that have not had backups taken within a certain amount of time. This could be useful if you need to build your own monitoring. However, even if you use a third party monitoring tool,as I do, it is helpful to run this query on the server for which I've received an alert.

    Many of the servers I manage have a large number of databases. The alerts I receive for overdue backups are database specific. But, in general, if one database is overdue, chances are something has happened on the server. So this query can give me a quick overview.

    Wednesday, 25 November 2015

    Script to Alter File Growth

    I recently inherited a large estate of SQL Servers. Over the years, before I started, it grew organically and was managed by an insufficient number of accidental DBAs. As a result many SQL server instances were built and left with their default settings.

    One such setting is file growth, which, tends to be set either at a growth rate of 10% or 1MB. Given the number of databases that can be on a single server I wrote a little script to aid me in making the changes.

    I realise that every server and database has different requirements. I am using this script to get my databases and servers to a basic level of "best practice" and avoid any performance issues that may arise.

    The script below sets the database file growth as follows:
    • Finds databases with files set to grow by either a percentage or 1MB or less
    • Calculates the file growth rate as follows:
      • 100MB for files < 1GB
      • 1024MB for files > 10GB
      • File growth value set to 10% of the file's size when file is between 1GB and 10GB (rounded to the nearest 100)
    Run the script then copy and paste the results into another SSMS window. It is best to review the resulting scripts to be sure they will do what you expect them to.

    As with all my scripts, this comes with no guarantees. Any scripts should be tested before put into a production environment.

    Friday, 11 September 2015

    Finding When A SQL Agent Job Ran

    A quick post for today since I haven't added anything in quite a while.

    Recently, I was investigating a deadlock that was occurring, like clockwork, on a particular server. A trace revealed that it was being executed by the SQL Server Agent account. The host machine that executed a query involved in the deadlock is our SSIS host. Therefore, there are loads of jobs with a dizzying array of schedules to weed through.

    Friday, 24 July 2015

    SQL Server Optimization

    This post aims to aggregate a lot of the best practice or otherwise recommended configuration for SQL Server performance. It is meant as a summary or quick-reference sheet. All of these concepts are discussed at length all over the internet, so I will not spend much time explaining them. 

    As with any recommendations of this sort please use them carefully and test before deploying into a production environment. Also, depending on your specific environment's configuration, not all of the below may be advisable or bring about clear benefits. To quote Jonathan Kehayias: "While there is a lot of good guidance available for how to best configure SQL Server, the specifics of any given implementation is a very big “It Depends…”What this post should achieve is providing the reader with an awareness of features and options that influence a SQL Server instance's operation. 

    Tuesday, 26 May 2015

    Add an Operator to All SQL Server Agent Alerts

    I was investigating a new server I'd been assigned, looking for gaps in configuration using the trusty sp_Blitz tool. The organisation that owns this server instance is small and doesn't use third-party monitoring. I decided to add some basic alerting as recommended by Brent Ozar and Co. They've kindly provided a script to do this: Blitz Result: No SQL Server Agent Alerts Configured.

    A little later on I realised that I needed to add an additional operator to these alerts and was loath to step through them one at a time and tick the appropriate box. So, I knocked out a quick and simple script (I've included two versions, one that adds the operator to all alerts and one that allows you to select a subset). I hope it is helpful.

    Tuesday, 19 May 2015

    Study Materials for MCSE Microsoft Exams 70-457, 70-458, 70-459

    I passed the 70-457! Now studying for 70-458...
    I passed the 70-458! Now studying for 70-459, booked for Jan 29, 2016...

    I am studying for the MCSE Certification and will be taking the upgrade from MCITP 2008R8 tests 70-457, 70-458 and 70-459. After trawling the web for study materials I stumbled upon a series of posts on by Carla Abanes. It is a great resource, touching on all the topics covered by the 70-457 exam. I've collated the links below:

    Review Materials for 70-458

    Exam 70-458 Transition Your MCTS on SQL Server 2008 to MCSA: SQL Server 2012

    Review Materials for 70-457

    70-457 Reviewer #01, Rank Functions
    70-457 Reviewer #02, What is Columnstore Index?
    70-457 Reviewer #03, Planning an Installation of SQL Server 2012
    70-457 Reviewer #04, Implement a Migration Strategy
    70-457 Reviewer #05, Configuring SQL Server 2012 Components
    70-457 Reviewer #06, Configuring and Managing Database in SQL Server 2012
    70-457 Reviewer #07, Resolving Concurrency Problems in SQL Server 2012
    70-457 Reviewer #08, Implementing Clustered Instance in SQL Server 2012
    70-457 Reviewer #09, Auditing SQL Server 2012
    70-457 Reviewer #10 Managing SQL Server Agent
    70-457 Reviewer #11 Configuring and Maintaining a Backup Strategy
    70-457 Reviewer #12 Restoring SQL Server Databases
    70-457 Reviewer #13 Managing SQL Server Logins
    70-457 Reviewer #14 Database and Application Roles
    70-457 Reviewer #15 Implement Database Mirroring
    70-457 Reviewer #16 Implement AlwaysOn in SQL Server 2012
    70-457 Reviewer #17 Database Replication in SQL Server 2012

    Friday, 15 May 2015

    SQL Login and User for Performance Tuning

    I've run into an interesting dilemma: as a new staff member in a security conscious organisation, I can't really get any work done. There is, however, a lot of work a DBA can do in terms of analyzing SQL Server instances right from day one. There are lots of best practice recommendations that can be made that are application or db design agnostic. Alternately, it can be possible to investigate the database design without access to the underlying data itself. With that all in mind, I have put some scripts together that creates login/user permissions and roles that can give a SQL consultant useful, but security minded, access to a SQL instance.