Category: Blog

  • Trying GO with SQL Server

    Not the batch separator, but the GO language.

    I’m always interested in learning new things, and GO has been one of those items that I’ve wanted to look at and see how it works. This is a big language at Google and

    I saw a new blog from Mat Hayward-Hill on Golang (Go) and decided to make a quick test. After all, he had the code. I first installed the Go language, which was simple. I then added a variable according to the blog and the extension to VSCode.

    From there, I copied his code and tried it. Hello World worked fine, but I had a couple issues with the SQL connection.

    First, I don’t have default instances, just named ones. The connection string was:

    condb, errdb := sql.Open("mssql", "server=localhost\SQL2016;user id=sa;password=SA_PASSWORD=yourStrong(!)Password;")

    However, I had to escape the backslash. VSCode showed this to be an issue, so I tried a second one to clean things up. This worked fine.

    condb, errdb := sql.Open("mssql", "server=localhost\\SQL2016;user id=sa;password=SA_PASSWORD=yourStrong(!)Password;")

    The next thing was the entire password string, including the second equals sign, needs to be your pwd. I connected to SQL 2016 on Win, and once I removed the stuff after the second =, I connected and got the version back.

    The Go Language

    Go was created by Google and is something of an evolution of C, but with some of the ease of Python. It’s apparently good for things like network and web servers where concurrency matters. More of a systems programming language, not one for GUI or desktop apps.

    Who uses it? Quite a few well known sites. Google does, Kubernetes (the container company), YouTube (technically Google as well), 99Designs, Adobe, Bitbucket, Pinterest, and more. It seems to have more popularity than I’d expect, but I don’t examine the language breakdowns often and I’m sure it’s just one of many languages used at these companies.

    Still, it was neat to try a new language. Now to try a few tutorials over at golang.org.

  • Quick Graph Database

    There’s a sample to work through here: https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-sample

    I decided to try this in CTP2 and just see how it works. I didn’t do much, but I added a node and an edge with this code:

    CREATE TABLE Person (ID INTEGER PRIMARY KEY, name VARCHAR(100)) AS NODE;
    CREATE TABLE friends (StartDate date) AS EDGE;

    Next I added a few values, based on the samples.

    INSERT Person
     VALUES (1, 'Steve')
          , (2, 'Andy')
          , (3, 'Brian')
          , (4, 'Leon')
          , (5, 'Jon')
    GO
    INSERT Friends VALUES ((SELECT $node_id FROM Person WHERE id = 1), (SELECT $node_id FROM Person WHERE id = 2),'3/10/2001')
    INSERT Friends VALUES ((SELECT $node_id FROM Person WHERE id = 3), (SELECT $node_id FROM Person WHERE id = 4),'5/1/2000')
    INSERT Friends VALUES ((SELECT $node_id FROM Person WHERE id = 1), (SELECT $node_id FROM Person WHERE id = 3), '3/1/2001')

    Then I ran query.

    SELECT *
    FROM Person p1, Friends, Person p2
    WHERE MATCH (p1-(friends)->p2)
    AND p1.name = 'Brian';

    What does this give me? First, these columns with this data. It’s a wide result set, so I have the column and data listed after it, even though this is really a 1 row table.

    $edge_id_5F276FF32E2B492A96858AC68B530F09                                               
    
    {"type":"edge","schema":"dbo","table":"friends","id":1}
    
    $from_id_DE63E53A3F4749C2980FC989BC2E5405                                            
    
    {"type":"node","schema":"dbo","table":"Person","id":2}                                              
    
    $to_id_19F4532DDEC74B22876DCCFBB24797BE                                                 
    
    {"type":"node","schema":"dbo","table":"Person","id":3}                                              
    
    StartDate  
    
    2000-05-01
    
    $node_id_D004B78ADB644588BE4B9E337823356A                                            
    
    {"type":"node","schema":"dbo","table":"Person","id":2}
    
    ID
    
    3
    
    name                                                                                                 
    
    Brian
    
    $node_id_D004B78ADB644588BE4B9E337823356A                                        
    
    {"type":"node","schema":"dbo","table":"Person","id":3}   
    
    ID
    
    4
    
    name
    Leon

    What does all that mean? No idea. Clearly there is JSON that’s returned here and can be deserialized to gather meanings. Is this useful? I think graphs solve a certain set of problems very well, and more efficiently than relational systems. Certainly I could implement a graph structure relationally, but at scale I’m not sure the queries would be as easy to write or run as quickly.

    I don’t know if I’d use a graph structure in any of the problems we try to solve in the SQLServerCentral app, but who knows. Maybe we would if we could.

    This is just another option for SQL Server, another tool in your toolbelt. Should you use it? I don’t know, but I’d recommend that if you think you have a complex relationship structure, maybe lots of FKs internal to a table or you are modeling relationships, learn more about GraphSQL and how graph databases work and build a POC. I’m not sure when the SQL Server implementation will be production ready, but it doesn’t hurt to test and learn a bit if you have the chance.

  • DevOps Basics–Creating a local repo and committing files

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. This is also a part of a basic series on git and how to use it.

    A local repo is a repository, and is the version control system you will use locally. In a previous post I looked at cloning a repo. That’s a way to get code from others, but what if I want to start a new project?

    That’s easy. This post will start a new project, save a few files, and show how to commit these to my git VCS.

    Create a Repo

    If you use tooling, there is usually a CREATE function somewhere, but at the command line, you can just do this:

    git init

    Assuming you’ve installed git, this will create a repo in your folder, and let you know it exists.

    2017-04-26 14_16_13-cmd

    At this point I have an empty repo, and if I look in my folder, there’s a .git folder.

    2017-04-26 14_16_20-GitTests

    This folder will essentially control how this repo works on my system. Let me start by adding a couple text files. I’ll use a markdown file as a Readme, since I’ll eventually push this to Github and I like to have something there that makes sense. I’ve also got the contents of the text file here, which makes it easy to track what changes are being made and versioned.

    2017-04-26 14_17_30-SomeTestFile.txt - Notepad

    Let’s now check my status:

    2017-04-26 14_18_01-cmd

    I’ve saved files here, but they aren’t being versioned. There’s not automatic tracking here just because I’ve saved files. This is something I need to do. Some tooling will do this for you, but it’s good to understand how this actually works. I need to tell git to track these files, so let’s do that.

    First, I’ll add the files. I could specify specific files, but for now, I’m adding them all (both of them). Then I’ll check my status.

    2017-04-26 14_19_14-cmd

    Notice the files are in green now. These are being tracked, and they’re “staged” for commit, but they’re not committed. Git sees these are new files, but the changes haven’t been saved.

    I’ll now save the files with a git commit. I use the –m option to specify a comment on the command line. In another post I’ll show you what happens when you don’t do this.

    2017-04-26 14_20_41-cmd

    If I now look at status, I see nothing.

    2017-04-26 14_21_32-cmd

    Why?

    Git is concerned with changes and versioning. If everything is tracked, then git sees a clean directory and no files to commit. The files exist, but the version is not tracked in git.

    Changes

    I’ll make a change to a file and then we can see the effect. Here I’ll add text and save the file.

    2017-04-26 14_23_39-GitTests

    Now let’s check status. Below you’ll see I have a “modified” file, which I’ll then “stage” and add as something I want to commit.

    2017-04-26 14_24_05-cmd

    Let’s now commit this.

    2017-04-26 14_25_21-cmd

    I can see that things are clean again, and my folder looks like I’d expect. The two files, one of which has two lines in it.

    That’s really it for now. If you want to play along, download git, create a repo, and make some changes and commits. In another post, I’ll look at how I see the changes and get back to a previous version.

    SQLNewBlogger

    This was a quick post, about 10 minutes, as I practiced and experimented with things I know about git, trying to ensure I get them straight in my mind. That’s a good way to learn or improve skills in an area.

    The hardest part in this post is trying to focus and stop writing.

  • Using DBCC Clonedatabase

    I haven’t messed with the new DBCC CloneDatabase option in SQL Server 2014/12016 (depending on patches), but recently I saw this:

    2017-04-28 17_39_32-Argenis Fernandez on Twitter_ _Anyone out there using TFS that can send me a bac

    On a quiet Friday afternoon, this seemed like a good time to experiment. We (Redgate Software) have a salesdemo VM that we use to show various Redgate products to customers and clients. On the VM, we have a TFS install that shows how we plug into that platform.

    We have a slick demo system on EC2 where I can fire up a self-service VM for use anytime. Since DBCC CloneDatabase needs recent SQL Server versions, I had to use that. My export of the demo VM from late last year is SQL 2012 Sad smile.

    Once the system was up, I found the SQL instance that hosted the TFS databases and connected with SSMS. Then what?

    I ran a quick search and found the MS support article: How to use DBCC CLONEDATABASE to generate a schema and statistics only copy of a user database in SQL Server 2014 SP2 and SQL Server 2016 SP1

    This explains that the basic syntax is

    DBCC CLONEDATABASE (source, target)

    The source is the database to clone, and the target is the new database you want created. Using that, I quickly ran this twice, once for the tfs_config database and once for the tfs_defaultcollection db. I used create names for the targets.

    DBCC CLONEDATABASE (tfs_config, argenistfs_config)
    
    DBCC CLONEDATABASE (tfs_defaultcollection, argenistfs_defaultconfig)

    With these database, I ran backups and uploaded these to a share for Argenis to use. The clones contain schema and stats, but no data, so they’re small. The live databases are a few GB, but the clones are small.

    2017-04-28 17_49_58-Public

    I haven’t used the cloned databases for anything, but there are articles out there that will help you to use this to work on issues in your main system. SQL Performance has one from Erin Stellato and so does Brent Ozar.

    This is going to be one of those tools that will be helpful for DBAs in the future, so play around with it.