Categories
Geeky/Programming

IT and Development Best Practice: Just Because You Can Doesn't Mean You Should..

One thing I have learned over the years in IT and Development is this: Just because you can do something doesn’t mean you should.

What does this mean? It means that sometimes software and programs and hardware will let you do thing/configure things in such a way that is possible in the software, but that doesn’t mean you should do it.

Some examples:

1) Development – You can add a gazillion button controls to a form. Your development IDE doesn’t complain at all. You run your program and it totally dogs or has weird issues. Why? Because common sense tells you that shouldn’t put many controls on a form, you need to redesign! Some guru’s (Aaron Ballman, Raymond Chen) on the subject have blogged about this and it is talked about all around the web

2) Networking – Windows 98 (and other OS’s – I just know this one is from example) allows you to set two gateways on your adapter. Does this make sense? Two DEFAULT Gateways. Shouldn’t there just be one? I have seen first hand two gateways that don’t talk to each other and then end users sometimes could connect to the Internet, and sometimes they could connect to internal stuff, but not at the same time! Doh!  Chris might be able to add more to this as I am not a networking guru, but I know it just isn’t right.

3) Data Warehousing – Now, The way SQL Server Analysis Services is set up, you have your SSAS server. Then you can make multiple "databases" under that instance. Sort of like regular SQL Server, Instance->Databases->Objects. The thing is, under an SSAS Database, you can create multiple cubes. Now, there might some small instances where you want to do this, but just because the GUI/API lets you create multiple cubes under a SSAS DB, doesn’t mean you should! For one, you can’t share linked objects between SSAS Databases, because both cubes are in the same DB. The other thing is that if both cubes are tightly bound or are mutually exclusive, then you run into MAJOR pains when trying to deploy/process, etc. You risk taking one cube offline because you are having a deployment issue with the other cube. Keep your cubes in separate SSAS databases! 🙂 I ran across this the other day which finally put the nail in the coffin on this issue for me

I am sure there are many more instances where there is the ability to configure or do something but you shouldn’t. It really can lead to major headaches and issues for all involved if common sense isn’t used beforehand. Although sometimes there is an unknown factor and you just have to decide, but then later when you realize it you should go back and fix it (that is probably a good topic for another post in itself!)

Keep geekin!

Technorati Tags: ,,,,,,,,,,,,,
Categories
Business Intelligence Geeky/Programming SQLServerPedia Syndication

SQL Server 2005 Performance Dashboard Reports

Check these out. http://www.microsoft.com/downloads/details.aspx?familyid=1d3a4a0d-7e0c-4730-8204-e419218c1efc&displaylang=en

If you are a DBA, these are pretty much money. I have seen some many custom implementations and DBA’s trying to get reports like this, and if you have SQL Server 2005 SP2, you can download that and run a SQL script and then you have these reports. They give you insight into pretty much everything going on with your server. Why pay $1000’s for a product that will give you the same info when it is built right into SQL Server!!

Technorati Tags: ,,,,
Categories
Geeky/Programming SQLServerPedia Syndication

SQL 2005: Using Data Management Views (DMV's) to View Status of Database and Log Restores

SQL 2005 introduced some cool "data management views" (DMV’s) that let a DBA see what is happening on their database. Previously you had to use undocumented system tables and information schemas to get the info, and a lot of the info wasn’t even available. Well recently I was doing some backup/restore stuff on a database and wanted to know when it would finish. The first question I ask is – why isn’t this built into the SSMS view of a db, when it says "DatabaseName (Restoring)" why couldn’t they just add the % done at the end of something? Anyway’s – if you want to see what is restoring on your server and where it is at, go ahead and use this query:

use master
go

SELECT
    percent_complete AS ‘PctComplete’,
    start_time AS ‘StartTime’,
    command AS ‘Command’,
    b.name AS ‘DatabaseName’,
    DATEADD(ms,estimated_completion_time,GETDATE()) AS ‘EstimatedEndTime’,
    (estimated_completion_time/1000/60) AS ‘EstimatedMinutesToEnd’
    FROM sys.dm_exec_requests a
    INNER JOIN sys.databases b ON a.database_id = b.database_id
    WHERE command like ‘%restore%’
    AND estimated_completion_time > 0

I did notice that it says 100% complete but then the restore still takes about 20-30 seconds to finish, so be warned there. Also, if you are restoring a DB, the database name will be master (from what I have seen, it was a fresh restore). If you are just restoring logs, it will show you the database name of the database where the logs are restoring.

Take this and create a quick SSRS (Reporting Services) report. Or even better schedule this to run every 2 minutes or something and maybe once it sees some records, kick off a report, or make a cool little desktop app that has balloon popups and statuses, so as  DBA you can see what is going on with your restores. Maybe I will create that app one Saturday if am bored or something 🙂

Technorati Tags: ,,,,,,,,
Categories
Business Intelligence Geeky/Programming SQLServerPedia Syndication

SQL 2005 SSAS Deployment Wizard – Where Did My Roles Go?

Ok.. if you are using SSAS 2005 and using the deployment wizard (not using BIDS) – probably because you want to retain your partitions or something. But anyways, if you use it and choose the option to retain your roles, you will deploy, and no one will be able to get to your SSAS DB or Cubes. What gives?

See this link on Microsoft. Doesn’t make sense to me but I guess in Microsoft speak Retain roles and members really equals Screw up roles and members and leave you scratching your head.

Technorati Tags: ,,,,
Categories
Geeky/Programming

HowTo: Get iSight Camera Working in Windows XP through Parallels 3.0 on MacBook

So, I use Pidgin for IM. It is like Trillian, but I like it better. No webcam support. I don’t webcam really, but I wondered today what I could use to webcam if I wanted to. I found a site, tokbox that lets you do just that, through the web – cool!

Anyway’s, It works fine on Vista with my webcam hooked up on my Dell, but I tried it on XP on Parallels on my MacBook and it couldn’t find a cam, WTF? So I did some digging and this is what I found, it will work, you just need to set it up!

In the MacBook, Parallels menu->Devices->USB->Built-In iSight which will enable it for use in XP..but no drivers. Here they are – iSight Drivers For Windows XP.

When going through the driver wizard in XP, point to the extracted drivers from that zip and it should work! Happy web camming! 🙂

BTW – I am on tokbox now (http://tokbox.com/ScaleOvenStove), so if you want to chat, audio/video, give me a "call". I haven’t tried it yet with anyone.

Technorati Tags: ,,,,,,,,,,,
Categories
Geeky/Programming

.NET Programming – Stopwatch class in System.Diagnostics

So in two recent small little projects, I have needed to "time" things in code. Back in the day, it was using timespans, timers in windows forms, ticks, and just two dates and taking datediffs, getting the milliseconds or seconds, coverting to the time element you needed. Well, no longer do you have to hack your way through all that! There is a Stopwatch class in System.Diagnostics!! pseudo C# code to follow:

stopwatch

using System.Diagnostics;

Stopwatch _stopwatch = new Stopwatch();

_stopwatch.Start();

… do you things here…

_stopwatch.Stop();


_stopwatch.Elapsed. <every time element you need here>


_stopwatch.Reset();

This totally rocks and I see myself using it more and more. Pretty sweet! System.Diagnostics has a ton of cool things – go explore it!

Technorati Tags: ,,,,,,,,,,,,
Categories
Geeky/Programming

HowTo: System Admin – Remotely Log Memory of Processes Greater Than X

If you ever wanted to remotely log memory usage (and CPU usage) of processes, you can use "tasklist"

The key is, "tasklist /s"  and then the remote computer name. That along with filters you can query and log process info remotely with no more than a cmd window, or batch file in a scheduled task.

For example, here is how you would log processes with memory over 25000 KB

tasklist /s remotecomputername /fi "memusage gt 25000" >> c:mem.txt

take a look at the tasklist help by typing "tasklist /?" at the cmd prompt. You can see how all the filter work. You can filter by PID, Image Name, etc. Pretty sweet. BTW it does work on your local machine too, just remove the /s computername

Technorati Tags: ,,,,
Categories
Geeky/Programming SQLServerPedia Syndication

SQL Server: sp_add_job – parameter passing

I am sure there are other SQL procedures that take parameters the same way, but I recently have been dealing with sp_add_job. You can call the procedure and pass in parameters

sp_add_job @job_name = ‘My Job Name’

for an example. But..you cannot do this

DECLARE @test AS VARCHAR(50)

SET @test = ‘ Test ‘

sp_add_job @job_name = ‘My Job Name’ + @test

You will get an error, because you cannot concatonate a string while passing it in as a parameter. What you need to do is this.

DECLARE @test AS VARCHAR(50)

SET @test = ‘My Job Name Test’

sp_add_job @job_name = @test

* Note there are other params you need to pass to the procedure, but I just passed the one for example’s sake

Technorati Tags: ,,
Categories
Geeky/Programming SQLServerPedia Syndication

.NET – Fastest Way to Load Text File To SQL – SqlBulkCopy

An exercise in coding. Loading a text file to SQL. There are a billion different ways to do it, and depending on your source data format and such, you can parse the file a million ways too. But how fast can you get data from disk to SQL using .NET code? (VB.NET or C#) . This post is going to show the differences in some of the ways you can load data from disk to SQL.

I am sure I could do more, but this is a good sampling. Lets assume a 1,00,000 row file, comma separated with 3 columns, string, int, string of variable length. Lets assume our destination is SQL Server 2005, table already created, no keys or anything on the table.

We will call our table LoadedData. Our test app will be a VB.NET Console Application, running on the same box as SQL 2005 is loaded. Now, there are many ways to load files. A few are: Reading them line by line, ReadToEnd() and also using the JET engine to read in a CSV, etc. From the testing I have been doing, all of these seem to work fairly fast, maybe a comparison on these is for another blog post, but for brevity’s sake, lets just say they are all comparable. Now, I chose 3 methods of inserting data.

1) StreamReader.ReadLine, Insert Line By Line

Sub Method1()
    Dim i As Long = 0

    Dim sr As StreamReader = New StreamReader(filename)
    Dim line As String = sr.ReadLine()
    Dim dbConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("MyDB").ToString())
    Dim dbCmd As SqlCommand = New SqlCommand()
    dbCmd.Connection = dbConn

    Dim wholeFile As String = sr.ReadToEnd()

    Do
        Dim fields() As String = line.Split(",")

        dbCmd.CommandText = "INSERT INTO dbo.TestData (Column1,Column2,Column3) " & _
        " VALUES (‘" & fields(0) & "’," & fields(1) & ",’" & fields(2) & "’)"

        dbConn.Open()
        dbCmd.ExecuteNonQuery()
        dbConn.Close()
        i = i + 1
        line = sr.ReadLine()

    Loop While Not line = String.Empty

End Sub

2) StreamReader.ReadLine, Batch Insert With DataAdapter

