1
我有這個查詢來獲取給定過濾器的行數,由於db太大而需要很長時間才能得到結果,有沒有什麼辦法可以優化它我使用PostgreSQL 8.2優化在PostgreSql中爲多個分組按字段選擇行數
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))
顯示我們從EXPLAIN分析輸出。 – zero323
另外(1),sum()調用和「order by」的原因是什麼? (2)你爲什麼使用大約兩年前達到報廢時間的版本? http://www.postgresql.org/support/versioning/ –