How to request a random row in SQL?

前端 未结 29 2903
孤城傲影
孤城傲影 2020-11-21 06:45

How can I request a random row (or as close to truly random as is possible) in pure SQL?

29条回答
  •  有刺的猬
    2020-11-21 07:33

    Be careful because TableSample doesn't actually return a random sample of rows. It directs your query to look at a random sample of the 8KB pages that make up your row. Then, your query is executed against the data contained in these pages. Because of how data may be grouped on these pages (insertion order, etc), this could lead to data that isn't actually a random sample.

    See: http://www.mssqltips.com/tip.asp?tip=1308

    This MSDN page for TableSample includes an example of how to generate an actualy random sample of data.

    http://msdn.microsoft.com/en-us/library/ms189108.aspx

提交回复
热议问题