Author: way0utwest

  • IoT Increases Efficiency

    There is no shortage of articles about problems with IoT, especially with consumer based IoT products. In fact, many of the ideas and sales are gimmicky with dubious value for me, but I know what some people get excited that they can change their home thermostat from the car or have the fridge send them an inventory, or even have a tracker on their keys.

    There certainly are some great implementation, especially in healthcare, but I think there is a lot of IoT that can really help in business, some of which will create new opportunity for us as data professionals. As the costs for sensors come down and their capabilities rise, I’m sure more people will come up with interesting ways that they can be used.

    I spent quite a few years earlier in life working in the restaurant industry, and I’ve spent my whole life shopping in grocery stores for ingredients to cook with. I know margins are low in these businesses, and there is a lot of waste. When I ran across this article on using IoT in the food industry, I was skeptical that it would become commonplace, but I think I might be wrong. Keeping food fresh has a noticeable impact on profits and sales, and cheap sensors that can help measure data is important in all stages. From the supply chain to your fridge, if data can inform you of those foods that are nearing the end of their lifetime, perhaps you’d make different decisions in shipping, in promotions in restaurants, or even in the recipes you pick for tonight.

    And I’m sure there are some of you that would love to chat about the source and age of the lettuce in your salad at a restaurant. That’s not for me, but IoT is going to change our lives in many ways and give us lots of data with which to work.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 3.0MB) podcast or subscribe to the feed at iTunes and Libsyn.

     

  • Better Security with dbatools

    I really like the dbatools project. This is a series of PowerShell cmdlets that are built by the community and incredibly useful for migrations between SQL Servers, but also for various administrative actions. I have a short series on these items.

    One of the things that I see many junior SQL Server people struggle with (or forget) is the need to get logins onto multiple servers. This is often in situations where some sort of DR or HA technology is being used between instances. An admin will set up a second server, add logins, and forget about the entire system.

    Until something fails. Then their failover works perfectly, or mostly, or good enough, and they move on with their day. A few days later

    The cmdlet, Sync-DbaSqlLoginPermissions, is designed to help solve this issue. I certainly could use this to quickly sync logins (SQL or Windows) between two instances, but it’s great for an automated, regular sync between two HA or DR instances. If you set this up as a job, it won’t necessarily catch every login if servers fail between the syncs and a login is added, but this will give you some added protection over a long term as you make changes on one system.

    This is a way to solve some cross instance issues that are hard in T-SQL, but simple in PoSh. Kudos to the dbatools team for this cmdlet.

  • Are You Always Up?

    I’ve had the opportunity to work for and with quite a few companies in my career. In some sense working as a database developer or database administratrator is very similar at many places. The “job’s the job,” as I’ve said in more than a few interviews, as I often ask to meet with others and better understand the environment. I’m rarely sold on the technology in use, usually more concerned with management and co-workers, though you might view employment differently.

    At the same time, it can be amazing how different the environment may be and how varied the requirements are for my application and systems. One employer was a small, but busy and growing import/export company. We stretched the limits of technology at the time, and it was a challenge to meet the desires of management. At the same time, we were primarily a weekday, roughly 9-5 shop and I had the opportunity to bring down systems at night or on weekends for maintenance.

    This was in contrast to my prior position at a nuclear power plant. There systems were expected to be up 24×7, with (grudgingly) scheduled maintenance once a quarter. That was the environment with the most stringent requirements, though I also learned over time that I preferred to have a bit more latitude in how I could architect systems or manage databases. Though that was when I had children, and I might be willing to work in a similar environment now.

    Many of you probably have varying requirements by application, but I suspect more of you are expected to keep systems up more often than in the past. It seems the world has moved to a greater dependency on database platforms, with the expectation that the data is always available.

    When you plan upgrades or maintenance, can you work with a flexible schedule? Or are changes always carefully managed? I’m sure some of you prefer one type of environment over the other, so chime in today with your preferences and restrictions. I’m sure some of you work in less busy organizations, and I’m curious if that’s the case.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 3.7MB) podcast or subscribe to the feed at iTunes and Libsyn.

  • Advent of Code 2017 Day 5–#SQLNewBlogger

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

    This is day 5 of the Advent of Code 2017. If you want to read about the puzzles, start with Day 1. This is going to be a crazy looping item, since it will move through the list, relative to the current spot, and incrementing items, I know this won’t be good in SQL.

    Still. Worth solving.

    Let’s load the data. I’ll use a table, but first, I’ll also add an identity. This will help me number instructions and figure out what the next one is.

    CREATE TABLE Day5
    ( InstructionKey INT IDENTITY(1,1)
    , Instruction INT)
    GO

    There are issues with identities, but this is a great trick:

    CREATE VIEW Day5V
    AS
    SELECT d.Instruction FROM dbo.Day5 AS d
    GO
    -- reusable code
    BULK INSERT Day5V FROM 'e:\Documents\GitHub\AdventofCode\2017\Day5\Input.txt' WITH (ROWTERMINATOR='\n')
    GO

    Now I can get to work. Here’s the logic I used.

    I wanted to first set some starting points. I have a counter (0 based, increment first). This determines how many times I jump around. I also need to track the current instruction key and the next key. And, of course, I need the instruction value.

    The identity is the array index, or the instruction key (which place am I in). In this case, I’ll try to follow this logic.

    Get the end (out of bounds, which is the max + 1). I loop until I get an jump outside of the end range. The loop does these items:

    • Get the current instruction jump
    • Set the next location to be the current key + the current jump
    • Update the current jump to increment by 1
    • Set the current instruction key to the next key
    • loop

    This seems to be what I need. On the test set, this worked fine. When I first set this up, I used this code:

    DECLARE @end INT ,
             @CurrentInstructionKey INT = 1 ,
             @Instruction INT ,
             @NextInstructionKey INT ,
             @counter INT = 0;
    SELECT @end = MAX(InstructionKey) + 1
    FROM dbo.Day5 AS d;
    
    -- SELECT [end] = @end;
    
    WHILE @CurrentInstructionKey < @end
    BEGIN
         SET @counter = @counter + 1;
         SELECT @Instruction = Instruction
         FROM Day5
         WHERE InstructionKey = @CurrentInstructionKey;
         SELECT @NextInstructionKey = @CurrentInstructionKey + @Instruction;
         UPDATE dbo.Day5
         SET Instruction = Instruction + 1
         WHERE InstructionKey = @CurrentInstructionKey;
         SET @CurrentInstructionKey = @NextInstructionKey;
    --PRINT @CurrentInstruction
    END;
    
    SELECT Counter = @counter ,
            [current] = @CurrentInstruction;

    When I ran this, it chugged for some time. I bet in Python or C#, which would solve quickly with arrays. With updates, it’s slow. Like minutes slow for 1074 rows.

    However, it worked.

    Part II

    In this part, this instructions are almost the same, but based on the current instruction value, we either increase or decrease the value. Not a big change. Our new update looks like:

    UPDATE dbo.Day5
    SET Instruction = Instruction + CASE
                                         WHEN @Instruction >= 3 THEN
                                             -1
                                         ELSE
                                             1
                                     END
    WHERE InstructionKey = @CurrentInstructionKey;

    This also works, albeit slowly. I left this around 5:30 and went to the gym.

    One of the easier puzzles.