oracle数据库中随机抽取记录的方法,在oracle中使用sample获得随机结果集,通过例子学习oracle随机获取n条记录的方法。
采样表扫描(sample table scan):
BLOCK:
SEED:
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 block(10) where rownum<6; OBJECT_NAME -------------------------------------------------------------------------------- URIFACTORY DBMS_XMLGEN DBMS_XMLGEN DBMS_XMLSTORE DBMS_XMLSTORE
-- 使用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
|