楼主: ZALBB

请教,ORACLE里如何随机读取表中的纪录?

[复制链接]
论坛徽章:
314
行业板块每日发贴之星
日期:2012-07-12 18:47:29双黄蛋
日期:2011-08-12 17:31:04咸鸭蛋
日期:2011-08-18 15:13:51迷宫蛋
日期:2011-08-18 16:58:25紫蛋头
日期:2011-08-31 10:57:28ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47蜘蛛蛋
日期:2011-10-20 15:51:25迷宫蛋
日期:2011-10-29 11:12:59ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41鲜花蛋
日期:2011-11-09 20:33:30
11#
 楼主| 发表于 2008-4-8 09:37 | 只看该作者
原帖由 joyw 于 2008-4-8 09:30 发表
用sample 行不?

select *  from test sample(10);


但返回纪录数好像无法确定,时多时少.

使用道具 举报

回复
论坛徽章:
46
凯迪拉克
日期:2013-08-22 10:00:10Jeep
日期:2013-08-10 07:21:13ITPUB社区12周年站庆徽章
日期:2013-10-08 14:57:28ITPUB十周年纪念徽章
日期:2011-11-01 16:20:282012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07奥运会纪念徽章:体操
日期:2008-10-24 13:08:31会员2007贡献徽章
日期:2007-09-26 18:42:10马上加薪
日期:2014-04-11 09:34:11秀才
日期:2015-09-06 10:19:32
12#
发表于 2008-4-8 09:40 | 只看该作者
请问LZ,这是啥需求?

使用道具 举报

回复
论坛徽章:
27
会员2007贡献徽章
日期:2007-09-26 18:42:102011新春纪念徽章
日期:2011-02-18 11:43:342010广州亚运会纪念徽章:排球
日期:2011-03-03 12:19:332010广州亚运会纪念徽章:篮球
日期:2011-03-10 14:25:06ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15灰彻蛋
日期:2011-12-28 16:56:322012新春纪念徽章
日期:2012-01-04 11:50:44迷宫蛋
日期:2012-03-09 15:14:20蜘蛛蛋
日期:2012-03-26 09:46:32
13#
发表于 2008-4-8 09:42 | 只看该作者
SQL> select * from ( select * from tab order by dbms_random.value ) where rownum <= 3;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
ALL_PART_COL_STATISTICS                                      VIEW
BOOTSTRAP$                                                   TABLE
V_$DATAFILE                                                  VIEW

SQL> select * from ( select * from tab order by dbms_random.value ) where rownum <= 3;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
USER_UNUSED_COL_TABS                                         VIEW
EXU9TABC                                                     VIEW
PRVT_EPGCTAB_DAD                                             TABLE

SQL> select * from ( select * from tab order by dbms_random.value ) where rownum <= 3;

TNAME                                                        TABTYPE         CLUSTERID
------------------------------------------------------------ -------------- ----------
DBA_ROLLBACK_SEGS                                            VIEW
V_$LOGMNR_REGION                                             VIEW
EXU9TABSU                                                    VIEW

使用道具 举报

回复
论坛徽章:
314
行业板块每日发贴之星
日期:2012-07-12 18:47:29双黄蛋
日期:2011-08-12 17:31:04咸鸭蛋
日期:2011-08-18 15:13:51迷宫蛋
日期:2011-08-18 16:58:25紫蛋头
日期:2011-08-31 10:57:28ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47蜘蛛蛋
日期:2011-10-20 15:51:25迷宫蛋
日期:2011-10-29 11:12:59ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41鲜花蛋
日期:2011-11-09 20:33:30
14#
 楼主| 发表于 2008-4-8 09:47 | 只看该作者
原帖由 cow977 于 2008-4-8 09:40 发表
请问LZ,这是啥需求?


同事请求帮忙,我们只负责提供技术支持,不问需求.

不过随机功能,应该是很有用的.

使用道具 举报

回复
论坛徽章:
314
行业板块每日发贴之星
日期:2012-07-12 18:47:29双黄蛋
日期:2011-08-12 17:31:04咸鸭蛋
日期:2011-08-18 15:13:51迷宫蛋
日期:2011-08-18 16:58:25紫蛋头
日期:2011-08-31 10:57:28ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47蜘蛛蛋
日期:2011-10-20 15:51:25迷宫蛋
日期:2011-10-29 11:12:59ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41鲜花蛋
日期:2011-11-09 20:33:30
15#
 楼主| 发表于 2008-4-8 09:48 | 只看该作者
原帖由 joyw 于 2008-4-8 09:29 发表
用sample 行不?

select *  from test sample(10);


ORACLE 的官方文档里,哪个地方介绍了此方法? sample 里的种子,与产生随机数存在什么关系?

使用道具 举报

回复
论坛徽章:
5
生肖徽章2007版:鸡
日期:2008-01-02 17:35:53奥运会纪念徽章:羽毛球
日期:2008-06-23 12:00:05奥运会纪念徽章:柔道
日期:2008-07-04 09:42:36奥运会纪念徽章:皮划艇激流回旋
日期:2008-08-12 14:50:402010新春纪念徽章
日期:2010-03-01 11:19:07
16#
发表于 2008-4-8 09:59 | 只看该作者
原帖由 ZALBB 于 2008-4-8 09:48 发表


ORACLE 的官方文档里,哪个地方介绍了此方法? sample 里的种子,与产生随机数存在什么关系?


在select语句的sample子句里面

sample_clause

SAMPLE [BLOCK] ( sample_percent )

sample_clause
The sample_clause lets you instruct Oracle to select from a random sample of rows from the table, rather than from the entire table.

See Also:
"Selecting a Sample: Examples"


BLOCK
BLOCK instructs Oracle to perform random block sampling instead of random row sampling.

See Also:
Oracle9i Database Concepts for a discussion of the difference


sample_percent
sample_percent is a number specifying the percentage of the total row or block count to be included in the sample. The value must be in the range .000001 to (but not including) 100.

Restrictions on Sampling During Queries
You can specify SAMPLE only in a query that selects from a single table. Joins are not supported. However, you can achieve the same results by using a CREATE TABLE ... AS SELECT query to materialize a sample of an underlying table and then rewrite the original query to refer to the newly created table sample. If you wish, you can write additional queries to materialize samples for other tables.

See Also:
"Selecting a Sample: Examples"


When you specify SAMPLE, Oracle automatically uses cost-based optimization. Rule-based optimization is not supported with this clause.


--------------------------------------------------------------------------------
Caution:
The use of statistically incorrect assumptions when using this feature can lead to incorrect or undesirable results.



Selecting a Sample: Examples
The following query estimates the number of orders in the oe.orders table:

SELECT COUNT(*) * 100 FROM orders SAMPLE (1);


The following example creates a sampled subset of the sample table hr.employees table and then joins the resulting sampled table with departments. This operation circumvents the restriction that you cannot specify the sample_clause in join queries:

CREATE TABLE sample_emp AS
   SELECT employee_id, department_id FROM employees SAMPLE(10);

SELECT e.employee_id FROM sample_emp e, departments d
   WHERE e.department_id = d.department_id
   AND d.department_name = 'Sales';

使用道具 举报

回复
论坛徽章:
314
行业板块每日发贴之星
日期:2012-07-12 18:47:29双黄蛋
日期:2011-08-12 17:31:04咸鸭蛋
日期:2011-08-18 15:13:51迷宫蛋
日期:2011-08-18 16:58:25紫蛋头
日期:2011-08-31 10:57:28ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47蜘蛛蛋
日期:2011-10-20 15:51:25迷宫蛋
日期:2011-10-29 11:12:59ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41鲜花蛋
日期:2011-11-09 20:33:30
17#
 楼主| 发表于 2008-4-8 10:05 | 只看该作者

回复 #16 mihawk 的帖子

收到,多谢!

使用道具 举报

回复
论坛徽章:
30
ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:442012新春纪念徽章
日期:2012-01-04 11:49:54茶鸡蛋
日期:2012-04-19 11:02:10奥运会纪念徽章:棒球
日期:2012-06-19 17:41:27ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07复活蛋
日期:2013-01-15 10:40:032013年新春福章
日期:2013-02-25 14:51:24咸鸭蛋
日期:2013-02-28 10:06:50蛋疼蛋
日期:2013-03-20 13:55:26劳斯莱斯
日期:2013-08-04 11:17:23
18#
发表于 2008-4-8 10:18 | 只看该作者
原帖由 ZALBB 于 2008-4-8 09:48 发表


ORACLE 的官方文档里,哪个地方介绍了此方法? sample 里的种子,与产生随机数存在什么关系?


表中数据采样!  但并不精确取多少条, 只能个大概百分比

使用道具 举报

回复
论坛徽章:
67
现任管理团队成员
日期:2012-06-02 02:10:00ITPUB元老
日期:2012-09-12 14:06:14ITPUB社区千里马徽章
日期:2013-06-09 10:15:34季节之章:冬
日期:2012-09-04 11:05:30季节之章:春
日期:2012-09-05 09:20:36优秀写手
日期:2013-12-18 09:29:09马上有房
日期:2014-04-10 13:35:362014年新春福章
日期:2014-04-14 09:54:08马上有车
日期:2014-02-28 16:43:13马上加薪
日期:2014-02-19 11:55:14
19#
发表于 2008-4-8 11:23 | 只看该作者
哈哈,有点意思

使用道具 举报

回复
论坛徽章:
24
授权会员
日期:2006-12-23 13:14:362008新春纪念徽章
日期:2008-02-13 12:43:03数据库板块每日发贴之星
日期:2008-02-16 01:05:15ITPUB元老
日期:2008-02-19 22:12:27行业板块每日发贴之星
日期:2008-02-29 01:06:00开发板块每日发贴之星
日期:2008-03-03 01:05:35ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41紫蛋头
日期:2012-11-28 16:41:42
20#
发表于 2008-4-8 11:42 | 只看该作者
sample,学习下了.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表