Sub Method2()
    Dim i As Long = 0
    Dim dbConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("MyDB").ToString())
    Dim sr As StreamReader = New StreamReader(filename)
    Dim line As String = sr.ReadLine()

    Dim strArray As String() = line.Split(",")
    Dim dt As DataTable = New DataTable()
    Dim row As DataRow

    For Each s As String In strArray
        dt.Columns.Add(New DataColumn())
    Next

    Do

        row = dt.NewRow()
        row.ItemArray = line.Split(",")

        dt.Rows.Add(row)

        i = i + 1
        line = sr.ReadLine()

    Loop While Not line = String.Empty

    Dim dataAdapter As New SqlDataAdapter()

    dataAdapter.SelectCommand = New SqlCommand("SELECT TOP 1 Column1,Column2,Column3 from dbo.TestData", dbConn)

    Dim cmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(dataAdapter)

    dbConn.Open()

    Dim ds As DataSet = New DataSet
    dataAdapter.Fill(dt)

    dataAdapter.UpdateBatchSize = 1000
    dataAdapter.Update(dt)

    dbConn.Close()
End Sub

3) StreamReader.ReadLine, SqlBulkCopy

Sub Method3()
    Dim i As Long = 0
    Dim dbConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("MyDB").ToString())
    Dim sr As StreamReader = New StreamReader(filename)
    Dim line As String = sr.ReadLine()

    Dim strArray As String() = line.Split(",")
    Dim dt As DataTable = New DataTable()
    Dim row As DataRow

    For Each s As String In strArray
        dt.Columns.Add(New DataColumn())
    Next

    Do

        row = dt.NewRow()
        row.ItemArray = line.Split(",")

        dt.Rows.Add(row)

        i = i + 1
        line = sr.ReadLine()

    Loop While Not line = String.Empty

    Dim bc As SqlBulkCopy = New SqlBulkCopy(dbConn, SqlBulkCopyOptions.TableLock, Nothing)
    bc.DestinationTableName = "TestData"

    bc.BatchSize = dt.Rows.Count

    dbConn.Open()
    bc.WriteToServer(dt)
    dbConn.Close()
    bc.Close()
End Sub

