Select n random rows from SQL Server table

前端 未结 16 1076
陌清茗
陌清茗 2020-11-22 10:54

I\'ve got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I\'ve thought of a complicated way, creating a temp table wi

16条回答
  •  一整个雨季
    2020-11-22 11:35

    The server-side processing language in use (eg PHP, .net, etc) isn't specified, but if it's PHP, grab the required number (or all the records) and instead of randomising in the query use PHP's shuffle function. I don't know if .net has an equivalent function but if it does then use that if you're using .net

    ORDER BY RAND() can have quite a performance penalty, depending on how many records are involved.

提交回复
热议问题