索引PostgreSQL中的空值

我有一个查询的形式:
select m.id from mytable m
left outer join othertable o on o.m_id = m.id
    and o.col1 is not null and o.col2 is not null and o.col3 is not null
where o.id is null

该查询返回几百条记录,尽管这些表具有数百万行,并且将永远运行(大约一个小时)。

当我使用以下命令检查我的索引统计信息时:

select * from pg_stat_all_indexes
where schemaname <> 'pg_catalog' and (indexrelname like 'othertable_%' or indexrelname like 'mytable_%')

我看到只有othertable.m_id的索引被使用,并且col1..3的索引根本没有被使用。为什么是这样?

我已经阅读了few places,PG一直不能为NULL值索引。但是,从PG 8.3开始,我已经看过这个改变了?我目前在Ubuntu 10.04上使用PostgreSQL 8.4。我是否需要专门制作“部分”或“功能”索引来加快IS NOT NULL查询,或者是否已经对NULL建立索引,我只是误解了这个问题?

你可以尝试部分索引:
CREATE INDEX idx_partial ON othertable (m_id)
WHERE (col1 is not null and col2 is not null and col3 is not null);

从文档:http://www.postgresql.org/docs/current/interactive/indexes-partial.html

相关文章

文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,...
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb...
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才...
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation...
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgr...
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解...