是否可以为数据库中的特定列值生成索引?例如,我有一个名为 status
的列,其值为 {0,1,2,3}
,但最常调用状态 2
所以我想专门索引那个值。这可能吗?我想语法将类似于您可以像这样按降序索引的方式:
create index foo on table(bar desc);
我可能会看到这个工作:
create index foo on table(status 2);
这会在 SQLdeveloper 中针对 Oracle 数据库生成缺少右括号
错误。
最佳答案
基于函数的索引 ...
create index foo on table(case status when 2 then status end)
...和一个谓词...
...
where case status when 2 then status end = 2
... 就是为了这个。
这利用了从 btree 索引中省略空值的优势。如果状态为 2,则 case 表达式返回“2”,否则返回 null,因此索引仅包含状态 =“2”的条目。如您所见,您确实需要一个不同的谓词。坦率地说,其他一些 RDBMS 在这方面做得更好。
关于sql - 针对特定值生成索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958111/