【发布时间】:2021-12-28 18:36:57
【问题描述】:
我们正在 Oracle 数据库 11g 上进行 SQL 调优/索引。目前在 CustomerTransaction ProductId 上没有索引,所以我们想添加一个来帮助 Join 调优性能。但是,DBA 注意到 CustomerTransaction 中 95% 的 ProductId 为 Null。 Oracle 是否可以选择仅对非空行进行索引?索引的替代方法是什么,或者我们应该在这个 90% 的分布/统计场景中应用索引?
select ct.customerId, pr.ProductName
from dbo.CustomerTransaction ct
inner join dbo.Product pr
on ct.ProductId = pr.ProductId
CREATE TABLE [dbo].[CustomerTransaction](
[CustomerTransactionId] [int] NOT NULL, // this is the primary key
[ProductId] [int] NULL,
[SalesDate] [datetime] NOT NULL,
...
)
ProductId,计数分布 等样本列表
NULL,34065306
2,127444
3,103996
5,96280
6,78247
366,66744
9,58251
4,48056
10,29841
155,27353
8,22143
1052,20885
16,18298
23204,17242
21,16413
26,15084
11,15061
23205,14161
168,14086
7,14022
738,13294
115,12385
13,12119
18,11844
23208,11610
【问题讨论】:
-
你说95%的数据有
ProductId作为null在CustomerTransaction,但是表是用not null为这个列声明的...Oracle不索引行所有索引列中的空值。所以在你的情况下索引肯定会有用 -
如果您在实时模式上频繁运行该查询,那么您就错过了房间里的大象。 astentx 是正确的,该值不能为空。
-
嗨@astentx 我的错,我刚改成不为空
-
现已编辑,但如果您担心性能,您应该关注更明显的问题。
-
明显的问题是什么? @symcbean
标签: sql database oracle indexing oracle11g