A quick PSA on the behaviour of Serverless Azure SQL DB space reporting in the Azure Portal.
I recently had to shrink a large Azure SQL DB for cost savings after a deleting a lot of data and noticed the Portal space usage pie chart seemed off. I was pretty sure it had a few hundred GB of used space and around 2TB of allocated space, but when looking at the chart on the paused database before waking it up for a shrink session, it seemed to have miraculously compacted…
Huh? Ok, maybe it shrunk itself thanks to some new Azure backend wizardry. That wouldn’t surprise me. I’ve also seen odd behaviour when shrinking Azure SQL DBs before – specifically, a piecemeal shrink script making very slow progress, only for the database to suddenly shrink 100% of excess allocated space when no shrink operation was visibly running. (That one was identified as a bug at the time.)
But…that wouldn’t explain the reduced data space used. Let’s wake ‘er up and see what’s going on…
Ok, yep, that’s over 2TB allocated as I suspected.
The Portal still showed the 78.53GB used/allocated at this point, but waiting 10 minutes and viewing again showed the correct values – 157.5GB used, 2.23TB allocated;
So, looks like sometimes you can’t trust the data storage numbers in the Portal for a paused Serverless Azure SQL DB at the moment. And as it doesn’t appear to have been reported yet, that’s my first Feedback item. Woohoo!
Hopefully you already know everything about your SQL estate, including what services are installed and what’s running them, either because it’s so small you just know, or, preferably, you have some kind of CMDB. But what if you don’t? Powershell comes to the rescue, of course.
This post details a script to capture basic information – service name, account, start mode and status – about the database engine, Agent and any SSRS/SSAS/SSRS installs. It requires remote WMI connectivity and, for SSIS, SMB access to enabled administrative shares on the drives where the service is installed.
(Note – if all you’re after is the Database Engine and/or SQL Agent info, and you’re running SQL 2008 R2 SP1 or higher, no need to carry on reading – just use the sys.dm_server_services DMV instead, it’s easy.)
To begin we need some kind of data source containing both the hostnames and instance names (MSSQLSERVER if default instance) of the servers in the estate – as we’ll be connecting to the hostname but making checks using the instance name. Either stored in a table;
Then we can loop over the servers and use the Get-WmiObject Win32_Service cmdlet. For the database engine, Agent, SSRS and SSAS this is straightforward – we’re filtering on the service name looking for a reference to the instance, then disregarding any CEIP services and pulling out a few pieces of information. For example, a snippet of the script checking for a SSAS install;
SSIS is a bit trickier. You may have noticed that, unlike for the Database Engine, SSRS or SSAS, the instance name is not part of the service name;
So we can’t just filter on the service name to check which instance the service is installed as part of. Instead we need to find the MsDtsSrvr.ini.xml file associated with the service (by using the service PathName), open it up as XML (this is where the requirement for SMB and administrative share access arises) and look at the DtsServiceConfiguration.TopLevelFolders.Folder.ServerName element;
An example output from the whole script, ran on a single server;
This shows I have the database engine and agent running from MSAs, and two extra services installed – SSAS running as a local service and SSIS running as an MSA, all set to auto-start and only SSIS not currently running.
The entire skeleton script is below (github link), using the CSV file example for retrieving host/instance names as a base and simply writing the output to console – it’s easily modifiable to change the source and/or save the output to a file or table.
Introduced in SQL Server 2008, the date datatype brought storage of dates in the YYYY-MM-DD format – 0001-01-01 to 9999-12-31, 3 bytes. This was an addition to the existing types of datetime – 2021-03-09 12:20:16.021, 8 bytes – and smalldatetime – 2021-03-09 12:20, 4 bytes.
It solved a range problem (datetimes can’t store years prior to 1753, smalldatetimes1900) that previously required hacking around, but so did the new datetime2type, so that’s not why I’m nominating it my favourite data type for T-SQL Tuesday.
The reason is simply what it is – a date without a time component.
Often we just want a date and don’t care about the time at all, nevermind the time to millisecond accuracy. Think transaction dates, staff joining dates, DOBs, etc. But the existing types foisted a time upon the column along with extra bytes of storage, and I couldn’t begin to count the times I’ve had to type this pattern;
WHERE DateTimeColumn BETWEEN '2020-03-09 00:00:00.000' AND '2020-03-09 23:59:59.999'
-- or if performance is not an issue
WHERE CONVERT(DATE, DateTimeColumn) = '2020-03-09'
With date it’s simple;
WHERE DateColumn = '2020-03-09'
Three cheers for this three part, three byte hero.
Now, I was still at college when date was born and years away from touching SQL. One might think that 13 years later the code I’ve been working with recently as a dev/DBA would have plentiful usage of date when appropriate…
Unfortunately not. This would have been very different if date had been in SQL 2005 or 2000, but it wasn’t – so the result is oodles of datetimes left lying around because the schemas had already been designed. Then they hang around not only because existing code is extended over the years, but because people doing entirely new deployments copy conventions from existing codebases.
It’s a great example of a ghost of a missing feature still haunting the present. I wonder how many GB/TB (/PB??) on the planet is wasted by datetimes with 00:00:00.000 time components right now.
The login auditing setting on a SQL Server instance controls the auditing of logins – either all, successful only, failed only or none. It’s set via SSMS on the Server properties dialog;
and is responsible for these types of message in the log;
You might want to change this for several reasons – perhaps you have strict auditing requirements so want everything logged, or perhaps you only care about failures – especially if you have a noisy application that floods the log with successful logins.
It’s straightforward enough to change in SSMS – though note that a service restart is required for changes to take effect – but what if you want to change this setting on a bunch of servers? Fortunately, it can be programmatically read/changed using the (undocumented) xp_instance_regread and xp_instance_regwrite extended stored procedures, reading from/writing to the Windows registry value AuditLevel (specifically, the value in the correct key for the instance the proc is executed from) – that the setting in SSMS actually represents;
This key maps to the following values;
0 – None
1 – Successful logins only
2 – Failed logins only
3 – All logins only
To retrieve the value, we can use xp_instance_regread ;
DECLARE @result INT
EXEC xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'AuditLevel', @result OUTPUTSELECT @result AS 'AuditLevel'
To set it, we use xp_instance_regwrite. An example of setting it to 2 (Failed logins only);
So by calling this procedure we can script out a read/change to the setting to multiple instances at once using Powershell. (Yep, we could directly modify the registry with Powershell and skip the call, but doing it this way benefits from the wrapping of the registry modification in the SQL extended proc, and thus the correct key for the instance being automatically used thanks to the ‘instance’ versions of the reg read/write procs vs the standard xp_regread/regwrite)
Retrieving the key is a straightforward iteration over instances. In this example I’ve populated an $instances variable with an array of instance names, but this can be substituted with reading from a text file or database;
After modification, the first script can be re-ran to confirm the modifications – and don’t forget a service restart is required to kick the setting into action.
Disclaimer – the registry-editing procedures used here are undocumented, and editing the registry is generally best avoided – any such code should be tested thoroughly before use in production.