簡體   English   中英

當在PostgreSQL partitoned表上使用函數時,它會執行全表掃描

[英]when using functions on PostgreSQL partitoned tables it does a full table scan

這些表在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無法正常使用函數和分區表。

有人知道解決這個問題的工作嗎?

問題是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 ...'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM