Oracle开发专题之:随机查询
来源:互联网 发布:钢制散热器的技术数据 编辑:程序博客网 时间:2024/06/05 03:50
http://www.blogjava.net/pengpenglin/archive/2009/03/19/206796.html
【1】方法一:通过dbms_random.random
select * from (select * from largetable order by dbms_random.random) where rownum <= 20000;
【2】方法二:通过dbms_random.value
select * from (select * from largetable order by dbms_random.value) where rownum <= 20000;
【3】方法三:通过采样表扫描
select * from (select * from largetable sample(10)) where rownum <= 20000;
下面我们通过实践来比较这3种方法的效率,首先我们创建一个包含有10W条记录的表用于实验:
create table LARGETABLE
(
ID NUMBER not null primary key,
BIRTHDAY DATE not null
)
(
ID NUMBER not null primary key,
BIRTHDAY DATE not null
)
接下来我们插入10W条数据
create or replace procedure random_insert as
i number;
startDate date := sysdate;
begin
for i in 1 .. 100000 loop
insert into largetable values (i, startDate + 1);
end loop commit;
end;
i number;
startDate date := sysdate;
begin
for i in 1 .. 100000 loop
insert into largetable values (i, startDate + 1);
end loop commit;
end;
在SQL*PLUS下设置显示SQL语句执行时间:set timing on,让后分别运行上述三条语句:
第一个的执行时间为 00: 00: 16: 04
第二个的执行时间为 00: 00: 54: 04
第三个的执行时间为 00: 00: 08: 07
从这里我们可以看出在进行数据随机抽取时,采用sample的方法效率是最高的。为了保证每次随机查询的数据尽量不重复,我们可以把sample中的百分比提高一些(例如从10%提高到20%)。
关于Oracle Sample的介绍和用法,请参考eygle他老人家的一篇文章介绍:如何从结果集中获得随机结果
- Oracle开发专题之:随机查询
- Oracle开发专题之:随机查询
- Oracle开发专题之:随机查询
- Oracle开发专题之:级联查询(Hierarchical Queries)
- Oracle开发专题之:时间运算
- Oracle开发专题之:分析函数(OVER)
- Oracle开发专题之:分析函数总结
- Oracle开发专题之:删除重复记录
- Oracle开发专题之:行列转换
- Oracle开发专题之:时间运算
- Oracle开发专题之:分析函数(OVER)
- Oracle开发专题之:分析函数总结
- Oracle开发专题之:行列转换
- (转)Oracle开发专题之:时间运算
- Oracle开发专题之:时间运算
- Oracle开发专题之:报表函数
- Oracle开发专题之:窗口函数
- Oracle开发专题之:分析函数(OVER)
- SQL Server 2005 Integration Services (SSIS) (2) - 导入/导出向导
- 真正的DocumentComplete
- 一些编码对应的code page
- 支持向量机(SVM)特辑 & Michael Jordan
- 怎样获得可以下载资源的积分
- Oracle开发专题之:随机查询
- JavaScript 增加 与调试
- c# 连接access2007且查询
- 正在学习JAVA。。。
- 两个结构完全相同的表a和b,主键为index,使用SQL语句,把a表中存在但在b表中不存在的数据插入的b表中
- 软件工程师的6年总结
- 程序的价值
- 用递归些线索二叉树建立的注释
- what is dvd ripper for pc