当在PostgreSQL分区表中使用函数时,它会执行全表扫描

时间:2012-12-04 14:56:39

标签: sql enterprisedb

表格在PostgreSQL 9数据库中分区。当我运行以下脚本时:

select * from node_channel_summary
where report_date between '2012-11-01' AND '2012-11-02';

它从正确的表中发送数据而不进行全表扫描。但是如果我运行这个脚本:

select * from node_channel_summary
where report_date between trunc(sysdate)-30 AND trunc(sysdate)-29;

在这种情况下,它执行全表扫描,其性能是不可接受的。 -30和-29将被参数替换。

经过一些研究后,Postgres无法正常使用函数和分区表。

是否有人知道解决此问题的工作?

1 个答案:

答案 0 :(得分:1)

问题是PostgreSQL在编译函数时计算并缓存执行计划。这是分区表的问题,因为PostgreSQL使用查询规划器来消除分区。您可以通过将查询指定为字符串来解决此问题,从而强制PostgreSQL在运行时重新解析并重新规划您的查询:

FOR row IN EXECUTE 'select * from node_channel_summary where report_date between trunc(sysdate)-30 AND trunc(sysdate)-29' LOOP
    -- ...
END LOOP;

-- or
RETURN QUERY EXECUTE 'select * from ...'