任意過濾器如果你的唯一標識符鍵是一個隨機的GUID(不與NEWSEQUENTIALID()或UuidCreateSequential生成),你可以使用下面的方法。這將使用聚集主鍵索引而不對所有行進行排序。
SELECT t1.*
FROM (VALUES(
NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID())
,(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID())
,(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID()),(NEWID())) AS ThirtyKeys(ID)
CROSS APPLY(SELECT TOP (1) * FROM dbo.Table1 WHERE ID >= ThirtyKeys.ID) AS t1;
[檢查這一點,我認爲這可以幫助你(https://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql) – Dogan
您是否有表一個唯一的索引或主鍵? –
是的,它有主鍵和唯一索引 – Manikandan