我第一次遇到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,然后适当地编辑您的问题。 –