单列表select count(*) / 索引 等效率

测试,当一个表中,只有一个列的时候,查询count(*), 使用和不使用索引的时候,那个效率高一些?

-- 结论,使用索引反而会效率低一些。因为索引中不仅存在的有列的值,还有rowid,索引段相对来说大一些,所以走索引效率低。IO大 

测试用表

ZBB@test>desc t11
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 OBJECT_ID                                 NOT NULL NUMBER

ZBB@test>

不使用索引的i情况下,执行计划

ZBB@test>select count(*) from t11;

  COUNT(*)
----------
     87357


Execution Plan
----------------------------------------------------------
Plan hash value: 4132580237

-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    41   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |
|   2 |   TABLE ACCESS FULL| T11  | 84680 |    41   (0)| 00:00:01 |
-------------------------------------------------------------------

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


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        137  consistent gets
          0  physical reads
          0  redo size
        528  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

ZBB@test>

修改该列为not null, 否则不会走索引。

alter table t11 modify object_id not null; -- 不然一直都不会走索引

查看走索引的情况下的执行计划。走了索引,效率反而不高。

select /*+index(t11 idx_t11) */ count(*) from t11;
ZBB@test>select /*+index(t11 idx_t11) */ count(*) from t11;

  COUNT(*)
----------
     87357


Execution Plan
----------------------------------------------------------
Plan hash value: 4270730777

--------------------------------------------------------------------
| Id  | Operation        | Name    | Rows  | Cost (%CPU)| Time     |
--------------------------------------------------------------------
|   0 | SELECT STATEMENT |         |     1 |   209   (1)| 00:00:01 |
|   1 |  SORT AGGREGATE  |         |     1 |            |          |
|   2 |   INDEX FULL SCAN| IDX_T11 | 84680 |   209   (1)| 00:00:01 |
--------------------------------------------------------------------

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


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        195  consistent gets
          0  physical reads
          0  redo size
        528  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

ZBB@test>
END

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/80980078