Oracle 12c New Feature:
http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT498
1.2 Approximate Count Distinct
近似Count Distinct
新的優化的SQL 函數:
APPROX_COUNT_DISTINCT()
, 提供近似count distinct 集合函數。處理大量的資料時,它顯著快于精确集合方法,尤其對于不同值數量比較大的資料集合,處理結果僅僅會相比精确值帶有微不足道的偏離。
在當下的資料分析中,統計資料集合中的不同值個數是常見的。憑借數量級(orders of magnitude ),當提供幾乎精确的結果,加速任何已經存在的處理過程并且提供新的洞察力分析水準,優化處理時間和資源使用。
Approximate Count Distinct SQL Detail:
Note: The
APPROX_COUNT_DISTINCT
function is available starting with Oracle Database 12c Release 1 (12.1.0.2).
文法:
APPROX_COUNT_DISTINCT
(expr)
說明:
APPROX_COUNT_DISTINCT
傳回包括不同expr資料的行數(傳回資料類型)。
這個函數提供近似
COUNT
(DISTINCT
expr
)
功能, COUNT (DISTINCT expr) 傳回包含不同值得expr的精确行數。
APPROX_COUNT_DISTINCT
處理大量資料時明顯快于
COUNT
, 雖然帶有微不足道的偏差相比精确值(至于這個偏差是多大,還有待進一步實驗驗證,總之效率高是一個特色)。
順便說一句,對于 這個 expr,除了
BFILE
,
BLOB
,
CLOB
,
LONG
,
LONG
RAW
, or
NCLOB
這些資料類型,可以指定具有任何資料量級别的一列。
APPROX_COUNT_DISTINCT
忽略帶有空值(null)行, 傳回資料類型(
NUMBER)
.
COUNT (DISTINCT expr) 也是忽略null值所在行。
舉個栗子:
SELECT APPROX_COUNT_DISTINCT(manager_id) AS "Active Managers"
FROM employees;
還可以帶group by,order by
SELECT prod_id, APPROX_COUNT_DISTINCT(cust_id) AS "Number of Customers"
FROM sales
GROUP BY prod_id
ORDER BY prod_id;