日期:2014-05-19  浏览次数:20508 次

如何随机把数据库平分为n份
100多万条数据的一个表
我想随机把表内数据分成N份,放到不同的表里面
表结构是
id   user   pass1   pass2   name
5个字段
一定要随机挑选数据
请问如何做?


------解决方案--------------------
/*
假设随机分配到3张表
table_1,
table_2,
table_3 中去
*/
declare @count int
create table #test(ident int identity(1,1),PK_field int)
--pk_id为mytable主键
insert into #test(PK_field) select mytable.pk_id from mytable order by newid()
set @count=@@rowcount

--以下的写入可以写动态语句完成,这个仅仅是例子而已

insert into table_1 select mytable.* from mytable,#test where ident <=
@count/3 and pk_field=pk_id

insert into table_2 select mytable.* from mytable,#test where ident <=
(@count*2)/3 and ident> @count/3 and pk_field=pk_id

insert into table_3 select mytable.* from mytable,#test where ident>
(@count*2)/3 and pk_field=pk_id

--这样就算完成了
--最后删除临时表
drop table #test