N records are randomly extracted from Oracle. 1. Random data is retrieved from Oracle.
1. Basic methods for accessing data from Oracle: 1) Full table Scan: perform Full table Scan and read all records from the Oracle table, check whether each row meets the WHERE condition. Oracle sequential reads are allocated to each data block of the table, and each data block is read-only once in Oracle. in this way, full table scan can benefit from multiple reads. 2) sample table scan: scan and return random SAMPLE data in the table. This access method must include the SAMPLE option or sample BLOCK option in the FROM statement. www.2cto.com Note: Starting from Oracle8i, Oracle provides the sample table scanning feature 2. Use SAMPLE to obtain a random result set 2.1. Syntax: SAMPLE [BLOCK] (sample_percent) [SEED (seed_value)] sample options: A full table scan is performed by row sampling. Oracle reads a specific percentage of records from the table and determines whether the WHERE clause is satisfied to return results. BLOCK: indicates the use of random BLOCK examples instead of random row examples. Sample_percent: the percentage of records in a random table. For example, if the value is 10, it is a random 10% record in the table. The value must be greater than or equal to. 000001 and less than 100. SEED: indicates the record from which the returned results are similar to pre-set example results, so the results returned each time are fixed. The value must be between 0 and 4294967295. 2.2. Example: create a test temporary table: SQL code SQL> create table zeeno as select * from dba_objects; 1), sample (sample_percent ): SQL code -- 10% of records are randomly extracted from the "full table scan" in the table zeeno, and 5 records are randomly queried. SQL> select object_name from zeeno sample (10) where rownum <6; OBJECT_NAME paiuet $ VIEW $ I _SUPEROBJ2 TRIGGERCOL $ I _VIEW1 SQL & gt;/OBJECT_NAME ----------------------------------- --------------------------------------- I _FILE1 IND $ CLU $ FET $ I _COBJ #2), sample block (sample_percent) SQL code -- 10% of records are randomly extracted from "sample Table scan" in the zeeno table, randomly query five records: SQL> select object_name from zeeno sample block (10) where rownum <6; www.2cto.com OBJECT_NAME using urifac1_dbms_xmlgen DBMS_XMLGEN DBMS_XMLSTORE 3), sample Block (sample_percent) seed (seed_value) SQL code -- use seed to return a fixed result set. 10% of the records are randomly selected from the "Sample Table scan" in the table zeeno, and 5 records are randomly queried. SQL> select object_name from zeeno sample (10) seed (10) where rownum <6; OBJECT_NAME -------------------------------------------------------------------------------- UET $ I _CON1 I _FILE2 FET $ I _COL1 SQL & gt; select object_name from zeeno sample (10) seed (10) where rownum & lt; 6; OBJECT_NAME effecuet $ I _CON1 I _FILE2 FET $ I _COL1 note The following points: 1. sample is only valid for a single table and cannot be used for table join and remote table 2. sample automatically enables SQL to use CBO 3. DBMS_RANDOM package DBMS_RANDOM can be used in two ways: DBMS_RANDOM.VALUE () and DBMS_RANDOM.RANDOM 3.1. The SQL code for random numbers is SQL> select dbms_random.value () from dual; www.2cto.com DBMS_RANDOM.VALUE () ----------------- 0.146123095968043 SQL> select dbms_random.value () from dual; DBMS_RANDOM.VALUE () limit 0.90175764902345 SQL code SQL> select dbms_random.value () 0) from dual; DBMS_RANDOM.VALUE (9.86601968210438) ----------------------- SQL> select dbms_random.value () from dual; DBMS_RANDOM.VALUE) ----------------------- 3.43475105499398 3.2 SQL code> select * from (select object_name from zeeno order by dbms_random.random) where rownum <6; OBJECT_NAME limit/6dd0f Export/export KU $ _ PARSED_ITEMS javax/swing/text/IconView oracle/xml/jdwp/export jdwpstring SQL> select * from (select object_name from zeeno order by dbms_random.random) where rownum <6; OBJECT_NAME initialize java/io/ObjectOutputStream $1 sun/security/krb5/KrbAsReq/2d52a21c_Last sys_yoid1_006594 $/3 08fbfaw.beancontextservices SQL code SQL> select * from (select object_name from zeeno order by trunc (dbms_random.value (1, 3) where rownum <6; OBJECT_NAME policicol $ C_COBJ # region $ I _OBJ # UET $ www.2cto.com SQL> select * from (select object_name from zeeno order by trunc (dbms_random.value (1, 3) where rownum <6; OBJEC T_NAME define ICOL $ UNDO $ I _PROXY_ROLE_DATA $ _ 1 I _CDEF2 UET $ SQL code SQL> select trunc (dbms_random.value (0, 1000) randomNum from dual; -- (integer 0) RANDOMNUM ---------- 790 SQL> select dbms_random.value (0, 1000) randomNum from dual; -- (floating point number 0-997.876726) RANDOMNUM ---------- 4. Use the internal function sys_guid () SQL code SQL> select * from (se Lect OBJECT_NAME from zeeno order by sys_guid () where rownum <6; OBJECT_NAME example/example TABLE_EXPORT_OBJECTS/example KU $ _ REFCOL_T www.2cto.com SQL> select * from (select OBJECT_NAME from zeeno order by sys_guid () where rownum <6; OBJECT_NAME example ------------------------ Using sun/awt/InputMethodSupport V _ $ RESTORE_POINT COLORSLIST java/util/WeakHashMap $ Entry DBMSOUTPUT_LINESARRAY Note: When Using sys_guid (), the same records are sometimes obtained, that is, it is the same as the result set of the previous query. To find the relevant information, it is related to the operating system. Normally on the windows platform, the obtained data is random, in linux and other platforms, the Data Set remains the same. In some cases, the problem is caused by the sys_guid () function itself, that is, sys_guid () A 16-byte globally unique identifier is generated in the query. This identifier is composed of a host identifier and a thread identifier of a process or process on most platforms. That is to say, it is probably random, but it does not mean it must be.. Therefore, to ensure that the data read on different platforms is random, most of us use the sample function or DBMS_RANDOM package to obtain a random result set. The sample function is more commonly used, because the query scope is reduced during the query, the query speed is significantly improved when a large table is queried and the data to be extracted is not very small. 2. n records are randomly retrieved from other databases: 1. database records are randomly extracted from SqlServer.
Select top n * from Table order by newid () distinct select top 10 * from tablename order by NEWID () select top 10 * from tablename order by NEWID () 2. mysql randomly extracts the database record www.2cto.com Select * From Table order By rand () Limit n Limit select * from tablename order by rand () limit 10 select * from tablename order by rand () limit 10 3. Randomly extract database records FROM Access Select top n * from Table orDER BY Rnd (id) explain SELECT top 10 * FROM tablename order by Rnd (FId) SELECT top 10 * FROM tablename order by Rnd (FId) FId: wang286480403, author of the ID field name of your current table