Monday Monitor Tip: Compliance Checking for Old Versions

A customer recently wanted to know if any of their instances were too old and out of support. This was for a compliance purpose, and they had the need to show a report to management of when instances were out of compliance with policies.

This post shows how you can do this in Redgate Monitor.

This is part of a series of posts on Redgate Monitor. Click to see the other posts

Compliance Templates

This is an Enterprise feature, but it allows the administrators of Redgate Monitor to define templates for how the various servers and databases ought to be configured. Deviance from standards, or compliance with standards is often something auditors care about you documenting.

This is very easy with Redgate Monitor. Let’s see how this works.

Under the Security tab (appears in Enterprise edition), you can see there is a Configuration Compliance item. Click this.

2024-08_0127

This brings up the Configuration Compliance screen. This starts with Servers (highlighted with a bar to denote this tab, but there are also databases and Compliance templates. We want to choose compliance templates.

2024-08_0126

This tab shows the various templates that exist already in the system. I’m looking at monitor.red-gate.com, which has three templates configured already.

2024-08_0128

If you scroll to the right, we can see that two of these are server (instance) templates and one is a database template. We can add a new one, or edit existing ones. For this post, I’ll edit the Workload Server template.

2024-08_0129

After we click Edit, we get another set of tabs for the template itself. The default is for security, but there are also performance and environment options. Pic the Environment tab.

2024-08_0130

This brings up a list of settings. Scroll down to the bottom and you will see what settings below for Product Level, Product Version, Product version number, and Edition.

2024-08_0125

In this case, the template is checking that every server has at least SQL Server 2019 Enterprise installed. If any other version were installed, this would show as non compliant. This is useful for grouping your servers by versions.

The values entered here are the ones you would get back from the ServerProperty() function.

Summary

This post shows how you can configure a compliance template to check that the versions of SQL Server you have installed meet your requirements for a version.

Redgate Monitor is a world class monitoring solution for your database estate. Download a trial today and see how it can help you manage your estate more efficiently.

Posted in Blog | Tagged , , , | 1 Comment

Comparing an Old Running Total to Window Functions

Often I see running totals that are written in SQL using a variety of techniques. Many pieces of code were written in pre-2012 techniques, prior to window functions being introduced.

After SQL Server 2012, we had better ways to write a total. In this case, let’s see how much better. This is based on an article showing how you might convert code from the first query to the second. This is a performance analysis of the two techniques are different scales..

Pre SQL Server 2012

The old way:

SELECT Acc.ID,CONVERT(varchar(50),TransactionDate,101) AS TransactionDate
  , Balance, isnull(RunningTotal,'') AS RunningTotal 
 FROM Accounts Acc  
   LEFT OUTER JOIN (SELECT ID,sum(Balance) AS RunningTotal 
                    FROM (SELECT A.ID AS ID,B.ID AS BID, B.Balance 
                           FROM Accounts A 
                             cross JOIN Accounts B 
                           WHERE B.ID BETWEEN A.ID-4 
                           AND A.ID AND A.ID>4 
                          )T
                    GROUP BY ID ) Bal 
     ON Acc.ID=Bal.ID

What were the statistics on this? After running a few times, with STATISTICS IO ON, I get this:

Table ‘Accounts’. Scan count 37, logical reads 37, physical reads 0

Not bad. I’ve truncated out the other values as they were all 0.

Window Functions

Here is the same query written with a Window function.

SELECT
id
, TransactionDate
, Balance
, CASE WHEN LAG(TransactionDate, 4, null) OVER (ORDER BY TransactionDate) IS NOT NULL
THEN SUM (Balance) OVER (ORDER BY TransactionDate ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
ELSE 0
END AS runningotal
FROM dbo.accounts

The statistics?

Table ‘Worktable’. Scan count 0, logical reads 0, physical reads 0
Table ‘Accounts’. Scan count 1, logical reads 1, physical reads 0

The window function definitely does less work. A lot less. But how does this scale?

Performance Testing

There are numerous ways to create some test data for this. Since I have Redgate SQL Data Generator, I decided to use that. It’s simple and easy, and I added 100,000 rows first.

2024-09_0143

My results of the first query:

2024-09_0144

Lots of reads and scans. Let’s compare this to the window function.

2024-09_0145

Hmmm, both took essentially zero time less than a second. That might lead some developers to think either method is quick enough.

Let’s add 1mm more rows.

2024-09_0146

Now compare. The first takes about 15s with these results.

2024-09_0147

The window function? 3 sec, with these stats.

2024-09_0148

The comparison looks like this. First, let’s look at SSMS time

 

Old, Cross Join Window Function
20 rows 0 sec 0 sec
100,020 rows 0 sec 0 sec
1,100,020 rows 15 sec 3 sec

If we look at CPU time, then we see this:

 

Old, Cross Join Window Function
20 rows 0 ms 0 ms
100,020 rows 748 ms 250 ms
1,100,020 rows 5845 ms 2919 ms

If we look at the logical reads in total, we see this

 

Old, Cross Join Window Function
20 rows 37 1
100,020 rows 403,998 359
1,100,020 rows 6,450,895 3945

Clearly the window function is better and the better grows as the size of data grows.

Summary

This post looks at two queries and compares the performance across a few queries. These aren’t the only ones, and you might choose other types of queries, but these are both examples of how you might approach a problem using old tech and new tech.

The window function is not slightly more efficient, but extremely efficient compared to the older style method of using a cross join. As the data scales up, the difference is pronounced. While 1mm rows might not be a great test here, and you may prefer to test at 10mm or 100mm rows to get an idea of load, the fact is the Window function is much quicker and uses less resources.

If you are using older style code to perform T-SQL calculations, make some time to refactor that code (and test it) to use modern window functions.

Setup Code

Here’s the initial setup code:

CREATE TABLE Accounts
(
ID int IDENTITY(1,1),
TransactionDate datetime,
Balance float
)
go
insert into Accounts(TransactionDate,Balance) values ('1/1/2000',100)
insert into Accounts(TransactionDate,Balance) values ('1/2/2000',101)
insert into Accounts(TransactionDate,Balance) values ('1/3/2000',102)
insert into Accounts(TransactionDate,Balance) values ('1/4/2000',103)
insert into Accounts(TransactionDate,Balance) values ('1/5/2000',104)
insert into Accounts(TransactionDate,Balance) values ('1/6/2000',105)
insert into Accounts(TransactionDate,Balance) values ('1/7/2000',106)
insert into Accounts(TransactionDate,Balance) values ('1/8/2000',107)
insert into Accounts(TransactionDate,Balance) values ('1/9/2000',108)
insert into Accounts(TransactionDate,Balance) values ('1/10/2000',109)
insert into Accounts(TransactionDate,Balance) values ('1/11/2000',200)
insert into Accounts(TransactionDate,Balance) values ('1/12/2000',201)
insert into Accounts(TransactionDate,Balance) values ('1/13/2000',202)
insert into Accounts(TransactionDate,Balance) values ('1/14/2000',203)
insert into Accounts(TransactionDate,Balance) values ('1/15/2000',204)
insert into Accounts(TransactionDate,Balance) values ('1/16/2000',205)
insert into Accounts(TransactionDate,Balance) values ('1/17/2000',206)
insert into Accounts(TransactionDate,Balance) values ('1/18/2000',207)
insert into Accounts(TransactionDate,Balance) values ('1/19/2000',208)
insert into Accounts(TransactionDate,Balance) values ('1/20/2000',209)
go
Posted in Blog | Tagged , , | 2 Comments

Simple Talks Episode 7

The season 1, seventh episode of Simple Talks is out. Check it out, with Ryan as the main host.

Simple Talks is the Redgate podcast from myself, Grant, Ryan, and Louis. The main page is here, and it has links to the audio versions as well as a the video one.

Another one recorded in Austin, during the Redgate 25th Birthday week. We tried doing one on the roof, but it was too windy.

20240718_104551

We ended up in the conference room.

20240717_162745

A bit has changed in my AI view since then, with a second trip to San Francisco and people talking AI more and more. I’ll have a few thoughts later in another post on the Small Data SF conference.

Posted in Blog | Tagged , , | Comments Off on Simple Talks Episode 7

Dead to Me

I saw a note from someone recently that reminded me of Policy-Based Management. This was (is?) a technology in SQL Server that I thought might have great potential. I even had a few presentations on the subject, but sadly I’ve rarely seen anyone implement it. I’m sure some do, but I think for me, this is dead technology.

There have been other tech items from which I’ve been turned off or abandoned over the years. It seems in SQL Server, we have some tech that even Microsoft has abandoned and doesn’t put any development resources into improving.

This week, I’m asking if you have technology that’s dead to you. It could be in the SQL Server or Microsoft Data Platform, or it could be elsewhere. Let us know in the discussion below.

I have often moved on from some technology to something newer or better, but that’s not because the old software was dead to me, but rather because I found something better.

However, I have abandoned some things. Database Mirroring is something I see as dead, as does Microsoft. I think for me, any source control system other than Git is dead to me, though maybe that’s more because Git has proven to be better (and ubiquitous). I’d say that I find most multi-platform messaging clients dead to me, mostly because they can’t keep up or implement good interfaces that work across many places. At least not in a way that makes sense for me. I’d say these days, digital cameras (and video cameras) are dead to me as a casual snapper who can live with a phone.

I’m sure there are other dead technologies that you’re just annoyed with or think don’t (or can’t) meet your needs. There are likely many more which didn’t keep up with you over time, which you wouldn’t have abandoned if they had just improved a bit.

Let us know today what you think.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | 2 Comments