How to randomly retrieve 20 thousand records from 200 records?

Source: Internet
Author: User
Tags sorts

& Nbsp;
<Table cellspacing = "0" cellpadding = "0">
<Tbody>
<Tr>
<TD id = "postmessage_1459845"> try this statement, select * from (select * from test sample (1) where rownum & lt; 201;
Can be compared with the jianfs method, as follows:
SQL & gt; select count (*) from test; count (*)
----------
20000

1 row selected.

SQL & gt; analyze table test compute statistics;

Table analyzed.

SQL & gt; Set autotrace traceonly
SQL & gt; select * from (select * from test order by dbms_random.random () where rownum & lt; 201;

200 rows selected.

Execution Plan
----------------------------------------------------------
0 SELECT statement optimizer = choose (cost = <span style = "color: red;"> 1850 [/color] card = 20000 byte
(S = 2560000) </span>

1 0 count (stopkey)
2 1 View (cost = 1850 card = 20000 bytes = 2560000)
3 2 sort (order by stopkey) (cost = 1850 card = 20000 bytes = 17
00000)

4 3 Table Access (full) of 'test' (cost = 230 card = 20000 B
Ytes = 1700000)

Statistics
----------------------------------------------------------
<Span style = "color: red;"> 483 [/color] recursive cballs
<Span style = "color: red;"> 82 [/color] db block gets
1657 consistent gets
503 physical reads
<Span style = "color: red;"> 9068 [/color] redo size
28898 bytes sent via SQL * Net to client
1958 bytes encoded ed via SQL * Net From Client
16 SQL * Net roundtrips to/from client
3 sorts (memory)
1 sorts (Disk)
200 rows processed </span>

SQL & gt; select * from (select * from test sample (1) where rownum & lt; 201;

200 rows selected.

Execution Plan
----------------------------------------------------------
0 SELECT statement optimizer = choose (cost = <span style = "color: red;"> 230 [/color] card = 200 bytes = 1
(7000) </span>

1 0 count (stopkey)
2 1 Table Access (sample) of 'test' (cost = 230 card = 200 bytes
= 17000)

Statistics
----------------------------------------------------------
<Span style = "color: red;"> 3 [/color] recursive cballs
<Span style = "color: red;"> 8 [/color] db block gets
<Span style = "color: red;"> 1507 [/color] consistent gets
0 physical reads
<Span style = "color: red;"> 0 [/color] redo size
26488 bytes sent via SQL * Net to client
1937 bytes encoded ed via SQL * Net From Client
16 SQL * Net roundtrips to/from client
2 sorts (memory)
0 sorts (Disk)
200 rows processed </span>

& Nbsp;

& Nbsp; </TD>
</Tr>
</Tbody>
</Table>
Sample is not an analysis function, and 1 in the brackets of a SELECT statement indicates sampling 1%.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.