NewID() is commonly used in the order by to return the result set in random fashion. No idea whether its a "true" random, but usually good enough for day to day purposes. It's a decent technique and easy enough to apply, but it can be a performance killer. In the situation I saw they wanted to return a sampling of the matching universe to the user and they accomplished this via newid. The downside is that in some cases there could be 200k records in the initial match set, so you have to wait for SQL to assign the newid's to each row and then do the sort. And the wait can be more than a second or two! They had considered just adding a uniqueidentifer column to the table just for purposes of randomizing. Trade off there is that you add 16 bytes per row, and then you need to index it to make it effective, requiring more space and overhead. In the example I saw it looked like the overhead would have made sense, removing the newid from the order by reduced query time by about 50%.
Book Review: Big Red - Voyage of a Trident Submarine
I've grown up reading Tom Clancy and probably most of you have at least seen Red October, so this book caught my eye when browsing used books for a recent trip. It's a fairly human look at what's involved in sailing on a Trident missile submarine...
2009-03-10
1,439 reads