1

Assume I have a book table defined as follows:

table book(id varchar(50), title varchar(100),type varchar(20),publisher varchar(20), price float)

Suppose an index is defined on ID. Now queries which uses the ID will be improved, like:

select * from book where id='abcd123456'

But will queries that uses other fields be improved as well? For instance, these two queries:

select * from book where title = 'algorithms'
select * from book where price > 25
4

2 回答 2

2

不,不会的。但是,如果一列上有索引,并且查询涉及该列和其他列,则可以使用索引,如果可以的话。

于 2013-07-07T03:51:20.740 回答
2

每个进入 sql 性能调优的人都应该阅读Wolfgang Breitlingi 的基数反馈调优

至于您的问题 - oracle 只会选择索引访问路径,前提是它比访问整个表更便宜。它通过估计每个执行计划的成本来做到这一点。

在您的示例中,如果id是唯一的,则 oracle 需要 1 行 - 索引成本是访问索引头 + 到达叶子(通常是 2 个块读取)+ 表块访问(通常是 1 个块) - 总共 ~ 4 个 CR。执行 FTS(全表扫描)将导致读取表中的所有块。成本是表中的块数除以db_file_multiblock_read_count。如果 FTS 成本大于 4 个 oracle 将执行索引唯一扫描。

关于非唯一谓词的第二个问题 - 如果 oracle 估计price > 15会导致更少的行,他会更喜欢索引访问,否则他会更喜欢 FTS 访问。

这篇文章很好地解释了这一切。

于 2013-07-07T06:32:02.550 回答