Author: way0utwest

  • Adding Performance Counters back for SQL Server

    I had a strange situation the other day, where a number of things went wrong with my instance. First, I lost permissions to detached databases. The SID was listed in the file permissions, but apparently unlinked to an account.

    Next, I went to add an alert, and I only had the XTP counters.

    2017-06-14 12_16_20-SQLQuery4.sql - (local)_SQL2016.sandbox2 (PLATO_Steve (63))_ - Microsoft SQL Ser

    The counters are also missing in Performance Monitor. What is interesting is that I show the correct SQLAgent counters for each of my three instances.

    2017-06-16 08_54_19-Add Counters

    A quick search found me this blog on MSDN, where it recommends the following:

    unlodctr mssqlserver
    
    lodctr perf-mssqlserversqlctr.ini

    I had a named instance, so for me I entered:

    unlodctr mssql$sql2016
    
    lodctr perfMSSQL$SQL2016sqlctr.ini

    from an elevated command prompt. Running the last command again shows the counters loaded.

    2017-06-16 09_19_26-cmd (Admin)

    I also checked my registry, which appeared to be fine:

    2017-06-16 09_08_38-Adding Performance Counters back for SQL Server - Open Live Writer

    I next found another blog that noted I might need to resynch WMI, so I ran winmgmt, using the PID from Task Explorer (details tab):

    2017-06-16 09_24_32-cmd (Admin)

    I didn’t see counters at first, but I restarted the instance. Once that was done …

    2017-06-16 09_23_29-New Alert

    A nice fix, and one I probably won’t forget after this blog.

  • Improving Replication

    I really like replication as a technology. I think the ability to move data around to other systems, at a gross level, is extremely handy in many systems. While SSIS and other ETL tools are very flexible and powerful, they also require quite a bit of work to maintain. Being able to send a table (or a vertical/horizontal partition) to another system is just valuable.

    This is why I’m constantly disappointed that SQL Server hasn’t really bolstered their replication technologies to make it more robust.  Don’t get me wrong, there have been improvements in various versions, and replication has come a long way since SQL Server 6.5, but as an overall subsystem in SQL Server, it has a long way to go. The tooling needs work, the reliability and robustness needs work. I find replication brittle, as do many others, and when there are code deployments needed, it seems that administrators often just script out the system, tear it down, make changes, and rebuild it.

    That’s not a great plan. In 2017, that shouldn’t be the plan. I don’t get why Microsoft hasn’t made things better, after all, more replication options could mean more SQL Server instances installed to support disparate workloads. However, rather than complain, I’d like to give Microsoft ideas.

    Today I’m curious. What would you do to improve replication? What would you like to see? Perhaps you want better monitoring of the process. Simpler setup, such as that available in Azure. Better bidirectional replication? Maybe an easier way to deploy changes? Let us know your ideas for improving SQL Server replication.

    Steve Jones

    The Voice of the DBA Podcast

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

  • Loading All CSV Files with PowerShell

    I ran across Denis Gobo’s post about working with names from Social Security cards and wanted to play with the dataset. However, rather than use xp_cmdshell, which I might have, I decided to use PowerShell. I need to repeat this with Python, but for now, this worked.

    There is a file you can download that has a lot of .txt files, each one with a CSV. You can read about this in more detail in Denis’ post. Essentially there were three fields, and the year of the file in the name. I downloaded and unzipped the files into a folder.

    Here was my process:

    1. Loop through the files
    2. Extract the year from the file name
    3. bcp in the file into a staging table
    4. call a proc that takes the year as a parameter and moves all data

    With that in mind, here’s how I set things up.

    The SQL Server Side

    I started by creating two tables.

    CREATE TABLE Names
    (   FirstName  VARCHAR(500),
         Gender     CHAR(1),
         NameCount  INT,
         YearInFile INT
    );
    CREATE TABLE Names2
    (   FirstName VARCHAR(500),
         Gender    CHAR(1),
         NameCount INT
    );

    With these in place, I created a procedure:

    CREATE PROCEDURE MoveNames @year INT
    AS
    INSERT Names SELECT FirstName, Gender, NameCount, @year FROM Names2;
    TRUNCATE TABLE Names2;
    GO

    I tested these and they worked fine for my process. I want to load into Names2, then to get the year, I move the data to the other table, adding the year. There are probably better ways, but this worked fine for a relatively small load (few million rows).

    PowerShell

    With PoSh, I started with a simple loop. I tend to set variables first since this makes this easier to turn into a function.

    $sourcefolder = “E:\Downloads\names”

    $sourcefiles = Get-ChildItem $sourcefolder -Filter “*.txt”

    foreach($file in $sourcefiles ){
       $yearinfile = $file.Name.Substring(3, 4)
       write-host “Loading File:” $file.Name ” year: ” $yearinfile

    }

    Running this gets me a list of all files along with the  years. That’s what I want, since I’m going to process each file and load it in with bcp. Thanks to Mike Fal for the outline of how I’ll do this.

    Next, I set more variables at the top:

    $InstanceName = “.\SQL2016”
    $DatabaseName = “Sandbox”
    $StagingTableName = “Names2”
    $StagingProc = “MoveNames”

    I’ll use these in this code. I call Invoke-Expression to run bcp and then Invoke-Sqlcmd to run my proc.

    $cmd = “bcp ‘$DatabaseName.dbo.[$StagingTableName]’ in ‘$file’ -S’$InstanceName’ -T -c -t’,’”

    Invoke-Expression $cmd
    Invoke-Sqlcmd -ServerInstance $InstanceName -Database $DatabaseName -Query “$StagingProc $yearinfile”

    From here, I put this in a file and ran it from my download location. The result:

    2017-06-16 12_41_08-Windows PowerShell ISE

    and from SQL Server:

    2017-06-16 12_41_50-SQLQuery7.sql - (local)_SQL2016.sandbox (PLATO_Steve (56))_ - Microsoft SQL Serv

    Now I can run some queries, and find out where I stand. #26, it turns out.

    2017-06-16 12_43_35-SQLQuery7.sql - (local)_SQL2016.sandbox (PLATO_Steve (56))_ - Microsoft SQL Serv

  • The Traveling Data Professional

    It’s summertime in the Northern hemispheres, and a time when much of the world takes their holiday time away from work. Most kids are out of school for at least part of the June/July/August time period. With many of us workers trying to spend time with our families, this is the part of the year that we schedule trips and events.

    Taking time away to be with family is important, but just getting away from work is healthy for all of us. Whether it’s a planned trip to some new location, visiting relatives in another city, or even just taking time to enjoy a leisure activity, we all need a break. Some of us will try to do too much during our break, arriving back at work physically worn out. Even in these situations, it’s still nice to get your mind away from the environment you face most days. I think any break is good, and especially in the era of smartphones and nearly ubiquitous connectivity, time away is good for your health.

    This week, I’m wondering if any of you have plans for an adventure that you’re excited about. Are you going somewhere new or engaging in some activity you look forward to? Perhaps you don’t have big plans this year, but something you’re working towards in the future? Make us jealous and let us know what holiday break might be coming your way.

    I’ve been more of a winter person for most of my life, looking to take time off from work when there’s snow on the ground, but I’ve had my share of summer trips. Usually my family will camp and take a few trips. With my children getting older, I expect to take even more trips when it’s just my wife and me. Since I can work from anywhere, I can follow along on some of her horse adventures.

    This year we had planned on a long trip to Glacier National Park, but with older kids, scheduling looks to be a bit of an issue, so we’ll likely stick to camping in Colorado. However, I am honored to have been chosen to speak at the Data Platform Summit in Bangalore, India, so we’ll all have a bit of a vacation in August as we travel to a new country.

    Enjoy the weekend and let me know what your plans are this year.

    Steve Jones

    The Voice of the DBA Podcast

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