【发布时间】:2011-03-28 22:25:16
【问题描述】:
我有一个表格查询:
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 的索引根本没有被使用。这是为什么呢?
我在fewplaces 中读到,PG 传统上无法索引 NULL 值。但是,我读过这自 PG 8.3 以来应该有所改变?我目前在 Ubuntu 10.04 上使用 PostgreSQL 8.4。我是否需要专门创建一个“部分”或“功能”索引来加速 IS NOT NULL 查询,还是它已经索引 NULL 而我只是误解了这个问题?
【问题讨论】:
标签: sql database postgresql indexing