Focus and embrace new habits

About a while ago, I wrote a note about slowing down, taking the time to enjoy what we are doing, and focusing on one thing at a time.

I am running an experiment in my own life. I’m trying to slow down and focus intimately on the task at hand, which is not easy. Distractions are everywhere, and technologies such as our smartphones and social media make it nearly impossible to escape their digital crack. I want to allow myself to enjoy my surroundings and enjoy the present instead of focusing on just what’s ahead, or worst, what’s in the past.

In the book The Power of Habit, Charles Duhigg writes about ways of creating new habits. He describes the habit loop as a practical way to create long-lasting habits. The habit loop consists of a cue or trigger, a routine, and finally, a reward.

The habit loop is a widely used method by many industries, including the game and social media organizations, to get us addicted to their products. A good example of this is your smartphone notifications; that’s what triggers your craving brain, it makes you look at you social media app or email without thinking about it, and finally, it gives you a reward in the form of a new reply, a new like, or a new follower.

This habit loop is something we can use to our benefit, we can replace or use an existing trigger and follow a new routine and a new reward to accomplish something we want, instead of something someone else wants us to do or see.

So the habit I am trying to replace is the habit of reaching out to my phone or computer as often as I do today. And to do this, I am experimenting with using existing cues that make me look at my social media, email, etc., and instead use those same cues and triggers to do something else. I really want to do something that will keep me focused on my goals and the tasks I assign to myself.

For example, every morning, when I wake up, I see my phone, which immediately triggers my brain into picking it up and going through emails, scrolling through my social media apps, etc. Lately, I have been placing my kindle or a book in place of my phone, and every morning when I wake up, I don’t even think about it; I pick up the kindle or book and read for at least 20-30 minutes. Reading a book is more beneficial and relaxing than wasting my morning time with the digital crack, making me feel anxious and stressed.

If you see, the cue is the same, the routine is the same as I’m still picking an object and looking at it, but the reward is even greater now; at least for me, it is. I replaced a bad habit with one that fulfills my goal of reading more books, and at the same time, I feel focused and accomplished doing this.

Changing habits is what I am focused on right now; in the past, I focused on creating new habits without replacing bad ones, and that was a mistake. The best way to focus, to slow down, and to be more present is to change your habits, but doing it without adding complexity or having to remember to do new things. Make it as simple as possible, use the same triggers that make you waste time looking down at your phone, and pick better routines.

You owe it to yourself and to the people around you.

How to (efficiently) update millions of records in a SQL table

You need to update one or more columns in a table with millions of records with data from another table, what’s a good way of going this with low risk and in an optimized way? One of my favorite ways of dealing with millions of records in a table is processing inserts, deletes, or updates in batches.

Updating data in batches of 10,000 records at a time and using a transaction is a simple and efficient way of performing updates on millions of records. The key is to batch out your data to allow you to process a smaller number of records at a time.

Example:

use DatabaseName
go

DECLARE @batchId INT
DECLARE @batchSize INT
DECLARE @results INT

SET @results = 1
SET @batchSize = 10000
SET @batchId = 0

-- when 0 rows returned, exit the loop
WHILE (@results > 0)
	BEGIN
		BEGIN TRAN;

		UPDATE Table1 SET columnA = Table2.columnA
		FROM Table2
		INNER JOIN Table1 ON Table2.Id = Table1.Id
		WHERE Table1.columnA is null
		AND (Table2.Id > @batchId
		AND Table2.Id <= @batchId + @batchSize)

		SET @results = @@ROWCOUNT
	
		-- next batch
		SET @batchId = @batchId + @batchSize

		COMMIT TRAN;
	END

-- the sql below is just to measure the performance of this update, it is not needed to update your data.

DECLARE @startTime DATETIME

SELECT  @startTime = GETDATE()
SET STATISTICS PROFILE ON
SELECT  COUNT(*)
FROM Table1 first OPTION (RECOMPILE)
SET STATISTICS PROFILE OFF
SELECT  'It took ' + CONVERT(VARCHAR(8), DATEDIFF(ms, @startTime, GETDATE())) + ' ms'
GO

In the query above we declare the following variables to do the batching:

  • @results – this variable will hold the number of records updated; when zero, the query will stop. This is to avoid an infinite loop.
  • @batchId – this is set to zero initially, and it is used to compare the table id against it, and after each update, it is set to the id plus the batch size. This allows splitting the millions of records into batches.
  • @batchSize – the number of records to update at a time.

It is important to know that the above algorithm will only work when your table’s primary keys are of type int. If your table’s primary keys are guids, the above will not work.

The section at the end of the query is only used to review the performance of your updates, it is not necessary to update the data and it is a nice way to see how the query performs overall.

I hope the information in this post is helpful, and it helps you learn a simple way to update millions of records in batches, making it easier and lower risk than attempting to do it all at once.

Slow down

You ever feel like time is not enough, or that you aren’t doing enough? Guess what? You are, slow down.

We work too much and too hard. We need to slow down to allow ourselves to do our best work.

We also eat too fast. Slow down and enjoy the food and the people around you.

Even when we are entertaining ourselves, we do it too fast or try to do many things. Please slow down, enjoy the moment, the movie, the ride, the music, that book, slow down, and enjoy it.

We are not robots; we do our best when we take the time to do things right.

Slow down.

Enabling software engineering teams for success.

Software development is hard, and it isn’t always the programming language or the framework you use, it’s the people who work on it.

People are an essential part of a team; everything can be easily changed and fixed, but to achieve effective communication and a great culture, you need to make sure people work well together. In my software development career of over 20 years, the critical difference between successful software projects and failed ones has been the way engineering teams are created and how they are allowed to function overtime.

Continue reading “Enabling software engineering teams for success.”

Maintain your mental health during the pandemic, go out for walks.

As we continue to be in quarantine, I’ve been walking more than usual, trying to fight boredom and insanity. Walking has always been one of my favorite activities, especially when it is paired with a good audiobook or podcast.

Staying home is the best we can do to help avoid spreading this virus, or to avoid getting sick and then requiring the help of hospitals and medical people who are already over capacity and overworked.

Continue reading “Maintain your mental health during the pandemic, go out for walks.”