SQLvariations: SQL Server, a little PowerShell, maybe some Hyper-V Rotating Header Image

TSQL2sDay

T-SQL Tuesday #31 – Logging

I’m excited to be hosting this month’s T-SQL Tuesday.  This month we’re talking about logging.  Logging comes in many form and fashions.  If you think about it, when you go to the grocery store with at big list, Do you put the items in the cart and then check them off the list?  If so, isn’t that a kind of write-ahead log?

I point that out because I don’t want anyone to constrain themselves to talking about logging within just SQL Server.  Please bring your ideas for file transfers, report generating, performance gathering, uptime monitoring and the like.  But don’t stop there!  This is an open invite to anyone that does anything in the SQL Server community.

If Karla Landrum ( blog | twitter ) wants to explain to us how on Earth she keeps track of all these SQL Saturdays around the world, that’s logging!  If Tim Radney ( blog | twitter ) wants to tell us how he makes sure he keeps in touch with all of the chapters he’s responsible for as a PASS Regional Mentor, that’s logging!

How you keep track of blog ideas, white papers you read, or however it is you life-hack *your* SQL world: please share it with us!

Rules
•Your post must be published between 00:00 GMT Tuesday June 12th, 2012, and 00:00 GMT Wednesday June 13th, 2012
•Your post must contain the T-SQL Tuesday logo from above and the image should link back to this blog post.
•Trackbacks should work, but if you don’t see one please link to your post in the comments section below so everyone can see your work

Optional, but highly encouraged…
•Include a reference to T-SQL Tuesday in the title of your post
•Tweet about your post using the hash tag #TSQL2sDay
•Consider hosting T-SQL Tuesday yourself. Adam Machanic keeps the list.

 

P.S.  Since I’m the host, any PowerShell topic on logging is cool too!

PowerShell Interfaces with Other Hammers

This month’s installment of T-SQL Tuesday is hosted by Pat Wright (blog | twitter). Pat says: “So the topic I have chosen for this month is Automation! It can be Automation with T-SQL or with PowerShell or a mix of both. Give us your best tips/tricks and ideas for making our lives easier through Automation.”

I have to tell you, this doesn’t get old:

imageI love that somebody on the other side of the planet is telling someone else that I ‘probably have a PowerShell script to solve their problem’. The funny thing is that a lot of the time I don’t have a script for the problem they’re trying to solve *yet*. I love it when someone gets referred to me and I don’t have a script because it gives me a chance to go explore the language and figure out something I don’t know. PowerShell makes this quest pretty fast.

imageIronically Paul White ( blog | twitter ) didn’t know I had a script for that, he was just making a joke. Well, actually, I didn’t have a script for that. I did however know right where to find the person that did; and that is even better than if I had a script of my own. Why is it better that I had to point someone some where else? Could I not have figured this script out own my own? No that’s not it at all.

imagePowerShell is NOT a hammer it’s an Automation Language. Better yet PowerShell is a Toolset, and it happens to come with some pretty freaking awesome hammers built in. We all understand that SQL Server isn’t just a hammer, that it’s an entire toolset. Inside of that toolset is: T-SQL, SQL Agent, Profiler, SSIS, the SMO, SSRS, SSAS etc… just to name a few.

The cool thing about PowerShell is it’s actually built to work with other hammers. So the reason that you hear me talking about PowerShell all the time is not because I’m replacing my T-SQL Hammer, I’m just automating it! Like this, here’s a quick script to figure out how long your SQL 2008+ instances have been running:

<# Number of Days since the Instance has been restarted #>
$InstanceList = "WIN7NetBook", "WIN7NetBook\R2", "WIN7NetBook\SQLExpress"
foreach($Instance in $InstanceList)
{
Invoke-Sqlcmd -Query "SELECT @@SERVERNAME AS 'ServerName'
, DATEDIFF(D, Sqlserver_start_time, SYSDATETIME()) AS 'NumberOfDays'
  FROM sys.dm_os_sys_info" -ServerInstance $Instance -Database master
}

The automation doesn’t stop with SQL Server. You can find plenty of posts form people in our SQL Community on working with things like Red-Gate’s SQL Compare, Outlook, Active Directory, and Subversion just to name a few. The key here is that all of these people are automating something adjacent to their SQL world and they’re using PowerShell to make it happen.

