Blog Post

The Perils of NewID for Randomizing Results

,

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%.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating