From the database: Query to present usage report for #sqlserver databases

By | August 17, 2014

This week I have been looking at the usage of a large number of SharePoint content databases. By *large* I mean LARGE. Over 300 spread over a number of clustered instances.

“Why should we care?” I hear you asking, “sounds like a normal day at the SharePoint office to me…”

You’re correct, pretty normal, but I’m not spending quite so much time in the weeds of what’s beneath SharePoint recently and so coming back to some mundane DBA stuff was rather refreshing.

“Yeah, so?”

I needed a quick way of grabbing some data about the databases as there were some inconsistencies believed to exist in terms of sizing, recovery models, usage and other aspects of the databases. Historically cursors have been the way to get information about all the databases on a given instance. They’re simple, effective and easy to construct, but at a cost.

From a resource perspective, cursors can be quite expensive and running queries repeatedly within cursors across all databases (effectively looping as a “For…Each” through the instance) had the potential for service impact which was not acceptable to the customer.

Enter Dynamic SQL, the “new” (yeah, I know. It’s been around for years…) and “correct” way of doing this kind of thing which can be likened to passing parameters into a PowerShell function that is being repeatedly called.

By no stretch am I am SQL Server wizard but I have been around it long enough to *almost* know my way around the system tables,views and DMV constructs so knew that I could dig what I needed out of what was already in place and stuff it into a temp table. Wrapping the query as Dynamic SQL which executes against the sys.databases catalogue view allows me to wrap my query as Dynamic SQL to execute on a per database basis. Simples!

What is interesting here is that there really is no SQL magic here. It’s simple stuff. I’ve made it seemingly more complex as I want to display the information in a ready to use way so have added some clutter in the form of logic (to determine if files are database or log files for instance) and math (to make things render as MB as opposed to the OOTB 8k pages that SQL Server works in).

Enough of the narrative, let’s look at what’s what.

I know there is no commenting in here and I’m sure there are optimisations to make, but for my needs, I’m happy.

Comments welcomed as always!

more to follow…

Leave a Reply

Your email address will not be published. Required fields are marked *