Heck just last week I wanted to know how many sessions had been submitted to SQL Saturday #67 in Chicago. Do you think I went to the schedule page, copied the list of sessions into Excel and then checked to see what line number the last session was on to get my count?? Heck NO!! Last year I wrote a script for Allen Kinsel ( blog | twitter ) to count how many people at the PASS Summit had registered their twitter handle when the signed up. I took that script, changed the link, changed the term I was looking for and presto! I now know that 105 sessions have been submitted to SQL Saturday #67.

That has nothing to do with SQL Server right?!… Except for that whole bit about it being a SQL Saturday that I was interested in.

<#Start up a web client and download the web page into a text file#>
$url="http://www.sqlsaturday.com/67/schedule.aspx"
$file="c:\temp\DownloadSession$(((Get-Date).ToString("yyyyMMddHHmm"))).txt"
$webclient = New-Object system.net.webclient
$webclient.DownloadFile($url,$file)            

<#Sift through the file for lines that include "viewsession" #>
(Select-String -path $file -Pattern "viewsession").count

OK, so what’s your point?

My point is that I wouldn’t have even had the twitter script that I started with if Allen Kinsel hadn’t taken a chance and sent me an email at noon the Friday before the PASS Summit and asked me if I could use PowerShell to count the number of attendees with twitter handles.

Again, what’s your point?

My point is Use PowerShell to make one tool talk to another.

And…

And tell people faced with an annoying problem that you think I’ve got a script for that because whose knows, maybe I do!

Picture the Index: T-SQL Tuesday #10 – Indexes

TSQL2sDay150x150I don’t really have the time to write this month, but indexes are such a key feature of databases that I thought I should.

A whole post would take more time than I have. Instead I’m going to mention something that Rob Farley (of LobsterPot Solutions in Australia) said in a SARGability talk for the AppDev Virtual Chapter of PASS a few months back.

Rob talked about the way that we find entries in an index using a Seek operation, and compared to using a phonebook. In particular, he described the bit at the top corner of the phonebook, which we use to find the right page before looking at each record. This is very much how an index works. An index is stored in a b-tree, with the levels at the top being like the corner sections in the phonebook, and the leaf level of the tree being like all the records there. 

A quick bit of searching flickr found an image at http://www.flickr.com/photos/blinky5/376596220/ which describes this perfectly. If you’re looking for Wilma Todd, you can use the top corner to figure out which page she’s on and then find her record on the page very easily.  So what’s my point?: The next time that you’re picking a data type for a column think of this.  If you use a data type that’s twice as large as what you really needed (int vs. smallint, nvarchar vs varchar) and then you realize you need to put an index on it, it would be be like doubling the font in this phone book thereby requiring twice as many pages to print the book.  May not seem like much but when you have to scan for data you’re now going to have to physically touch twice as many pages.  And hey, now the books twice as heavy as it needed to be just like your backups are going to be! 

Phonebook

Anyway, I don’t have any more time to spend on this post, gotta get my code ready in case I need to fill in tomorrow for someone during the 24 Hours of PASS, so I’ll just publish it and wish you all a Happy T-SQL Tuesday.

Controlled Failure is the Key to Learning More

TSQL2sDay150x150Recently Andy Leonard ( blog | twitter ) tweeted: “I was asked recently about secrets to success. My reply? “Never fear failure.” :{>” I really agree with this in a lot of ways. Surprised? Don’t be, there’s a big difference between ‘not fearing failure’ and ‘liking failure’. I don’t get up in the morning because I like to fail, I just know that it’s going to happen. A lot.

I approach a new programming feature by reading through it’s capabilities one command at a time. After I’ve read the definition of a command then I like to play with the examples. After that I’ve done some of the things it says it can do, then I like to try and make it do things that weren’t actually listed. I like to test the boundaries of a feature. This is where some “controlled failure” comes in.

Boundary testing is useful for a number of reasons. A best practice for using the feature might be established, but without testing the boundary you won’t understand why it is the best practice. When I’m done testing boundaries for myself I look at what others have done with the feature. I find out what problems they’ve experienced and what innovations they discovered. Because I’ve tested the boundaries myself I can better understand examples online and discuss these discoveries with other data nerds. By doing this I learn from my failures and other peoples failures too.

This appetite for failure spills over into my work–and I’d bet it spills over into your work too. At the end of the day though no matter how big or awesome a solution we create chances are a ton of failure that went into it. On a good day I probably fail at trying more things by noon than most people do all day, heck maybe even all week. Embrace failure as part of the learning process. It makes you a better employee and even a better mentor at work, because let’s face it, if you can fail and then persistently succeed yourself then you’re better positioned to help a struggling coworker find that persistent success too.