Never Stop Learning

In Development by Paul1 Comment

I’m always trying to learn new things. A great example was when I worked on Microsoft Encarta Class Server. When we started that project, there were only a few developers on it. We needed a database to store information — perhaps something based on SQL Server. But none of us had ever used SQL. So I learned.

It took a while to understand how SQL Server “thinks”. I still cringe when I think back to some of those first SQL stored procedures that I wrote. They actually had loops in them! SQL is optimized to do things in batches. For instance, I could write a statement that updates a value in all rows that match certain criteria:

     UPDATE Address
     SET OlderThan1990 = 1
     WHERE Date < '1/1/1990'

But some of my first SQL stored procedures looked more like this:

     DECLARE @id int
     DECLARE @date datetime
 
     DECLARE MyCursor CURSOR FOR
     SELECT Id, Date
     FROM Address
 
     OPEN MyCursor
     FETCH NEXT FROM MyCursor INTO @id, @date

     WHILE @@FETCH_STATUS = 0
     BEGIN

         IF @date < '1/1/1990'
         BEGIN

             UPDATE Address
             SET OlderThan1990 = 1
             WHERE id = @id

         END

         FETCH NEXT FROM MyCursor INTO @id, @date

    END

    CLOSE MyCursor
    DEALLOCATE MyCursor

Because of the loop, that ran a lot slower. Thankfully, I learned quickly. Over the next few months, I became the SQL expert on that team. On later teams, I was known as the “SQL guy”. I regularly had people from other teams asking me for SQL performance and schema suggestions. And I almost never wrote a loop in SQL again.

 

 

Share:

Comments

  1. Pingback: Android Studio & Java Impressions: Part 1 | Galden Studios

Leave a Comment