Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"order by newid()" - how does it work?

I know that If I run this query

select top 100 * from mytable order by newid() 

it will get 100 random records from my table.

However, I'm a bit confused as to how it works, since I don't see newid() in the select list. Can someone explain? Is there something special about newid() here?

like image 510
Tola Odejayi Avatar asked Feb 12 '11 18:02

Tola Odejayi


People also ask

How does Newid order work?

SQL Server NewId() generates a random GUID or unique identifier which can be used to return randomized rows from a SELECT query. T-SQL developers will realize that the return list of a SQL SELECT query is sorted randomly when they place "NEWID() function in the "ORDER BY" clause of the SELECT statement.

How does Newid work in SQL?

SQL NewID function is used for selecting random row from a resultset in SQL Server databases. Using with TOP N keyword in SELECT statements where SQL NEWID is in the ORDER BY statement, random records are selected from a table or from a set of rows.

Is newid () random?

NewID() Creates a unique value of type uniqueidentifier. and your table will be sorted by this random values.

What does Newid return?

The "NEWID()" function, when used with an order by clause, will return a set of random rows from a table.


2 Answers

I know what NewID() does, I'm just trying to understand how it would help in the random selection. Is it that (1) the select statement will select EVERYTHING from mytable, (2) for each row selected, tack on a uniqueidentifier generated by NewID(), (3) sort the rows by this uniqueidentifier and (4) pick off the top 100 from the sorted list?

Yes. this is pretty much exactly correct (except it doesn't necessarily need to sort all the rows). You can verify this by looking at the actual execution plan.

SELECT TOP 100 *  FROM master..spt_values  ORDER BY NEWID() 

The compute scalar operator adds the NEWID() column on for each row (2506 in the table in my example query) then the rows in the table are sorted by this column with the top 100 selected.

SQL Server doesn't actually need to sort the entire set from positions 100 down so it uses a TOP N sort operator which attempts to perform the entire sort operation in memory (for small values of N)

Plan

like image 151
Martin Smith Avatar answered Sep 16 '22 15:09

Martin Smith


In general it works like this:

  • All rows from mytable is "looped"
  • NEWID() is executed for each row
  • The rows are sorted according to random number from NEWID()
  • 100 first row are selected
like image 43
Uhlen Avatar answered Sep 16 '22 15:09

Uhlen