May 2009
Mon Tue Wed Thu Fri Sat Sun
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Month May 2009

Links for the Week

SQL Server

GUID Ordering in SQL Server – Look, I know what I’ve said about GUIDs. Nobody wants them around, except developers. The point is that sometimes GUIDs are a necessity. We all might as well learn how to deal with them. Martin Bell does a great job of talking about the various ways of generating GUIDs and the pros and cons of each.

Fast ordered delete – Ever want to know how to efficiently perform an ordered delete over a large quantity of data? Wonder no more.

UPDATE Statements and Indexes – Rob Boek shows how to use some cunning wizardry and dynamic SQL to minimize the data values that you change in the database while performing and update operation.

Development

Introducing TypekitJeff Veen andJason Santa Maria have come to gether to bring use Typekit – a way to embed real, honest to goodness fonts on the web. They’ve worked with various font foundries to create web-only licensing that should make the need for Flash techniques like sIFR obsolete. For more on the current, shabby, state of fonts on the web, visit Web fonts now (how we’re doing with that)

An nUnit testfixture file template for resharper that also conforms to stylecop laws – ReSharper is amazing. Absolutely amazing. I’ll be honest, I miss this tool when I’m not at my home machine or when I’m not in Visual Studio and I’m trying to write some code.

Stuff & Things

The Vendor/Client Relationship… in the real world. I laugh at this because I’ve been on both sides of the fence. Now, where do I get my ‘Friends of Brent’ discount?

How to Buy a Suit Online which was promptly followed by From the Comments: How to Get a Great Suit

One quarter ago?

Today at work we discovered that DATEADD can do date math involving quarters. Which, if you ask me, is pretty cool.

In an effort to avoid work investigate this scenario, we started playing around with our calendar table and DATEADD.

SELECT [Date],
       DATEADD(q, -1, [Date]) AS OneQuarterAgo,
       DATEDIFF(d, [Date], DATEADD(q, -1, [Date])) AS diff_days
  FROM Calendar
 WHERE y = 2009

Using that query, we figured out that:

  1. We had a different idea of quarter than SQL Server
  2. We understand why SQL Server works like this
  3. God help us all if we have to figure out financial quarters correctly

Basically, SQL Server says “Give me a date that is three months ago and has the same, or closest, ordinal date value.” This is the most apparent when looking at the output of that previous query specifically for May (three months before May is February):

SELECT [Date],
       DATEADD(q, -1, [Date]) AS OneQuarterAgo,
       DATEDIFF(d, [Date], DATEADD(q, -1, [Date])) AS diff_days
  FROM Calendar
 WHERE y = 2009
   AND m = 5

Which produces:

Date OneQuarterAgo diff_days
2009-05-01 2009-02-01 -89
2009-05-02 2009-02-02 -89
2009-05-03 2009-02-03 -89
2009-05-04 2009-02-04 -89
2009-05-05 2009-02-05 -89
2009-05-06 2009-02-06 -89
2009-05-07 2009-02-07 -89
2009-05-08 2009-02-08 -89
2009-05-09 2009-02-09 -89
2009-05-10 2009-02-10 -89
2009-05-11 2009-02-11 -89
2009-05-12 2009-02-12 -89
2009-05-13 2009-02-13 -89
2009-05-14 2009-02-14 -89
2009-05-15 2009-02-15 -89
2009-05-16 2009-02-16 -89
2009-05-17 2009-02-17 -89
2009-05-18 2009-02-18 -89
2009-05-19 2009-02-19 -89
2009-05-20 2009-02-20 -89
2009-05-21 2009-02-21 -89
2009-05-22 2009-02-22 -89
2009-05-23 2009-02-23 -89
2009-05-24 2009-02-24 -89
2009-05-25 2009-02-25 -89
2009-05-26 2009-02-26 -89
2009-05-27 2009-02-27 -89
2009-05-28 2009-02-28 -89
2009-05-29 2009-02-28 -90
2009-05-30 2009-02-28 -91
2009-05-31 2009-02-28 -92

