Tudo bem?
Gostaria de tirar uma dúvida com vocês:
É mais performático, dar um
COUNT(1)
, do que um COUNT(*)
?Caso positivo, porque? porque o count(1) busca a conta através da PK, automaticamante?
Muito obrigado.
COUNT(1)
, do que um COUNT(*)
?Pelo que percebi, nas versões atuais, não temos mais essa diferença.
Counting records
--------------------------------------------------------------------------------
Over the years, there have been many debates regarding what is the optimal way to count a selection of records from a table. Without going into the large history of this topic, different versions of Oracle mandated different approaches to best counting records.
However, in the more recent releases of Oracle, the fast full index scan (bitmap especially) has generally become the most prudent way to count the records. Thankfully, under the cost optimiser, Oracle now performs this for most of combinations of "count(*)" that DBA's have advocated in the past, thus any of them will perform equivalently as can be seen from the examples below
Example with indexed table
Example with unindexed tableSQL> select count(*) from PURCHASED_VEHICLES; COUNT(*) ---------- 283761 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=247 Card=1) 1 0 SORT (AGGREGATE) 2 1 INDEX (FAST FULL SCAN) OF 'PUVE_VEFE_MODEL_I' (NON-UNIQU E) (Cost=247 Card=283761) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 2583 consistent gets 4 physical reads 0 redo size 369 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select count(1) from PURCHASED_VEHICLES; COUNT(1) ---------- 283761 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=247 Card=1) 1 0 SORT (AGGREGATE) 2 1 INDEX (FAST FULL SCAN) OF 'PUVE_VEFE_MODEL_I' (NON-UNIQU E) (Cost=247 Card=283761) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 2583 consistent gets 4 physical reads 0 redo size 369 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select count(rowid) from PURCHASED_VEHICLES; COUNT(ROWID) ------------ 283761 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=247 Card=1 Bytes=7) 1 0 SORT (AGGREGATE) 2 1 INDEX (FAST FULL SCAN) OF 'PUVE_VEFE_MODEL_I' (NON-UNIQU E) (Cost=247 Card=283761 Bytes=1986327) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 2583 consistent gets 3 physical reads 0 redo size 373 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select count(id) from PURCHASED_VEHICLES; -- indexed col COUNT(ID) --------- 283761 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=247 Card=1 Bytes=7) 1 0 SORT (AGGREGATE) 2 1 INDEX (FAST FULL SCAN) OF 'PUVE_VEFE_MODEL_I' (NON-UNIQU E) (Cost=247 Card=283761 Bytes=1986327) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 2583 consistent gets 3 physical reads 0 redo size 373 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
SQL> select count(*) from PURCHASED_VEHICLES; COUNT(*) ---------- 283761 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=11952 Card=1) 1 0 SORT (AGGREGATE) 2 1 TABLE ACCESS (FULL) OF 'PURCHASED_VEHICLES_JN' (Cost=119 52 Card=459871) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 126734 consistent gets 126569 physical reads 0 redo size 369 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select count(1) from PURCHASED_VEHICLES_JN; COUNT(1) ---------- 478957 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=11952 Card=1) 1 0 SORT (AGGREGATE) 2 1 TABLE ACCESS (FULL) OF 'PURCHASED_VEHICLES_JN' (Cost=119 52 Card=459871) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 126734 consistent gets 126569 physical reads 0 redo size 369 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed SQL> select count(rowid) from PURCHASED_VEHICLES_JN; COUNT(ROWID) -------------- 478957 Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=11952 Card=1) 1 0 SORT (AGGREGATE) 2 1 TABLE ACCESS (FULL) OF 'PURCHASED_VEHICLES_JN' (Cost=119 52 Card=459871) Statistics ---------------------------------------------------------- 0 recursive calls 4 db block gets 126734 consistent gets 126569 physical reads 0 redo size 369 bytes sent via SQL*Net to client 424 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
Usuários navegando neste fórum: Nenhum usuário registrado e 18 visitantes