select a.title, a.type from ( select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type from a a1 ) a where a.rcn<1000000; 此处,将title换为ID即可
最快的方法,根据ROWID来取至于其他考虑细节,自己想想吧,呵呵
我第一想法也是rowid 但是觉得rowid没什么规律 不知道杨兄有什么好的方法
1、如果ID有规则,且可以重新生成,则可以据此规律生成一个包含100万个ID记录的表,然后关联取出数据,最后记录可通过create table as 的方式生成放入临时表中。 2、使用数据块采样的方式,避免全表扫描会更快。先计算采样的百分比。如:1百万/1亿=1%最后使用如下语句(当然采样1%数据块可能不是刚好一百万数据,如果少可以增加一点百分比,如果多于则使用rownum限制): create table t_tmp as select * from t_sr_servicerequest sample block(1) where rownum<1000000;
当然,最后在t_tmp后加上nologging减少重做日志产生。
Id既然是主键,那么直接取随机的id不就可以了吗?
select a.title, a.type from ( select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type from a a1 ) a where a.rcn<1000000; 此处,将title换为ID即可 推荐一下这个,sys_guid()可以生成32位序列号 每次生成不一样 可以用过表主键值,据说效益比sequence更高!
--从一个表中随机取100W条记录的5种方法 --1.dbms_random.value select * from(select * from t_ums_config order by dbms_random.value) where rownum <= 1000000; --2.dbms_random.random select * from(select * from table_name fig order by dbms_random.random) where rownum <= 1000000; --3.sys_guid() select * from(select * from table_name order by sys_guid()) where rownum <= 1000000; --4.sample(20) 按百分比 select * from(select * from table_name sample(20)) where rownum <= 1000000; --5.sample block(20) 按数据块 select * from(select * from table_name sample block(20)) where rownum <= 1000000; 4.5效率相对好些!
4.5要改下:--4.sample(1) 按百分比 select * from(select * from table_name sample(1)) where rownum <= 1000000; --5.sample block(1) 按数据块 select * from(select * from table_name sample block(1)) where rownum <= 1000000;
select * from (select * from tb order by dbms_random.value) where rownum<=1000000估计最好的方法也就使用采样表扫描 select * from tb sample(20) where rownum<=1000000
select * from tb sample(20) where rownum<=1000000 1亿=100个100w 每次采样1 就够了 但这里采样20% 然后取100w 会不会取到重复的啊
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.rcn<1000000;
此处,将title换为ID即可
不知道杨兄有什么好的方法
2、使用数据块采样的方式,避免全表扫描会更快。先计算采样的百分比。如:1百万/1亿=1%最后使用如下语句(当然采样1%数据块可能不是刚好一百万数据,如果少可以增加一点百分比,如果多于则使用rownum限制):
create table t_tmp as select * from t_sr_servicerequest sample block(1) where rownum<1000000;
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.rcn<1000000;
此处,将title换为ID即可
推荐一下这个,sys_guid()可以生成32位序列号 每次生成不一样 可以用过表主键值,据说效益比sequence更高!
--从一个表中随机取100W条记录的5种方法
--1.dbms_random.value
select * from(select * from t_ums_config order by dbms_random.value) where rownum <= 1000000;
--2.dbms_random.random
select * from(select * from table_name fig order by dbms_random.random) where rownum <= 1000000;
--3.sys_guid()
select * from(select * from table_name order by sys_guid()) where rownum <= 1000000;
--4.sample(20) 按百分比
select * from(select * from table_name sample(20)) where rownum <= 1000000;
--5.sample block(20) 按数据块
select * from(select * from table_name sample block(20)) where rownum <= 1000000; 4.5效率相对好些!
select * from(select * from table_name sample(1)) where rownum <= 1000000;
--5.sample block(1) 按数据块
select * from(select * from table_name sample block(1)) where rownum <= 1000000;
from (select * from tb order by dbms_random.value) where rownum<=1000000估计最好的方法也就使用采样表扫描
select * from tb sample(20) where rownum<=1000000
1亿=100个100w
每次采样1 就够了 但这里采样20% 然后取100w
会不会取到重复的啊
每次查一个 哈哈