SELECT COUNT(*) 索引會走 index fast full scan

paulyibinyi發表於2008-03-24

測試如下:不加的會走全表掃描 要是資料量大的話 有可能結果需要很長時間才出來

SQL*Plus: Release 9.2.0.4.0 - Production on Mon Mar 24 10:00:23 2008

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production

SQL> set autot trace exp
SQL> select count(*) from tb_TEST

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT ptimizer=CHOOSE (Cost=22978 Card=1)
   1    0   SORT (AGGREGATE)
   2    1     PARTITION RANGE (ALL)
   3    2       TABLE ACCESS (FULL) OF 'TB_TEST (Co
          st=22978 Card=39590341)

SQL> alter table TB_test

  2    add constraint pk_tb_test1 primary key (ID);

Table altered.

SQL> select count(*) from TB_TEST;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT ptimizer=CHOOSE (Cost=3 Card=1)
   1    0   SORT (AGGREGATE)
   2    1     INDEX (FAST FULL SCAN) OF 'PK_TB_TEST1' (UNIQUE) (Cost=3
           Card=39590341)

查詢結果也很快

SQL> set timing on
SQL> select count(*) from tb_test;

  COUNT(*)
----------
  39590341

Elapsed: 00:00:02.08 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7199859/viewspace-217018/,如需轉載,請註明出處,否則將追究法律責任。

相關文章