2012-12-04 36 views
3

這些表在一​​個PostgreSQL 9數據庫分配。當我運行以下腳本時:上的PostgreSQL使用時功能partitoned表確實全表掃描

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的不正確使用函數和分區表工作。

是否有人知道作爲一個變通來解決這個問題?

+0

您使用的表繼承來實現分區? – 2012-12-04 14:59:58

+2

什麼是'trunc()'和'sysdate'?他們當然不是PostgreSQL的東西... – dezso

+0

我認爲我們實際上是在談論[EnterpriseDB公司(http://enterprisedb.com/solutions/oracle-compatibility-technology):從根本上PostgreSQL的,但無數支持Oracle特性,包括[ 'TRUNC()'和'sysdate'](http://www.enterprisedb.com/docs/en/9.1/oracompat/Postgres_Plus_Advanced_Server_Oracle_Compatibility_Guide-72.htm#P5531_309120)。 – willglynn

回答

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 ...'