我有一个巨大的表(数百万条记录),其中有几百到几千条记录由 bool 字段标记(值 = 1 而不是 0)。
我只关心真实的记录(值 = 1)。有没有办法创建一个只“索引”这些记录的索引?我应该使用什么样的索引?
select count(*)
from records
where boolean_field = 1
环境:Oracle 10g(但我也对其他 dbms 的评论感兴趣)
谢谢!
最佳答案
如果您可以使您的“假”值为空而不是 0,您将获得您想要的结果。
否则,您可以像这样创建一个基于函数的索引:
create index idx on recors (case boolean_field when 1 then 1 end);
这只会索引 1,但要让 Oracle 在您的查询中使用它,您的查询必须像这样:
select * from records where case boolean_field when 1 then 1 end = 1;
关于performance - 仅包含一个值的数据库索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7283258/