2015-05-30 53 views
5

我想了解postgresql中的查詢優化,我有一個函數,其中有一些查詢。其中有些是簡單的querys一個用於保存值到一個變量,然後下一個查詢需要這個變量找東西..讓說:postgreSQL解釋分析函數

function()... 
select type into t 
from tableA 
where code = a_c; 

select num into n 
from tableB 
where id = t; 
end function... 

等等..如果我想解釋分析整個功能我執行命令explain analyze select function();這是做到這一點的正確方法,還是我必須解釋分析函數中的每個查詢,並且如果有的話,用什麼值?

+0

如果「解釋分析」每一個人的查詢,您將可以看到,如果一個特定查詢可以優化。不知道它在功能級別上的工作原理,以及它是否合理。 – Ashalynd

+2

你可以把函數體放入一個'PREPARE事物(arg1,arg2,...)AS ...原體...; '並使用'explain analyze execute thing(args)'來調用該函數;' – wildplasser

+0

@wildplasser在body內部聲明語句出現錯誤,如果我不聲明它們,則會出現此錯誤:實用程序語句無法準備 –

回答

4

考慮使用auto_explain模塊:

The auto_explain module provides a means for logging execution plans of slow statements automatically, without having to run EXPLAIN by hand. This is especially helpful for tracking down un-optimized queries in large applications.

auto_explain.log_nested_statements開啓:

auto_explain.log_nested_statements (boolean)

auto_explain.log_nested_statements causes nested statements (statements executed inside a function) to be considered for logging. When it is off, only top-level query plans are logged. This parameter is off by default. Only superusers can change this setting.

+0

我試圖做到這一點,但我得到的輸出是函數返回的結果。我使用的代碼是:LOAD'auto_explain'; SET auto_explain.log_min_duration = 0; SET auto_explain.log_analyze = true; SET auto_explain.log_nested_statements = on; select find_4(); –

+0

由這個模塊完成的'explain'的結果進入服務器的日誌,它不會改變你得到的輸出。 –

+0

不起作用......根本不在日誌中 – Pencilcheck