首页 > 数据库 > Oracle > 关于索引取出数据的时间的测试
Tom说 ”从1000行的索引返回数据所用的时间与从一个100000行的索引结构中返回数据的时间是一样的。”
做一个测试吧……
[@more@]SQL> create
table test1 as select * from dba_objects;
表已创建。
SQL> insert into
test1 select * from test1;
已创建68589行。
SQL> insert into
test1 select * from test1;
已创建137178行。
SQL> insert into
test1 select * from test1;
已创建274356行。
SQL> commit;
提交完成。
SQL> create
index ind_test1 on test1(object_id);
索引已创建。
SQL> select
count(*) from test1;
COUNT(*)
----------
548712
我们建立表test1并插入了50多万记录,并为object-id这一列建立索引。
SQL> create
table test2 as select * from dba_objects;
表已创建。
SQL> create
index ind_test2 on test2(object_id);
索引已创建。
SQL> select
count(*) from test2;
COUNT(*)
----------
68590
我们建立表test2并放入6万多条记录,并为object-id这一列建立索引。
SQL> insert into
test1(owner,object_name,object_id) values
('
已创建 1 行。
SQL> commit ;
提交完成。
SQL> insert into
test2(owner,object_name,object_id) values
('
已创建 1 行。
SQL> commit ;
提交完成。
我们分别在test1,test2中插入一行记录。
并观察在test1,test2中查询这一条记录时所使用的时间。
SQL> set
autotrace traceonly
SQL> set timing
on
SQL> select * from test1 where object_id=99999;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value:
3348460575
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows
| Bytes | Cost (%CPU)| T
ime |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | |
1 | 207 | 4
(0)| 0
0:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST1 |
1 | 207 | 4
(0)| 0
0:00:01 |
|* 2 | INDEX RANGE SCAN | IND_TEST1 | 1 |
| 3 (0)| 0
0:00:01 |
-----------------------------------------------------------------------------------------
Predicate
Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=99999)
Note
-----
- dynamic sampling used for this statement
统计信息
----------------------------------------------------------
0
recursive calls
0
db block gets
4
consistent gets
0
physical reads
0
redo size
1276
bytes sent via SQL*Net to client
416
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 *
from test2 where object_id=99999;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value:
4170160382
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows
| Bytes | Cost (%CPU)| T
ime |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | |
1 | 207 | 2
(0)| 0
0:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST2 |
1 | 207 | 2
(0)| 0
0:00:01 |
|* 2 | INDEX RANGE SCAN | IND_TEST2 | 1 |
| 1 (0)| 0
0:00:01 |
-----------------------------------------------------------------------------------------
Predicate
Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=99999)
Note
-----
- dynamic sampling used for this statement
统计信息
----------------------------------------------------------
0
recursive calls
0
db block gets
3
consistent gets
0
physical reads
0
redo size
1276
bytes sent via SQL*Net to client
416
bytes received via SQL*Net from client
2
SQL*Net roundtrips to/from client
0
sorts (memory)
0
sorts (disk)
1
rows processed
我们可以看果然如tom所说时间相同且均为0。
我们还可以注意一下执行计划,分别产生了仅仅几次的IO和物理读,如果我们不使用索引,将产生数千次的IO和物理读,对于生产数据库必然造成巨大压力,可见正确使用索引的重要性。
关于索引的信息可以查看user_indexes/dba_indexes
并且可以通过分析索引,得到索引的结构
analyze index index_name validate structure
select * from index_stats
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21143113/viewspace-1031286/ ,如需转载,请注明出处,否则将追究法律责任。