Oracle 何时为空列值建立索引?
我曾经认为当其中一个列值为空时,Oracle 不会索引一行。
一些简单的实验表明情况并非如此。 我能够运行一些查询,意外地只访问索引,即使某些列可以为空(这当然是一个惊喜)。
谷歌搜索导致一些博客的答案相互矛盾:我读到,除非所有索引列都为空,否则行会被索引,并且除非前导列值,否则行会被索引em> 表示索引为空。
那么,什么情况下一行不会进入索引呢? 这个 Oracle 版本是否特定?
I used to think that Oracle does not index a row when one of the column values is null.
Some simple experimentation shows this to be not the case. I was able to run some queries unexpectedly accessing only indexes even though some columns were nullable (which of course was a pleasant surprise).
A Google search led to some blogs with conflicting answers: I have read that a row gets indexed unless all indexed columns are null, and also that a row gets indexed unless the leading column value for the index is null.
So, in what cases does a row not enter an index? Is this Oracle version specific?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了APC的回答之外:当你想索引一个NULL值时,你可以在索引中添加一个常量表达式。
示例:
请注意全表扫描和 364 一致获取。
现在它使用索引并且只有3个一致的获取。
问候,
抢。
And in addition to APC's answer: when you want to index a NULL value, you can add a constant expression to the index.
Example:
Please note the full table scan and the 364 consistent gets.
And now it uses the index and has only 3 consistent gets.
Regards,
Rob.
除了APC的答案之外,NULLS也在位图索引中被索引。
In addition to APC's answer, NULLS are indexed in bitmap indexes.
如果任何索引列包含非空值,则该行将被索引。 正如您在下面的示例中看到的,只有一行没有被索引,并且该行在两个索引列中都有 NULL。 您还可以看到,当前导索引列具有 NULL 值时,Oracle 肯定会对行建立索引。
此示例在 Oracle 11.1.0.6 上运行。 但我非常有信心它适用于所有版本。
If any indexed column contains a non-null value that row will be indexed. As you can see in the following example only one row doesn't get indexed and that's the row which has NULL in both indexed columns. You can also see that Oracle definitely does index a row when the leading index column has a NULL value.
This example run on Oracle 11.1.0.6. But I'm pretty confident it holds true for all versions.