Oracle中随机抽取N条记录
转载来源:http://www.orafans.org/2006/09/oracle-random-record.html
转载来源:http://kb.cnblogs.com/a/1443619/
一、Oracle取随机数据
1、Oracle访问数据的基本方法:
1)、全表扫描(FulltableScan):执行全表扫描,Oracle读表中的所有记录,考查每一行是否满足WHERE条件。Oracle顺序的读分配给该表的每一个数据块,且每个数据块Oracle只读一次.这样全表扫描能够受益于多块读.
2)、采样表扫描(sampletablescan):扫描返回表中随机采样数据,这种访问方式需要在FROM语句中包含SAMPLE选项或者SAMPLEBLOCK选项.
注:从Oracle8i开始Oracle提供采样表扫描特性
2、使用sample获得随机结果集
2.1、语法:SAMPLE[BLOCK](sample_percent)[SEED(seed_value)]
SAMPLE选项:表示按行采样来执行一个全表扫描,Oracle从表中读取特定百分比的记录,并判断是否满足WHERE子句以返回结果。
BLOCK:表示使用随机块例举而不是随机行例举。
sample_percent:是随机获取一张表中记录的百分比。比如值为10,那就是表中的随机的百分之10的记录。
值必须大于等于.000001,小于100。
SEED:表示从哪条记录返回,类似于预先设定例举结果,因而每次返回的结果都是固定的。该值必须介于0和4294967295之间。
2.2、举例说明
创建测试临时表:- SQL>create table zeeno as select * from dba_objects;
SQL>create table zeeno as select * from dba_objects;
1)、sample(sample_percent):
- -- 从表zeeno中“全表扫描”随机抽取10%的记录,随机查询5条记录
- SQL>select object_name from zeeno sample(10) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- UET$
- VIEW$
- I_SUPEROBJ2
- TRIGGERCOL$
- I_VIEW1
- SQL> /
- OBJECT_NAME
- --------------------------------------------------------------------------------
- I_FILE1
- IND$
- CLU$
- FET$
- I_COBJ#
-- 从表zeeno中“全表扫描”随机抽取10%的记录,随机查询5条记录 SQL>select object_name from zeeno sample(10) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- UET$ VIEW$ I_SUPEROBJ2 TRIGGERCOL$ I_VIEW1 SQL> / OBJECT_NAME -------------------------------------------------------------------------------- I_FILE1 IND$ CLU$ FET$ I_COBJ#
2)、sample block(sample_percent)
- -- 从表zeeno中“采样表扫描”随机抽取10%的记录,随机查询5条记录
- SQL> select object_name from zeeno sample block(10) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- URIFACTORY
- DBMS_XMLGEN
- DBMS_XMLGEN
- DBMS_XMLSTORE
- DBMS_XMLSTORE
-- 从表zeeno中“采样表扫描”随机抽取10%的记录,随机查询5条记录 SQL> select object_name from zeeno sample block(10) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- URIFACTORY DBMS_XMLGEN DBMS_XMLGEN DBMS_XMLSTORE DBMS_XMLSTORE
3)、sample block(sample_percent) seed(seed_value)
- -- 使用seed,返回固定的结果集。从表zeeno中“采样表扫描”随机抽取10%的记录,随机查询5条记录。
- SQL> select object_name from zeeno sample(10) seed(10) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- UET$
- I_CON1
- I_FILE2
- FET$
- I_COL1
- SQL> select object_name from zeeno sample(10) seed(10) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- UET$
- I_CON1
- I_FILE2
- FET$
- I_COL1
-- 使用seed,返回固定的结果集。从表zeeno中“采样表扫描”随机抽取10%的记录,随机查询5条记录。 SQL> select object_name from zeeno sample(10) seed(10) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- UET$ I_CON1 I_FILE2 FET$ I_COL1 SQL> select object_name from zeeno sample(10) seed(10) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- UET$ I_CON1 I_FILE2 FET$ I_COL1
注意以下几点:
1.sample只对单表生效,不能用于表连接和远程表2.sample会使SQL自动使用CBO
3、使用DBMS_RANDOM包 DBMS_RANDOM有两种主要的使用方法分别是:DBMS_RANDOM.VALUE()和DBMS_RANDOM.RANDOM
3.1、取随机数
- SQL> select dbms_random.value() from dual;
- DBMS_RANDOM.VALUE()
- -------------------
- 0.146123095968043
- SQL> select dbms_random.value() from dual;
- DBMS_RANDOM.VALUE()
- -------------------
- 0.90175764902345
SQL> select dbms_random.value() from dual; DBMS_RANDOM.VALUE() ------------------- 0.146123095968043 SQL> select dbms_random.value() from dual; DBMS_RANDOM.VALUE() ------------------- 0.90175764902345
- SQL> select dbms_random.value(1,10) from dual;
- DBMS_RANDOM.VALUE(1,10)
- -----------------------
- 9.86601968210438
- SQL> select dbms_random.value(1,10) from dual;
- DBMS_RANDOM.VALUE(1,10)
- -----------------------
- 3.43475105499398
SQL> select dbms_random.value(1,10) from dual; DBMS_RANDOM.VALUE(1,10) ----------------------- 9.86601968210438 SQL> select dbms_random.value(1,10) from dual; DBMS_RANDOM.VALUE(1,10) ----------------------- 3.43475105499398
3.2、举例说明
- SQL> select * from (select object_name from zeeno order by dbms_random.random) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- /6dd0fe0e_CertificateCertifica
- /cf5224d7_SunJSSE_a4
- KU$_PARSED_ITEMS
- javax/swing/text/IconView
- oracle/xml/jdwp/XSLJDWPString
- SQL> select * from (select object_name from zeeno order by dbms_random.random) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- java/io/ObjectOutputStream$1
- sun/security/krb5/KrbAsReq
- /2d52a21c_Last
- SYS_YOID0000006594$
- /308fbfa1_BeanContextServices
SQL> select * from (select object_name from zeeno order by dbms_random.random) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- /6dd0fe0e_CertificateCertifica /cf5224d7_SunJSSE_a4 KU$_PARSED_ITEMS javax/swing/text/IconView oracle/xml/jdwp/XSLJDWPString SQL> select * from (select object_name from zeeno order by dbms_random.random) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- java/io/ObjectOutputStream$1 sun/security/krb5/KrbAsReq /2d52a21c_Last SYS_YOID0000006594$ /308fbfa1_BeanContextServices
- SQL> select * from (select object_name from zeeno order by trunc(dbms_random.value(1,3))) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- ICOL$
- C_COBJ#
- PROXY_ROLE_DATA$
- I_OBJ#
- UET$
- SQL> select * from (select object_name from zeeno order by trunc(dbms_random.value(1,3))) where rownum<6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- ICOL$
- UNDO$
- I_PROXY_ROLE_DATA$_1
- I_CDEF2
- UET$
SQL> select * from (select object_name from zeeno order by trunc(dbms_random.value(1,3))) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- ICOL$ C_COBJ# PROXY_ROLE_DATA$ I_OBJ# UET$ SQL> select * from (select object_name from zeeno order by trunc(dbms_random.value(1,3))) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- ICOL$ UNDO$ I_PROXY_ROLE_DATA$_1 I_CDEF2 UET$
- SQL> select trunc(dbms_random.value(0, 1000)) randomNum from dual; --(0-1000的整数)
- RANDOMNUM
- ----------
- 790
- SQL> select dbms_random.value(0, 1000) randomNum from dual; --(0-1000的浮点数)
- RANDOMNUM
- ----------
- 997.876726
SQL> select trunc(dbms_random.value(0, 1000)) randomNum from dual; --(0-1000的整数) RANDOMNUM ---------- 790 SQL> select dbms_random.value(0, 1000) randomNum from dual; --(0-1000的浮点数) RANDOMNUM ---------- 997.876726
4、使用内部函数sys_guid()
- SQL> select * from (select OBJECT_NAME from zeeno order by sys_guid()) where rownum < 6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- /6bedadd5_KeyManagerFactory1
- /ffd795c8_AddCRIF
- TABLE_EXPORT_OBJECTS
- /278cd3a4_CGParselet
- KU$_REFCOL_T
- SQL> select * from (select OBJECT_NAME from zeeno order by sys_guid()) where rownum < 6;
- OBJECT_NAME
- --------------------------------------------------------------------------------
- sun/awt/InputMethodSupport
- V_$RESTORE_POINT
- COLORSLIST
- java/util/WeakHashMap$Entry
- DBMSOUTPUT_LINESARRAY
SQL> select * from (select OBJECT_NAME from zeeno order by sys_guid()) where rownum < 6; OBJECT_NAME -------------------------------------------------------------------------------- /6bedadd5_KeyManagerFactory1 /ffd795c8_AddCRIF TABLE_EXPORT_OBJECTS /278cd3a4_CGParselet KU$_REFCOL_T SQL> select * from (select OBJECT_NAME from zeeno order by sys_guid()) where rownum < 6; OBJECT_NAME -------------------------------------------------------------------------------- sun/awt/InputMethodSupport V_$RESTORE_POINT COLORSLIST java/util/WeakHashMap$Entry DBMSOUTPUT_LINESARRAY
注:
在使用sys_guid() 这种方法时,有时会获取到相同的记录,即和前一次查询的结果集是一样的,查找相关资料,有些说是和操作系统有关,在windows平台下正常,获取到的数据是随机的,而在linux等平台下始终是相同不变的数据集,有些说是因为sys_guid()函数本身的问题,即sys_guid()会在查询上生成一个16字节的全局唯一标识符,这个标识符在绝大部分平台上由一个宿主标识符和进程或进程的线程标识符组成,这就是说,它很可能是随机的,但是并不表示一定是百分之百的这样。 所以,为确保在不同的平台每次读取的数据都是随机的,我们大多采用使用sample函数或者DBMS_RANDOM包获得随机结果集,其中使用sample函数更常用,因为其查询时缩小了查询范围,在查询大表,且要提取数据不是很不多的情况下,会对查询速度上有明显的提高。
二、其他数据库随机取出n条记录:
1、SqlServer中随机提取数据库记录
select top n * from 表 order by newid()
--------------------------------------------------------------------------------
selecttop10*fromtablenameorderbyNEWID()
selecttop10*fromtablenameorderbyNEWID()
2、mysql中随机提取数据库记录
Select * From 表 order By rand() Limit n
-------------------------------------------------------------------------------
select*fromtablenameorderbyrand()limit10
select*fromtablenameorderbyrand()limit10
3、Access中随机提取数据库记录Select top n * FROM 表 orDER BY Rnd(id)
-------------------------------------------------------------------------------
SELECTtop10*FROMtablenameORDERBYRnd(FId)
SELECTtop10*FROMtablenameORDERBYRnd(FId)
FId:为你当前表的ID字段名