The results of the 3 methods are surprising. The thing is, most people are going to use Method1 because it just is the first thing you think of doing, and maybe the easiest to code (everyone learns loops in school, etc) – now, nitpickers will say "use a stored proc" etc – that will save minimal time, and in best practice yes, but for the sake of the example bear with it..

Method2 is less intuitive, and really tricky to get working (at least I had some issues with it) but once it works, it makes a little bit more sense then Method1.

Method3 is something that no one ever hears or uses, but once they do, they never go back.

Side note: about 5 years ago I worked on a program that inserted huge files, and they were taking 10-20 minutes a piece. I was using VB6, and converted the line by line insert to use BCP from code and got it down to 2-3 minutes, which was good. So I know about BCP and BULK INSERT. I just didn’t know it was built into .NET, now I do..anyways, on to the results.

Method 1- 14.69 minutes to insert 1 million records

Method 2 – 7.88 minutes to insert 1 million records

Method 3 – 0.28 minutes to insert 1 million records

So yeah. Wow. That is not a typo for Method 3. Roughly 17 seconds. Now, give Method2 so credit, it reduced the time from Method1 by 50% but Method3 just KILLS them both. Man, just awesome. When you run something like that and see that kind of performance, you can’t help but smile.

A few caveats and gotchas:

Method2 – the BatchSize property I have set to 1000. If you set to 0 it uses the max. I tried this and locke
d my machine up. Yikes.

Method3 – The SqlBulkCopyOptions makes a difference – TableLock speeds up the operation. The BatchSize here I have set to the # of rows, It might run differently with different batch sizes, I really didn’t experiment with it, but adding the Copy Options and BatchSize, it sped up the operations.

So, the fastest way I have found in .NET to load data from files to sql – hands down – SqlBulkCopy. Since it took 17 seconds, The next step is to try different file reading methods and see what time times are there, like I said, maybe that will be my next post. 🙂

p.s. yes I am up at 2:00 AM posting this, insomnia anyone? In any event I got to watch the repeat of the Nevada Democratic Debate 🙂

Technorati Tags: ,,,,,,
Categories
Geeky/Programming

How To: Connect to SQL Server, VS TFS, etc using Windows Authentication when computer is not on Active Directory Domain (XP and Vista!)

Whew, long title, amazing results!

Problem: You have a laptop or computer and you are working remotely for a company. You VPN in. Your computer is not on their Active Directory (AD) domain. You try to connect to SQL Server using SSMS or Analysis Services using Excel, but it doesn’t work because it is using your user, not a domain user. How do you get around this?

Answer: Well, this is what I have found (tested on XP only) – start->run: computernamec$ – then it prompts you to login. Use your AD username and password, so

domainusername and password, and check the box to save password.

Seems that XP will save that in your authentication list somewhere, and then you can use SSMS or Excel to connect to the SQL Server via Windows Authentication!

This trick also works for TFS Build Servers/Team Explorer (tested with VS2005 Team Explorer) ..

Now for the fun part – Vista. The tricks above don’t work on Vista, but you can still get it to work. Here is what you do…create some shortcuts…

C:WindowsSystem32runas.exe /netonly /user:domainusername “C:Program FilesMicrosoft OfficeOffice12excel.exe”

C:WindowsSystem32runas.exe /netonly /user:domainusername “C:Program FilesMicrosoft Visual Studio 8Common7IDEdevenv.exe”

C:WindowsSystem32runas.exe /netonly /user:domainusername “C:Program FilesMicrosoft SQL Server90ToolsBinnVSShellCommon7IDESqlWb.exe”

 

Replace “domainusername” with your info. So if your domain is mycompany then it would be mycompanysteve.novoselac for example.

What happens is that then when you run those apps from those shortcuts it will prompt you for your domain password, you put it in, and it runs the app in the context of your domain user. You can then change the icon for each of these pretty easy, just browse to the exe in the second part when clicking the change icon button on the shortcut properties (the shortcuts are actually links to runas.exe which is a generic icon)

In Vista, for instance, if you are testing SQL (SSMS), you might get this error:

Login failed for user ''. The user is not associated with a trusted SQL Server connection. (Microsoft SQL Server, Error: 18452)

The shortcuts above will get you around it in the situation where your computer is not not on the domain or you are not logged in as a domain user..

These tricks above are especially good if you need to connect to SSAS (Analysis Services) since it is only Windows Authentication. And also, the IT department doesn’t really need to have consultant machines on the domain, or VM’s set up, etc, instead they can use these workarounds

Technorati tags: , , , , , , , , , , , , , , , , , ,