2013-06-04 25 views
1

我需要對一個非常大的表中的一組行進行一些非常複雜的操作。postgresql限制視圖的位置並將結果發送到函數?

看起來好像是在一個函數中完成的操作(基於JOIN,UNION &幾個ORDER BY)的「回填」空屬性。爲了最小化我在開始時需要WHERE子句的行數。

因爲所有這些都會與Rails對話,所以外層需要是VIEW。

減回填我有什麼是

CREATE OR REPLACE VIEW testing.for_sale_layouts_view 
AS SELECT * from bestforsalelayout((select address_id from public.for_sale_layouts limit 1)); 
; 

CREATE OR REPLACE FUNCTION bestforsalelayout(addr_id int) 
RETURNS public.for_sale_layouts 
AS $$ 
    select * from public.for_sale_layouts where address_id = addr_id; 
$$ 
LANGUAGE SQL IMMUTABLE; 

調用像

select * from testing.for_sale_layouts_view where address_id = <addr_id>; 

這類作品,但如果有一個以上的返回行for_sale_layouts函數不返回任何內容,單身行工作正常。

我不知所措,指針在工作方向將不勝感激。

回答

1

如果要返回多行,則需要該函數的return table形式。

查看文檔here

CREATE OR REPLACE FUNCTION bestforsalelayout(addr_id int) 
RETURNS table(for_sale_layouts int) 
AS $$ 
    select for_sale_layouts from public.for_sale_layouts where address_id = addr_id; 
$$ 
+0

該函數只會返回一行。問題似乎是,如果視圖中的子查詢包含多行,那麼只需要將address_id傳遞給該函數即可。 –