在一张10万行产品表(Product)中,随机取10条数据的几种方式:
SET STATISTICS IO ON SELECT TOP 10 ID FROM dbo.Product(NOLOCK) WHERE 0.01 >= CAST(CHECKSUM(NEWID(), ID) & 0x7fffffff AS FLOAT) / CAST(0x7fffffff AS INT) --扫描计数 1,逻辑读取 5 次 SELECT TOP 10 * FROM Product ORDER BY NEWID() --扫描计数 17,逻辑读取 11628 次 SELECT TOP 10*, NEWID() AS random FROM Product ORDER BY random --扫描计数 16,逻辑读取 11242 次 SELECT ID FROM Product WHERE ID IN (SELECT TOP 10 ID FROM Product ORDER BY NEWID()) --扫描计数 17,逻辑读取 2014 次