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?
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.
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 ofN
)