[英]PostgreSQL: sequential table scan on Hstore column despite having a GiN index
I have a table with a hstore
column and roughly 22 mio records (the ways table from a partial osm-database). 我有一个带有hstore
列和大约22个mio记录的表(来自部分osm数据库的ways表)。
Despite having a GIN index on the hstore column, queries for a specific tag result in a sequential table scan that takes > 60 sec to return a single column. 尽管hstore列上有GIN索引,但查询特定标签会导致顺序表扫描 ,该顺序表扫描需要60秒以上的时间才能返回单个列。
What i have been doing so far. 到目前为止我一直在做什么。
vacuum analayze
执行vacuum analayze
select id from table where tags->'name'='foo'
执行这种查询: select id from table where tags->'name'='foo'
analyze
on the table. [编辑]根据用户a_horse_with_no_name的建议,我通过在表上执行analyze
来更新表统计信息。 But that had no effect. 但这没有效果。 You can see the query plan here . 您可以在此处查看查询计划。 For some reason the explain analyze
takes only ~20 sec to complete. 由于某种原因, explain analyze
只需要约20秒即可完成。
How can I properly index a hstore column on a large table like this, to reduce query execution cost significantly? 我如何在这样的大表上正确索引hstore列,以显着降低查询执行成本?
Thank you for your help! 谢谢您的帮助!
I see two possible solutions: 我看到两种可能的解决方案:
If you always query that key value for equality you can use an a B-Tree index on the expression (`tags -> 'name') 如果您始终查询该键值是否相等,则可以在表达式上使用B树索引(`标签->'名称')
create index idx_name on ways ( (tags -> 'name') );
A quick test has shown that Postgres does use the index to find if a key value is present in the hstore column, but apparently not for finding the associated value. 快速测试表明,Postgres确实使用索引来查找hstore列中是否存在键值,但显然不是用于查找关联的值。
So you could try to add a condition to test for that key value as well: 因此,您也可以尝试添加条件来测试该键值:
select id
from ways
where tags ? 'name'
and tags -> 'name' = 'Wiehbergpark';
If all rows contain that key, it might not help though. 如果所有行都包含该键,则可能无济于事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.