It’s pretty apparent that the last four days in May have the same date produced by the calculation in the OneQuarterAgo column.

There’s not really anything else left to do here, so I’m going to go back to listening to punk covers of Lady Ga Ga songs.

Links for the Week 2009.05.22

Big pile o’ link love this week. Honestly, I didn’t include a ton of GREAT links from Brent Ozar because people would start to think that Brent Ozar pays me to link to his site and say Brent Ozar a lot. He doesn’t, but if you click on the links to Brent Ozar maybe he’ll see where the traffic came from and pay me to provide links to Brent Ozar

SQL Server

SQL Server 2008 Developer Training Kit Available for Download Denis Gobo provides a link and a quick summary of Microsoft’s most recent training offering for developers that will help get people up to speed with SQL Server 2008.

PASS Virtualization Virtual Chapter That’s right, we have a new thing at PASS. Well, the same old thing has a new name. SIGs are now Virtual Chapters. And Brent Ozar is now in charge of the PASS Virtualization Chapter. Check it out!

Download – SQL Server 2008 Developer Training Kit Free training. Free training. Freetraining. freetraining.

Excel Functions for SQL Server Sometimes I’ll find myself using SQL Server and longing for something from Excel that one of my more management-type friends has shown me. Now I can, in theory, have some of that Excel love right in SQL Server.

What’s a ‘DBA’? I’ve known for a long time that, while I love data, I’m not a DBA… not 100%, at least. Sam Bendayan answers the question and talks about what job title options there are for database professionals

Development

MvcFluentHtml – Fluent HTML Interface For MS MVC ASP.NET MVC doesn’t us a bad method to generate HTML, but there are definitely smoother ways, depending on how preference. Fluent HTML uses one paradigm to make it a bit easier to generate HTML in your views. It’s closer to how Ruby on Rails does things, and I like Rails. A lot. Almost as much as I like SQL Server.

Dear Art Director…

Stuff & Things

The Information Architecture of Personal Music Collections Dan Brown, famed Information Architect not famed author, spent a lot of time thinking about how people interact with music libraries. The poster is from 2005 but, shockingly, not a lot has changed since then.

11 Striking Findings From an Eye Tracking Study Eye tracking is some great stuff, it’s right up there with click tracking. It helps us, as bloggers, figure out what you, the readers, are paying attention to.

How to Maintain a Healthy Lifestyle When You’re Too Busy To Care Title says it all. Lazy? Want to get in better shape? Do this.

Evil Lair: On the Architecture of the Enemy in Videogame Worlds I don’t know what to say about this, really. This is a fascinating article about how architecture current works its way into video games and also how it could be used.

10 for $10 hardcore summer tour This is the coolest idea for a summer tour – 10 bands for $10. If you’re at all into hardcore punk, it’ll be a great show. If you aren’t (which is more than likely since you’re reading this blog), take note because it’s an interesting idea that you might see more of in the future.

How to Build Your Own PC WARNING: NOT SAFE FOR WORK SomethingAwful.com is often flagged as adult content. Don’t visit it if you like keeping your job. That being said, this is a hilarious look at building your own computer. It’s based on Brent Ozar‘s experiences building his hackintosh.

Copying Data Should Be Easy

Typically, moving data isn’t that big of a deal. You can either fire up SSMS and write a quick script to copy data from one server to another. Or, if you need to move data from production into development, you can just do a back up and restore.

“Typically” is the key word in the previous paragraph. In my situation, we have a production master that replicates to two separate servers. The data on each subscriber is different and the determination process is driven by business criteria (actually, it’s driven by a number in a TINYINT column). The publisher is mainly a central target for our ETL processes. As such, it doesn’t have a lot of indexing on it. The subscribers, however, do. Refreshing the development environment is a bit trickier than just restoring over the dev database.

Yeah, sure, destroy. Sounds good.

Yeah, sure, destroy. Sounds good.

Initially, I tried using the Import Data task to copy data across from the production server to the dev server. The trick here is that there were still foreign keys on the table. In case you’re unfamiliar with this feature of the Import and Export Wizard, it gives you the option to “Delete” rows in the destination table. In this case, the word “Delete” actually means “Use TRUNCATE TABLE instead and don’t tell anyone about it”. Well, the fun part is that TRUNCATE TABLE will fail noisily if you have foreign keys in place.

Being an enterprising young man, I thought to myself, I know, I’ll just write a script to write a script that will drop and re-create all of my foreign keys. So, instead of writing the script myself, I twittered and Ted Krueger send back a tweet that led me to this little gem. Don’t mind the sloppy syntax, that script works great. So, after modifying it to give me both the drop and creates, I was ready to go, right?

Not so fast! As we know, every row written to a table also has to be written to all of the non-clustered indexes as well: one table plus four indexes = five writes. This is a large database (just large, not a VLDB). As such, there are a lot of indexes. They are also large. Inserting a large number of rows into a large number of indexes will take, as they say, some time.

Being impatient, and enterprising, I decided to write a script to drop and re-create all of the indexes as well. But, rather than write a script, I just went ahead and found one and modified it to suit my purposes. Actually, I tweeted and Adam Machanic and Mladen Prajdic ended up pointing me toward Tibor Karaszi’s sp_indexinfo – enhanced index information procedure which I was able to easily modify to give me the index drop and create info I needed.

Protip: SQL Server Google hint: Add “vyas” or “tibor” to your searches for DDL scripts and you’ll almost always get a good hit… (Thanks, Adam!)

At this point, everything should be done and our story should be over. You, dear reader, would have falsely assumed that this story has a happy ending.

When I fired up the data import task it would eventually and inevitably fail catastrophically with an error along the lines of “TCP Provider: An existing connection was forcibly closed by the remote host.” We started looking into the actual networking issued before we realized that I was running the export data wizard from my local computer, which shares a hub with several other networked devices. Once I remoted into the development server and attempted the process, I got the following error: “TCP Provider: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.”

This error comes from an issue when you are using the /3GB switch on a 32-bit server that might be copying large quantities of data from one place to another. The solution is do make sure that you still have AWE and PAE enabled, remove the /3GB switch, and then reboot!

I’m still waiting for my reboot. I think I should have just done a backup and restore and copied the indexes back in from production. So much for trying to be clever and automate the process, eh?

Oh, here’s the script that drops and re-creates all of the indexes in the current database. It should be, at the very least, a curiosity to someone.

DECLARE @newline AS NVARCHAR(2);

SET @newline = NCHAR(13) + NCHAR(10);

IF OBJECT_ID(N'tempdb..#indexes') IS NOT NULL
  DROP TABLE #indexes;

WITH key_columns AS
(
  SELECT c.[object_id],
         c.name AS column_name,
         ic.key_ordinal,
         ic.is_included_column,
         ic.index_id,
         ic.is_descending_key
    FROM sys.columns AS c
         INNER JOIN sys.index_columns AS ic
            ON c.[object_id] = ic.[object_id]
           AND ic.column_id = c.column_id
),
physical_info AS
(
  SELECT p.[object_id],
         p.index_id,
         ds.name AS location,
         SUM(p.rows) AS rows,
         SUM(a.total_pages) AS pages
    FROM sys.partitions AS p
         INNER JOIN sys.allocation_units AS a
            ON p.hobt_id = a.container_id
         INNER JOIN sys.data_spaces AS ds
            ON a.data_space_id = ds.data_space_id
   GROUP BY [object_id], index_id, ds.name
)
SELECT OBJECT_SCHEMA_NAME(i.[object_id]) AS schema_name,
       OBJECT_NAME(i.[object_id]) AS table_name,
       i.name AS index_name,
       CASE i.type WHEN 0 THEN N'heap'
                   WHEN 1 THEN N'CLUSTERED'
                   WHEN 2 THEN N'nc'
                   WHEN 3 THEN N'xml'
                   ELSE CAST(i.type AS VARCHAR(2)) END AS [type],
       i.is_unique,
       CASE WHEN is_primary_key = 0 AND is_unique_constraint = 0 THEN N'IX'
            WHEN is_primary_key = 1 AND is_unique_constraint = 0 THEN N'PK'
            WHEN is_primary_key = 0 AND is_unique_constraint = 1 THEN N'UX'
       END AS cnstr,
       STUFF((SELECT CAST(', ' + kc.column_name
                          + CASE kc.is_descending_key WHEN 0 THEN ''
                                 ELSE ' DESC' END AS VARCHAR(MAX)
                     ) AS [text()]
                FROM key_columns AS kc
               WHERE i.[object_id] = kc.[object_id]
                 AND i.index_id = kc.index_id
                 AND kc.is_included_column = 0
               ORDER BY key_ordinal FOR XML PATH ( '' )), 1, 2, '') AS key_columns,
       STUFF((SELECT CAST(', ' + column_name AS VARCHAR(MAX)) AS [text()]
                FROM key_columns AS kc
               WHERE i.[object_id] = kc.[object_id]
                 AND i.index_id = kc.index_id
                 AND kc.is_included_column = 1
               ORDER BY key_ordinal FOR XML PATH ( '' )), 1, 2, '') AS included_columns,
       p.location,
       p.rows,
       p.pages,
       CAST((p.pages * 8.00) / 1024 AS DECIMAL(9, 2)) AS MB,
       s.user_seeks,
       s.user_scans,
       s.user_lookups,
       s.user_updates
  INTO #indexes
  FROM sys.indexes AS i
        LEFT OUTER JOIN physical_info AS p
          ON i.[object_id] = p.[object_id]
         AND i.index_id = p.index_id
        LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s
          ON s.[object_id] = i.[object_id]
         AND s.index_id = i.index_id
         AND s.database_id = DB_ID()
 WHERE OBJECTPROPERTY(i.[object_id], N'IsMsShipped') = 0
   AND OBJECTPROPERTY(i.[object_id], N'IsTableFunction') = 0
   AND OBJECT_NAME(i.[object_id]) LIKE N'%'
   AND i.[type] >< 0
 ORDER BY table_name, i.[type], index_name;

SELECT N' DROP INDEX '
       + QUOTENAME(i.index_name)
       + N' ON '
       + QUOTENAME(i.[schema_name])
       + N'.'
       + QUOTENAME(i.table_name)
  FROM #indexes AS i
 WHERE i.cnstr >< 'PK'; 

SELECT N'ALTER TABLE '
       + QUOTENAME(i.[schema_name])
       + N'.'
       + QUOTENAME(i.table_name)
       + N' ADD CONSTRAINT '
       + QUOTENAME(i.index_name)
       + N' PRIMARY KEY CLUSTERED ('
       + i.key_columns
       + N') ON '
       + QUOTENAME(location)
  FROM #indexes AS i
 WHERE i.cnstr = 'PK';

SELECT N'CREATE '
       + CASE WHEN is_unique = 1 THEN N' UNIQUE ' ELSE '' END
       + CASE WHEN type = 'CLUSTERED' THEN N' CLUSTERED INDEX '
              ELSE N' NONCLUSTERED INDEX ' END
       + QUOTENAME(i.index_name)
       + N' ON '
       + QUOTENAME(i.[schema_name])
       + N'.'
       + QUOTENAME(i.table_name)
       + N' ('
       + key_columns
       + N')'
       + CASE WHEN included_columns IS NOT NULL THEN N' INCLUDE (' + included_columns + N')'
              ELSE N'' END
       + N' ON '
       + QUOTENAME(i.location)
  FROM #indexes AS i
 WHERE i.cnstr >< 'PK'
   AND i.location IS NOT NULL;

RAISERROR and BIGINT

Thanks to Ted Krueger (onpnt if you’re on twitter or use lessthandot.com), I now know how to display a BIGINT in RAISERROR. You just used %I64d. It’s documented in BOL as a sidebar note, but you still have to wade through the RAISERROR documentation.

So, our code would look like this:

DECLARE @i AS BIGINT;
SET @i = 1;

RAISERROR('%I64d is a very big number', 0, 43, @i);

Here’s the link to the BOL entry: RAISERROR.

Thanks, twitter, for making this all possible!

Links for the Week of 2009.05.15

SQL Server

Find Last Date Time Updated for Any Table Ever want to know when a table was last updated? Now you can find out. Thanks to Pinal Dave for this slick little piece of code, I was totally astounded to know that you could do that.

Why use stored procedures? A very well reasoned argument for the use of stored procedures from Paul Nielsen.

Development

More ASP.NET MVC Best Practices Working with ASP.NET MVC? I know I haven’t had a chance to do much so I’ve got plenty of room to mess things up. That’s why best practices from others in the field are important to pay attention to.

What’s in your svn:ignore? Anyone who has ever used subversion is well aware of the pains that come when you get preference files and DLLs in your source control repository. How do you prevent that? By mucking around with svn:ignore, of course! (This also applies to anyone using VSTSDD, by the way.)

Unit tests vs Integration tests Ayende Rahien sums it up pretty well in just a sentence or two. I’ve had problems making the distinction clear to people in a succint way in the past. No longer shall I be at a loss for words!

Stuff & Things

6 Small Things You Can Do When You Lack Discipline So being disciplined is the key to success, right? And you’re having problems staying disciplined, so that clearly means you’re a failure, right? Wrong. None of us are 100% disciplined 100% of the time. Check out this post from Leo Babauta over at zenhabits.net for some great tips to keep yourself going.

How to Learn a Little Every Day I cannot stress this enough: learning is the most important thing you can ever do. I say it all the time. When people ask me what motivates me, I always respond that my primary motivation in everything is learning. Here are some great hints from Dumb Little Man on incorporating getting your learn on into your daily schedule.

burning fight Awesome live concert photos from Matt Miller. You can hire him for your less noisy events. After looking at his portfolio I would say that you probably should hire him.

On Non-Disclosure Agreements Seriously, this is what you sound like. Stop it. Unless it’s for humorous purposes. In that case you can go NDA yourself.

CBusPASS Meeting Recap

Thanks to everyone who made it out to the CBusPASS meeting tonight. A special thanks to Brent Ozar for taking time out of his evening to deliver his presentation on Using Cloud-Based BI to Interpret Perfmon & Profiler Results. This was a great follow-up to his earlier presentation – Perfmon and Profiler 101.

Turn out was a bit light, with 8 physical and 8 virtual attendees. However, this gave us the opportunity to focus on the presentation and have a great conversation afterward. It was a great meeting and I’d like to thank everyone again for coming out.

PASS App Dev SIG Live Meeting – May 19th, 1:00 PM

Rolling Your Own Replication

Presenter: Brent Ozar

SQL Server’s built-in replication has made great strides in the last couple of versions, but what if it’s still not enough? One team needed more flexibility, easier administration and higher scalability, so they built their own solution instead. Brent Ozar will discuss how the system was architected, the pros and cons, and how you can build a similar solution for your own needs. He’ll explain some of the lessons learned in scaling this out to thousands of remote SQL Servers

How Do I View the Presentation?

Follow the link and join the LiveMeeting.

Audio is provided through a conference bridge:
Toll-free: +1 (866) 379-8990
Participant code: 6489756

