Oracle count(*)是否走索引

來源:互聯網
上載者:User

count(*)在平常工作中,使用到的頻率很高,是否會走索引,對效能影響不小!但是不是所有的count(*)都能走索引!小記下

create table t3

(

sid number not null primary key,

sno number,

sname varchar2(10)

)

tablespace test;

declare

maxrecords constant int:=100000;

i int :=1;

begin

for i in 1..maxrecords loop

insert into t3 values(i,i,'ocpyang');

end loop;

dbms_output.put_line(' 成功錄入資料! ');

commit;

end;

/

declare

maxrecords constant int:=200000;

i int :=100001;

begin

for i in 100001..maxrecords loop

insert into t3(sid,sname) values(i,'ocpyang');

end loop;

dbms_output.put_line(' 成功錄入資料! ');

commit;

end;

/

create index index_sno on t3(sno);

exec dbms_stats.gather_table_stats('SYS','T3',cascade=>TRUE);

***********

1.count

***********

SQL> set autotrace traceonly explain stat;

SQL> select count(*) from t3;

執行計畫

----------------------------------------------------------

Plan hash value: 463314188

-------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU)| Time |

-------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | | |

| 2 | TABLE ACCESS FULL| T3 | 82 | 2 (0)| 00:00:01 |

-------------------------------------------------------------------

Note

-----

- SQL plan baseline "SQL_PLAN_27gnhfjz9qahj14fae16c" used for this statement

統計資訊

----------------------------------------------------------

55 recursive calls

38 db block gets

521 consistent gets

19 physical reads

14676 redo size

527 bytes sent via SQL*Net to client

520 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 t1 where sid is not null;

執行計畫

----------------------------------------------------------

Plan hash value: 1551730033

--------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

--------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 13 | 68 (0)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | 13 | | |

| 2 | INDEX FAST FULL SCAN| SYS_C0023596 | 85899 | 1090K| 68 (0)| 00:00:01 |

--------------------------------------------------------------------------------------

Note

-----

- dynamic sampling used for this statement (level=2)

- SQL plan baseline "SQL_PLAN_4xztry6akgpqqf2d247c8" used for this statement

統計資訊

----------------------------------------------------------

4 recursive calls

0 db block gets

310 consistent gets

0 physical reads

0 redo size

527 bytes sent via SQL*Net to client

520 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1 rows processed

--通過索引實現的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.