SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

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


I'm Andy Warren, currently a SQL Server trainer with End to End Training. Over the past few years I've been a developer, DBA, and IT Director. I was one of the original founders of SQLServerCentral.com and helped grow that community from zero to about 300k members before deciding to move on to other ventures.


No comments.

Leave a Comment

Please register or log in to leave a comment.