If this is your first time using LiveMeeting, please make sure to check your system for compatability issues.

Adding XML Schemas in MonoDevelop on OS X

Let’s say you’re crazy enough to have installed MonoDevelop on OS X. Let’s also postulate that you plan on using NHibernate on OS X to write an ASP.NET MVC app because you are, frankly, far too lazy to boot a virtual machine. Let us further supposed that you want to have sweet sweet XML syntax completion for your NHibernate configuration files.

How do you do such a thing?

Well, first things first you get a hold of the NHibernate source code. Then, you follow Benjamin Day’s excellent instructions on how to get intellisense working for NHibernate XML files in Visual Studio. The important part here is to copy the schema files to /Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/AddIns/MonoDevelop.XmlEditor/schemas instead of the path he specifies. Mainly because your mac doesn’t have a C: drive… unless you’re weird.

Update I lied, this makes MonoDevelop hang on start. I’m going to see what I can figure out and update later.

Extra Update I went through this process again, and MonoDevelop started up properly and everything worked correctly in my .hbm.xml file. There you have it. It works and I clearly did something silly the first time around.

CBusPASS Meeting – 2009.05.14

Yup, it’s that time again. Time for you to get your SQL shoes on and head on out to CBusPASS, your Columbus source for SQL Server related information. Apart from MSDN… and the library… and the internet.

When: Thursday, May 14th, 6:30PM – 8:30PM
Where: Battelle For Kids, Suite 500, 1160 Dublin Rd, Columbus, OH 43215

IMPORTANT NOTE: Battelle For Kids has moved to Suite 500, which is on the opposite end of the building from where it used to be.

Food and drinks will be provided.

If you have problems locating the venue, you can hit me up on twitter or via telephone/text at 614.515.0727

Using Cloud-Based BI to Interpret Perfmon & Profiler Results

After learning how to use Perfmon and Profiler to gather performance statistics about your SQL Server, it still takes a lot of time to interpret those results and figure out what’s going on. Microsoft’s SQL Server Data Mining team has built a free cloud-based data mining tool for Excel that can help slice and dice mountains of data and help you make sense of it all.

Even if you’re not ready for BI in the cloud, you can use this same type of tool in combination with a local SQL Server Analysis Services instance. Wait! Don’t freak out – it’s much easier than you think, and you never have to leave the comforting environment of Excel. Even if this doesn’t sound like fun to you, you might want to learn about it because mid-level managers in your company might want to use this technique to analyze sales or customer data.

Attendees will learn how to install & configure data mining in Excel, how to analyze Perfmon data to break the server’s load into categories, and how to use BI to write a performance report about your SQL Server.

Brent Ozar

Brent is a SQL Server Domain Expert with Quest Software. Brent has a decade of broad IT experience, performing systems administration and project management before moving into database administration. In his current role, Brent specializes in performance tuning, disaster recovery and automating SQL Server management. Previously, Brent spent 2 years at Southern Wine & Spirits, a Miami-based wine & spirits distributor.

Brent has experience conducting training sessions, has written several technical articles, and blogs prolifically at http://www.BrentOzar.com. He’s currently writing SQL Server Internals and Troubleshooting for Wiley/Wrox along with Christian Bolton, Justin Langford and Cindy Gross. He’s Editor-in-Chief at SQLServerPedia.com, where he also records video podcasts.

If you can’t attend in person, you can join us using LiveMeeting:

Join the meeting.

Audio Information – Computer Audio
To use computer audio, you need speakers and microphone, or a headset.

First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.

Troubleshooting
Unable to join the meeting? Follow these steps:

1. Copy this address and paste it into your web browser:

https://www.livemeeting.com/cc/usergroups/join

2. Copy and paste the required information:
Meeting ID: NM86HK
Entry Code: 5\-b6|jkH
Location: https://www.livemeeting.com/cc/usergroups

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 219 items have been purified.