我有这个查询来获取给定过滤器的行数,由于db太大,需要很长时间才能得到结果,有没有办法优化它我正在使用postgresql 8.2
SELECT COUNT(1) as numrows from ( select lower(column1) as column1, column2, column3, column4, sum(column5) as column5, sum(column6) as column6
from table_name tablename
where column_date >= 'SOME DATE' and column_date < 'SOME DATE'
group by lower(column1) as column1, column2, column3, column4 ORDER BY column5 desc) allRows
以下是我使用EXPLAIN查询
获得的内容XN Aggregate (cost=849751.05..849751.05 rows=1 width=0)
-> XN Subquery Scan allrows (cost=805802.05..842426.22 rows=2929933 width=0)
-> XN HashAggregate (cost=805802.05..813126.89 rows=2929933 width=26)
-> XN Seq Scan on table_name tablename (cost=0.00..512808.79 rows=29299326 width=26)
Filter: ((column_date < 'SOME DATE'::date) AND (column_date >= 'SOME DATE'::date))
答案 0 :(得分:1)
优化性能的最佳方法是升级到PostgreSQL的当前版本,如@Richard already commented。 8.2长期死亡,被遗忘,几乎无法跟上。这也是一种安全风险。
此外,摆脱sum()
计算和ORDER BY
,因为两者都没有对结果做出贡献。实际上,整个SELECT
列表是没用的。
SELECT COUNT(*) AS numrows
FROM (
SELECT 1
FROM table_name
WHERE column_date >= 'SOME DATE'
AND column_date < 'SOME DATE'
GROUP BY lower(column1), column2, column3, column4
) sub
除此之外,您的EXPLAIN输出表明您缺少索引,如:
CREATE INDEX table_name_column_date_idx ON table_name(column_date);