How Do You Decide to Rollback?

One of the constant questions I get from Redgate customers is about rolling back database changes. We build software to help you deploy changes, but what about rollbacks? I think I’ve heard this in almost every conversation I’ve had on this topic in the last 5 years. It’s one reason that Flyway has undo, though with some caveats. Please don’t expect this works magically like a restore operation.

These is a complex topic, and one that can be way harder than a rollback for application changes. In fact, everyone wants it to be simple, but I don’t think it ever will be. There is a big reason for that: data. Undoing data changes can be impractical, especially in if there are lots of changes.

The question I’d like you to ask yourself, and give me thoughts about, is when do you decide to rollback. How can you make that decision in a production system? Are there criteria or guidelines you use? Do you ask someone else? Maybe another aspect of this situation is how you decide to roll forward instead of rolling back.

I was listening to some MVPs discuss this awhile back and one of the main criteria that one person brought up was data changes. This individual said if no data had changed, they just undid everything. However, once data changed, they were likely in a roll forward scenario where they needed to fix code quickly.

That’s often the big factor for me. Once data is changed (or added), then we often can struggle to rollback. If we have an application that needs a new column, and we add it, if there software has a problem, we may not be able to roll back because the old software doesn’t work with the new column. We don’t want to delete the column, at least not until we save the data. These are the problems.

The easy rollbacks are when the deployment fails and transactions undo the work, or we just quickly undo everything we did. That’s an easy decision, but I rarely find this sort of issue. Instead, usually we find certain data breaks our application or the logic is improperly implemented.

I look forward to your ideas and thoughts here, but I do have some advice. One thing I always tell customers is that you want to deploy often, so that you are ready to fix something you broke. That’s important. The other thing that simplifies life is to never add and delete objects in the same deployment. This goes for everything. If I add new first and last name columns, but keep the old fullname column, if I’ve broken something, I still have the old columns and data. I can rollback easier. If everything works, I can always delete the fullname column later. Following these concepts has made my life much easier when I made database changes.

Steve Jones

Posted in Editorial | Tagged , | Comments Off on How Do You Decide to Rollback?

Basic Window Functions–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

Window functions are a class of functions that dramatically improve the performance of certain types of queries. We often think of these as aggregates, but there are ranking and analytic functions that can be used as well.

This is a basic post looking at the outline of these functions and their structure. I’ll look at a few more details in future posts.

The OVER() Clause

The idea in many of these functions is to create a window on which a function can operate and perform some computation over a row in the window. When I first heard of this, I wasn’t sure this was that powerful, but now I find these functions to be incredibly useful for many aggregates, especially because I can get things like a SUM() without requiring a GROUP BY.

The main way we define the window is with the OVER() clause. This comes after the function and defines a partition and ordering for the rowset, one which the window is then applied. That sounds more complex than it really is, so let’s look at a simple example.

I have a number of baseball statistics in a table. I’ll look at one player, Barry Bonds, and his career across a couple teams. If I look at the data, I see:

2021-07-06 12_58_18-SQLQuery1.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (51))_ - Microsoft SQL S

If I want to sum something like home runs, I can do that with the team easily like this:

2021-07-06 13_00_35-SQLQuery1.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (51))_ - Microsoft SQL S

I have a sum and a team, and a GROUP BY. What if I wanted to add something in there, like the at bats by year. Maybe I want to add some other text fields, which is easy to do, but every time I add a non-aggregate to the column list, I need to also add it to the GROUP BY. That’s a pain, and it’s cumbersome. Not only that, I’m limited to looking at the data in the same way. I might want an overall average number of at-bats, or maybe an average by team.

With the OVER clause, I can have different views of the rowset as applied to the function. In this query below, I order one rowset, but not the other. I could have partitioned by different values if I wanted, but that doesn’t help here.

2021-07-06 13_07_16-SQLQuery1.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (51))_ - Microsoft SQL S

Here the average is calculated on the partition, not on the entire group, which is what I’d have before the OVER() clause. I could even add other detail data, like the league, and I wouldn’t have to alter multiple clauses.

There are two key concepts here for the basic OVER clause that I want to discuss, and more details will come in other posts.

Partition BY

The partition by separates the rowset into groups, much like the GROUP BY does. In this case, we note that we want to break our data into groups based on the value of a column (or columns).

This can be different for each OVER() clause, as you can see below. Now the average is teh same for all rows, as Mr. Bonds only played in one league.

2021-07-06 13_12_26-SQLQuery1.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (51))_ - Microsoft SQL S

More details in a future post.

Order By (Inside OVER)

The ORDER BY is the ordering of the rows inside of the partition. In this case, the order doesn’t matter, but in some cases it might. With some functions it matters, so this can change the way your query works.

This works just like the ORDER BY clause at the end of a SELECT query, but when inside the parenthesis, this applies only to the partitions.

Summary

This is a very basic look at Window functions. This is the basic view of what these are, and how they might be used in queries. There are more options, and more to know, but this is a basic look at how you might think about building aggregate queries without a GROUP BY, better performance, and in my mind, easier to read.

SQLNewBlogger

I had to write a query recently and chose an window function because it simplified the code. It also performed very well. This is a start of a few posts based on that work, showing that I have some knowledge on how to use these features in SQL.

This post took about 10 minutes to write, mostly me structuring the example from a dataset I have, and then about 5 minutes to pull some things out of this post to focus on the basic part of building a query. I can expand on this in other posts.

This is a pattern you can easily follow in your own blog to show that you understand some concept in T-SQL, in Azure, or really any topic. Enough of these and they’ll help drive your interview to subjects you know something about.

Posted in Blog | Tagged , , | 2 Comments

Daily Coping 7 Jul 2021

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag. 

Today’s tip is to pause, breathe, and feel your feet firmly on the ground.

I often do this in yoga. It’s one reason I like classes. The instructor slows me down, has me sit and breathe, meditative-like, at times when I might not do that at home.

Today, I took a few minutes to stand outside the house, close my eyes, breathe, and enjoy the outdoor summer air.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 7 Jul 2021

The Second Half of 2021

July is starting, and with it we move to the second half of 2021, or H2 2021 for those of you that like the planning shorthand on roadmaps and project manager boards.

The first half of 2021 has felt like a big of progress in the world, as we try to recover from the pandemic that froze time for most of 2020. I felt like most of last year was one grinding day after another, with little change, lots of limitations, and a constant “sameness” in my life from day to day. While I like routine, I also like some changes and little alterations that keep life interesting.

I know the first quarter of this year felt like that as well, but slowly in the US things started to change. I got my vaccine does, something I didn’t expect until this month or later. The US and a few other countries have deployed so many vaccine doses that life has started to change and open a bit. I know many other countries aren’t as lucky, and I do hope that more assistance and sharing comes from the US and other privileged countries to ensure that we can end this pandemic.

As things change, I’m more hopeful than I was six months ago that we will move towards a more normal way of life, albeit likely will still some changes. I know I see less masks in the US, but not zero. We still have precautions in many places, and most people seem willing to follow whatever rules a business wants. More offices are opening up, although partial remote work seems to be the accepted practice. I’ve seen a few music and other entertainment events start to take place, so maybe we’ll see more technical events. There were good decent number of people at DevConnections in Florida, so that was a hopeful sign as well.

We are all emerging from the pandemic at different rates, with vastly different risk tolerances. Not only are countries taking different approaches, but even within a country like the US, the experience can vary. On a few recent trips, it seemed to me that we all need to be flexibly and respectful of others, willing to adapt to the situations as they occur.

The second half of 2021 will be interesting, but likely very different from the last year. No matter where you are, and how you feel, I do hope that life improves and you can enjoy yourself, both at work and away from work.

Steve Jones

Posted in Editorial | Tagged | Comments Off on The Second Half of 2021