From Oracle8i Start Oracle Providing Sampling Scan Properties.
Oracle Access Data The basic methods are: 1. Full table Scan 2. Sampling Scan
Full Table Scan Full Table Scan Returns all records in the table. Perform all records in the full table scan, Oracle reading the table, examine whether the WHERE condition is met each line. The Oracle sequence is allocated to each data block of the table, so that the full surface scan can benefit from multiple reads. Each data block Oracle is read once.
Sample Table Scan Sampling Table Scan Return Table Random Sampling Data. This way of access requires the Sample option or the Sample Block option in the FROM statement.
Sample option: When the sample is sampled, the Oracle reads a specific percentage record from the table, and determines whether or not the WHERE clause is met to return the result.
Sample Block Option: When using this option, Oracle reads a specific percentage of Block, check whether the result set meets the WHERE condition to return a record that meets the conditions.
Sample_percent: sample_percent is a number that defines the result set contains a percentage of records accounting for the total number of records. The SAMPLE value should be between [0.000001, 99.99999].
1. Use the Sample option
SQL> SELECT * from Employee Sample (30);
Empno ename Job Mgr Hiredate Sal Comm Deptno
---------- ------------------------------------- -------- --------------------
7369 Smith Clerk 7902 17-DEC-80 800 20
7788 Scott Analyst 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
Execution Plan
-------------------------------------------------- ------------
0 Select Statement Optimizer = Choose (COST = 2 Card = 25 Bytes = 2175)
1 0 Table Access (Sample) of 'Employee' (COST = 2 Card = 25 BYtes = 2175)
Statistics
-------------------------------------------------- ------------
0 Recursive Calls
0 DB Block Get
5 Consistent Gets
0 Physical READS
0 redo size
880 Bytes Sent Via Sql * Net to Client
503 Bytes Received Via SQL * Net from Client
2 SQL * NET ROUNDTRIPS TO / FROM Client
0 Sorts (Memory)
0 Sorts (Disk)
3 Rows Processed
SQL> SELECT * from Employee Sample (20);
Empno ename Job Mgr Hiredate Sal Comm Deptno ------------------------------------ --- ------ ----------------------------
7654 Martin Salesman 7698 28-SEP-81 1250 1400 30
7844 Turner Salesman 7698 08-SEP-81 1500 0 30
Execution Plan
-------------------------------------------------- ------------
0 Select Statement Optimizer = Choose (COST = 2 Card = 16 BYtes = 1392)
1 0 Table Access (Sample) of 'Employee' (COST = 2 Card = 16 BYtes = 1392)
Statistics
-------------------------------------------------- ------------
0 Recursive Calls
0 DB Block Get
5 Consistent Gets
0 Physical READS
0 redo size
839 BYTES SENT VIA SQL * NET to Client
503 Bytes Received Via SQL * Net from Client
2 SQL * NET ROUNDTRIPS TO / FROM Client
0 Sorts (Memory)
0 Sorts (Disk)
2 rows proped
2. Using the Sample Block option
SQL> SELECT * from Employee Sample Block (50);
Empno ename Job Mgr Hiredate Sal Comm Deptno
---------- ------------------------------------- -------- --------------------
7369 Smith Clerk 7902 17-DEC-80 800 20
7499 Allen Salesman 7698 20-Feb-81 1600 300 30
7521 Ward Salesman 7698 22-Feb-81 1250 500 30
7566 Jones Manager 7839 02-APR-81 2975 20
7654 Martin Salesman 7698 28-SEP-81 1250 1400 30
7698 Blake Manager 7839 01-MAY-81 2850 30
7782 Clark Manager 7839 09-JUN-81 2450 10
7788 Scott Analyst 7566 19-APR-87 3000 207839 King President 17-NOV-81 5000 10
7844 Turner Salesman 7698 08-SEP-81 1500 0 30
10 rows selected.
Execution Plan
-------------------------------------------------- ------------
0 Select Statement Optimizer = Choose (COST = 2 Card = 41 BYtes = 3567)
1 0 Table Access (Sample) of 'EMPLOYEE' (COST = 2 Card = 41 bytes = 3567)
Statistics
-------------------------------------------------- ------------
0 Recursive Calls
0 DB Block Get
4 consistent gets
0 Physical READS
0 redo size
1162 BYTES SENT VIA SQL * NET to Client
503 Bytes Received Via SQL * Net from Client
2 SQL * NET ROUNDTRIPS TO / FROM Client
0 Sorts (Memory)
0 Sorts (Disk)
10 Rows Processed
SQL>
3. Query of N records before sampling
You can also use the dbms_random package implementation
SQL> SELECT * FROM
2 SELECT * from Employee
3 Order by dbms_random.Value)
4 WHERE ROWNUM <= 4;
Empno ename Job Mgr Hiredate Sal Comm Deptno
---------- ------------------------------------- -------- --------------------
7654 Martin Salesman 7698 28-SEP-81 1250 1400 30
7839 KING PRESIDENT 17-NOV-81 5000 10
7369 Smith Clerk 7902 17-DEC-80 800 20
7788 Scott Analyst 7566 19-APR-87 3000 20
Execution Plan
-------------------------------------------------- ------------
0 Select Statement Optimizer = Choose
1 0 count (stopkey)
2 1 view
3 2 Sort (Order by Stopkey)
4 3 Table Access (Full) of 'Employee'Statistics
-------------------------------------------------- ------------
0 Recursive Calls
0 DB Block Get
3 Consistent Gets
0 Physical READS
0 redo size
927 Bytes Sent Via Sql * Net to Client
503 Bytes Received Via SQL * Net from Client
2 SQL * NET ROUNDTRIPS TO / FROM Client
1 Sorts (Memory)
0 Sorts (Disk)
4 Rows Processed
Contrast the Sample option
SQL> SELECT * from Employee Sample (40);
Empno ename Job Mgr Hiredate Sal Comm Deptno
---------- ------------------------------------- -------- --------------------
7499 Allen Salesman 7698 20-Feb-81 1600 300 30
7521 Ward Salesman 7698 22-Feb-81 1250 500 30
7698 Blake Manager 7839 01-MAY-81 2850 30
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 Turner Salesman 7698 08-SEP-81 1500 0 30
Execution Plan
-------------------------------------------------- ------------
0 Select Statement Optimizer = Choose (COST = 2 Card = 33 BYtes = 2871)
1 0 Table Access (Sample) of 'Employee' (COST = 2 Card = 33 BYtes = 2871)
Statistics
-------------------------------------------------- ------------
0 Recursive Calls
0 DB Block Get
5 Consistent Gets
0 Physical READS
0 redo size
961 BYTES SENT VIA SQL * NET to Client
503 Bytes Received Via SQL * Net from Client
2 SQL * NET ROUNDTRIPS TO / FROM Client
0 Sorts (Memory)
0 Sorts (Disk)
5 rows proped
SQL>
Mainly pay attention to the following points:
1.Sample only takes effect on a single table, cannot be used for table connections and remote table 2. Sample will automatically use CBOs for SQL
Author: eygle, Oracle technology followers, Oracle technical forum itpub.www.eygle.com from China is the biggest author's personal site you may contact the author by Guoqiang.Gai@gmail.com welcome to explore technical exchanges and links. Exchange. Original article:
http://www.eygle.com/sql/how.to.get.random.Output.Of.Record.Set.htm