我第一次遇到query
執行長時間的問題。問題實際上很大,因爲查詢在超過20秒內執行,對於端點用戶來說這是非常明顯的。優化PSQL查詢的執行時間
我有相當大的數據庫topics
(〜8K),主題的有它的參數(這是dictionared - 我有8K主題113個不同的參數)。
我想顯示有關這些主題的重複次數的報告。
topic table:
----------------+---------+-----------------------------------------------------
id | integer | nextval('topic_id_seq'::regclass)
topicengine_id | integer |
description | text |
topicparam_id | integer |
date | date |
topicparam table:
----------------+---------+----------------------------------------------------------
id | integer | nextval('topicparam_id_seq'::regclass)
name | text |
和我的查詢:結果
select distinct tp.id as tpid, tp.name as desc, (select count(*) from topic where topic.topicparam_id = tp.id) as count, t.date
from topicparam tp, topic t where t.topicparam_id =tp.id
Total runtime: 22372.699 ms
片段:
tpid | topicname | count | date
------+---------------------------------------------+-------+---------
3823 | Topic1 | 6 | 2014-03-01
3756 | Topic2 | 14 | 2014-03-01
3803 | Topic3 | 28 | 2014-04-01
3780 | Topic4 | 1373 | 2014-02-01
有什麼辦法,以優化執行時間,這個查詢?
請張貼的輸出'解釋analyze'(或上傳到http://explain.depesz.com)。還有哪些索引是在表格中定義的?你正在使用哪個精確的Postgres版本? –
請閱讀http://stackoverflow.com/tags/postgresql-performance/info,然後適當地編輯您的問題。 –