100万条数据的表如何快速随机抽取10条数据

wxz280973534 2012-02-28 01:06:18
select top 10 * from tablename order by newid()
这个速度太慢了,要等半分钟。有没有快速点的办法。

表的主键是varchar类型的,主键是字符串而不是int
...全文
889 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wxz280973534 的回复:]
引用 7 楼 wufeng4552 的回复:
引用 6 楼 wxz280973534 的回复:
引用 3 楼 wufeng4552 的回复:
SQL code

--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()


说rows附近有语法错误

你是2000的

20……
[/Quote]
那应该没语法错误
看看你数据库的兼容级别是不是80的了
如果是80的就有语法错误
wxz280973534 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wufeng4552 的回复:]
引用 6 楼 wxz280973534 的回复:
引用 3 楼 wufeng4552 的回复:
SQL code

--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()


说rows附近有语法错误

你是2000的
[/Quote]
2005的数据库
wxz280973534 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
SQL code

set nocount on--75151448行数据
select count(*) from syscolumns a,syscolumns b,syscolumns c

set statistics time on

select
top 10 * from(select a.name as name,left(b.name,10) as name……
[/Quote]

这个是什么意思啊?看不懂,太深奥了,
我的比如表名是 T1列名是C1,要怎么写sql语句
水族杰纶 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wxz280973534 的回复:]
引用 3 楼 wufeng4552 的回复:
SQL code

--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()


说rows附近有语法错误
[/Quote]
既然是随即
我觉得可以没必要在100W里随即
可以在前10或者20W里
再查随即看看
水族杰纶 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wxz280973534 的回复:]
引用 3 楼 wufeng4552 的回复:
SQL code

--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()


说rows附近有语法错误
[/Quote]
你是2000的
wxz280973534 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wufeng4552 的回复:]
SQL code

--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()
[/Quote]

说rows附近有语法错误
wxz280973534 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hyrongg 的回复:]
直接 top 10 ,不用order by 应该最快了
[/Quote]

要随机抽取
hyrongg 2012-02-28
  • 打赏
  • 举报
回复
直接 top 10 ,不用order by 应该最快了
水族杰纶 2012-02-28
  • 打赏
  • 举报
回复
--try
select top 10 *
from tablename tablesample(1000 rows)
order by newid()
叶子 2012-02-28
  • 打赏
  • 举报
回复
数据量太大,每次都order by newid()效率上确实会有问题。
--小F-- 2012-02-28
  • 打赏
  • 举报
回复
set nocount on--75151448行数据
select count(*) from syscolumns a,syscolumns b,syscolumns c

set statistics time on

select
top 10 * from(select a.name as name,left(b.name,10) as name1, checksum(newid()) as hash from syscolumns a,syscolumns b,syscolumns c )t
where
hash%100=0

set statistics time off

set nocount off

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