Tuesday, February 28, 2006

When to use cursors

This is cool article that describes a good reason for using cursors. The knee jerk reaction from most SQL Gurus is usually "Cursors bad, sets good". But there are times where it actually makes sense to use a cursor over set logic. Adam Machanic describes a few ways to generate a running sum of one of the columns of data in a record set. His gut reaction was to use varies combinations of self-joins, but the performance was hideous. For each row, you have to sum the values of all of the rows preceding, the performance cost is exponential to the size of the result set.

When he rewrote the query to use a cursor, the performance was dramaticly better, each row only needed to be read once. The performance cost was linear to the number of rows read.

If you stand back and look at the big picture, you may still want to avoid the cursor and just accumulate the running total in the code that is calling the query. That may prove to be the fastest way to execute this type of query.

Friday, February 17, 2006

Get just the date from a SQL Server datetime column

This should be faster than the other way ( converting it to a varchar and lopping off the time part).

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

Courtesy of [Greg's Cool [Insert Clever Name] of the Day]

Tuesday, February 14, 2006

Mini-primer on handling exceptions in .NET

There's a lot of good technique in a short article. I wont repeat it all here, just a taste...

Code Sample #3:
try {
    // code
} catch (Exception e) {
   throw new ApplicationException("Some useful message about what we're doing: " + e.Message);
}
 
Whoah again. We just lost a lot of information about the original exception, including its stack trace and any inner exceptions it contained and made our debugging lives a lot harder. I would recommend the following code instead:
 
try {
    // code
} catch (Exception e) {
   throw new ApplicationException("Some useful message about what we're doing", e);
}
 
This code propagates the original exception as the innerException so that we can see the exact origin of the failure.

Courtesy of [James Kovacs' Weblog]

Monday, February 13, 2006

Preventing Automatic Update from rebooting your machine


Here's how to prevent Automatic Update from rebooting your machine:

  1. Start -> Run
  2. Type: gpedit.msc
  3. Expand Local Computer Policy / Computer Configuration / Administrative Templates / Windows Components / Windows Update
  4. Double-click "No auto-restart for scheduled Automatic Updates installations"
  5. Select "Enabled", then OK. Close the Group Policy configuration
    program.

Fine print: 

  1. You need Administrator priveleges to make this setting.

  2. Some people complained that Windows ignored the "no auto-restart" setting for the WMF patch. Potentially a ID 10T error, though. 

From [JonGalloway.ToString()]

In the comments, there was another good tip for Windows Server 2003: Under Administrative Templates / System right-click on "Display Shutdown Event Tracker" and select properties. Select Disabled to prevent Windows from asking for a reason why you are shuting down Win2003 Server.