--从一个表中随机取两条记录
--1.dbms_random.value
select * from(select * from t_ums_config order by dbms_random.value) where rownum <= 2;
--2.dbms_random.random
select * from(select * from t_ums_config order by dbms_random.random) where rownum <= 2;
--3.sys_guid()
select * from(select * from t_ums_config order by sys_guid()) where rownum <= 2;
--4.sample(20)       按百分比
select * from(select * from t_ums_config sample(20)) where rownum <= 2;
--5.sample block(20) 按数据块
select * from(select * from t_ums_config sample block(40)) where rownum <= 2;
以上从一张表中随机取两条数据的方法有什么区别呢?望高手示下!其中:select * from(select * from t_ums_config sample block(40)) where rownum <= 2; 
经常取不到数据,取到的话,也几乎是一样的! 不知道是什么原因.

解决方案 »

  1.   

    1, 2, 3就不说了。
    4. 按照实际记录行采样,20%,肯定没什么问题。
    5. 按照数据块采样,就不好说了。
    下面是SAMPLE语法的使用示例:
    选择10%的记录
    select * from t1 sample(10)
    选择0.1%的记录
    select * from t1 sample(0.1)
    根据数据块选择1%的记录
    select * from t1 sample block(1)
    使用数据块选择与使用记录行选择的区别:使用数据块选择表示样本的采集是基于数据块采集的,也就是说样本如果一个数据块被采集为样本,则数据块里的记录全部都是样本样本统计是基于统计学采集的,是有概率问题,不一定完全准确,如你要取50%的记录,但实际可能返回给你49%的记录集,也可能返回给你51%的记录集例如
    如果表T1有数据块B1,B2
    B1有记录R1,R2,R3,R4,R5
    B2有记录R6,R7,R8,R9,R10
    如果使用如下SQL选择50%的数据
    select * from t1 sample block(50)
    则返回的结果可能是数据块B1的记录
    R1,R2,R3,R4,R5
    也可能是数据块B2的记录
    R6,R7,R8,R9,R10
    也可能不返回记录集如果使用如下SQL选择50%的数据
    select * from t1 sample (50)
    则返回的结果可能是
    R2,R3,R5,R8,R9
    也可能是如下的样子
    R1,R3,R4,R8应用示例:
    随机从表中取中1条记录,选取记录的概率是1%
    select * from t1 sample(1) where rownum=1
    随机从表中取中10条记录,选取记录的概率是0.1%
    select * from t1 sample(0.1) where rownum<=10
    注:当选取的概率越低,访问表的记录数将越多ORACLE参考手册中的相关说明:
    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.BLOCK
    BLOCK instructs Oracle to perform random block sampling instead of random row sampling.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.
    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.
    --------------------------------------------------------------------------------
     
    译:
    Sample选项
    使用sample选项的意思是指定Oracle从表中随机选择记录样本,这样比从整个表中选择更高效.
    block选项
    加上 BLOCK选项时表示随机取数据块,而不是随机取记录行.
    sample_percent选项
    sample_percent是指定总记录行或数据块为数据样本的百分比数值,这个值只能在0.000001到100之间,且不能等于100
    限制
    只能在单表查询的SQL中指定sample选项,不支持有连接的查询。但是,你可以使用CREATE TABLE ... AS SELECT查询的语法完成同样的效果,然后再采用新建的样本表重新编写查询SQL。
    当你指定用sample时,不支持基于规则(rule)的优化法则,ORACLE自动使用基本成本(cost)的优化法则.
    注意:
    The use of statistically incorrect assumptions when using this feature can lead to incorrect or undesirable results.
    这句话不太理解,估计是说采用样本采集特性可能会产生不准确的结果集。
      

  2.   

    ROWNUM是按照数据ROWID来进行排序的,本身没有规律性,当数据没有发生变化的话,ROWID的排序还是一样的,从实验数据得出这样理解的,
      

  3.   

    1 2 3貌似没什么差别吧,都是随机数,要说差别也是随机数产生原理的差别,不是专门搞安全的话,那东西貌似没什么研究的价值。
    另外rownum并不一定按照rowid来排序,rownun只跟oracle扫描表的方式有关,仅此而已。
      

  4.   

    sample只是一个大致的值,不是一个准确的值,同理按块抽样也是不准确的。