gpt4 book ai didi

sql - 索引没有被使用

转载 作者:行者123 更新时间:2023-12-02 06:48:13 25 4
gpt4 key购买 nike

这是 Tom Kyte 的书的摘录。

"We’re using a SELECT COUNT(*) FROM T query (or something similar) and we have a B*Tree index on table T. However, the optimizer is full scanning the table, rather than counting the (much smaller) index entries. In this case, the index is probably on a set of columns that can contain Nulls. Since a totally Null index entry would never be made, the count of rows in the index will not be the count of rows in the table. Here the optimizer is doing the right thing—it would get the wrong answer if it used the index to count rows."

据我所知,当我们使用 WHERE 子句时,索引就会出现。为什么索引会出现在上述场景中?在反击他之前我想了解事实。

最佳答案

"As far as i know indexes comes in picture when you used where clause. "

当我们想要快速访问由索引列的特定值标识的行时,这是索引的一个用例。但还有其他用途。

行数计数为一。要计算表中的行数,Oracle 实际上必须计算每一行(因为统计数据可能不够新鲜),这意味着从字面上读取每个存储 block 并计算每个 block 中的行数。可能会有很多阅读量。

但是,NOT NULL 列上的索引也为表的每一行都有一个条目。索引比表小得多(通常只有一列),因此索引 block 包含的条目比表 block 多得多。因此,Oracle 需要读取比扫描表所需少得多的索引 block 来获取行数。读取较少的 block 比读取更多的 block 更快。

如果表仅在可为空的列上有索引,则情况并非如此。 Oracle 不会为空值建立索引(除非索引是复合索引并且至少填充了一列),因此索引中的条目计数不能保证是表行的实际计数。

读取索引的另一个常见用例是满足 SELECT 语句,其中投影中的所有列都在一个索引中,并且该索引还服务于任何 WHERE 条件。

关于sql - 索引没有被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52135405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com