程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> ORACLESAMPLEblock

ORACLESAMPLEblock

編輯:Oracle教程

最近發現一個有意思的語法,采樣掃描,這種語法適用的場合是那種非常大的表,半天查不出來就可以用,或是一些非重要的統計功能,只想知道一個大概。按采樣掃描的維度不一樣,有兩種語法:

sample 按記錄采樣

sample block 按數據塊采樣

如果不知道什麼是數據塊那你很可能聽不懂。

SQL> drop table test purge;

SQL> create table test as select * from dba_objects;

sample([0.000001,100])

SQL> select count(*) from test;
COUNT(*)
----------
74105
SQL> select count(*) from test sample(10);
COUNT(*)
----------
7434
SQL> select count(*) from test sample(20);
COUNT(*)
----------
14869
SQL> select count(*) from test sample(50);
COUNT(*)
----------
37210
SQL> select count(*) from test sample(80);
COUNT(*)
----------
59505
SQL> select count(*) from test sample(99);
COUNT(*)
----------

73303

sample block([0.000001,100])

SQL> select count(*) from test sample block(10);
COUNT(*)
----------
8830
SQL> select count(*) from test sample block(20);
COUNT(*)
----------
10456
SQL> select count(*) from test sample block(50);
COUNT(*)
----------
47723
SQL> select count(*) from test sample block(80);
COUNT(*)
----------
62941
SQL> select count(*) from test sample block(99);
COUNT(*)
----------

73232

官方文檔:

sample_clause
The sample_clause lets you instruct Oracle to select from a random sample of rows from the table, rather than from the entire table.
BLOCK
BLOCK instructs Oracle to perform random block sampling instead of random row sampling.
sample_percent
sample_percent is a number specifying the percentage of the total row or block count to be included in the sample. The value must be in the range .000001 to (but not including) 100.
Restrictions on Sampling During Queries
You can specify SAMPLE only in a query that selects from a single table. Joins are not supported. However, you can achieve the same results by using a CREATE TABLE ... AS SELECT query to materialize a sample of an underlying table and then rewrite the original query to refer to the newly created table sample. If you wish, you can write additional queries to materialize samples for other tables.
When you specify SAMPLE, Oracle automatically uses cost-based optimization. Rule-based optimization is not supported with this clause.
--------------------------------------------------------------------------------
Caution:
The use of statistically incorrect assumptions when using this feature can lead to incorrect or undesirable results.
--------------------------------------------------------------------------------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved