加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

索引PostgreSQL中的空值

发布时间:2020-12-13 16:39:02 所属栏目:百科 来源:网络整理
导读:我有一个查询的形式: select m.id from mytable mleft 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 nullwhere o.id is null 该查询返回几百条记录,尽管这些表具有数百万行,并且将永远运
我有一个查询的形式:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读