【发布时间】:2011-05-30 09:37:59
【问题描述】:
原始查询如下
SELECT "TIME", "TRADEPRICE"
FROM "YEAR" where "DATE"='2010-03-01'
and "SECURITY"='STW.AX'
AND "TIME" < '10:16:00'
AND "TYPE" = 'TRADE'
ORDER BY "TIME" ASC LIMIT 3
我已经建立了三个索引如下
Columns "DATE" DESC NULLS LAST
Columns "SECURITY" DESC NULLS LAST
Columns "TIME" DESC NULLS LAST
我不索引 TYPE,因为它只取两个可能值之一
解释分析产生以下结果
"Limit (cost=50291.28..50291.28 rows=3 width=16) (actual time=1794484.566..1794484.567 rows=3 loops=1)"
" -> Sort (cost=50291.28..50291.29 rows=4 width=16) (actual time=1794484.562..1794484.563 rows=3 loops=1)"
" Sort Key: "TIME""
" Sort Method: top-N heapsort Memory: 25kB"
" -> Bitmap Heap Scan on "YEAR" (cost=48569.54..50291.24 rows=4 width=16) (actual time=1794411.662..1794484.498 rows=20 loops=1)"
" Recheck Cond: (("SECURITY" = 'STW.AX'::bpchar) AND ("DATE" = '2010-03-01'::date))"
" Filter: (("TIME" < '10:16:00'::time without time zone) AND ("TYPE" = 'TRADE'::bpchar))"
" -> BitmapAnd (cost=48569.54..48569.54 rows=430 width=0) (actual time=1794411.249..1794411.249 rows=0 loops=1)"
" -> Bitmap Index Scan on security_desc (cost=0.00..4722.94 rows=166029 width=0) (actual time=1793917.506..1793917.506 rows=1291933 loops=1)"
" Index Cond: ("SECURITY" = 'STW.AX'::bpchar)"
" -> Bitmap Index Scan on date_desc (cost=0.00..43846.35 rows=2368764 width=0) (actual time=378.698..378.698 rows=2317130 loops=1)"
" Index Cond: ("DATE" = '2010-03-01'::date)"
"Total runtime: 1794485.224 ms"
该数据库大约有 10 亿行运行在 Core2Quad 上,在 Ubuntu 64 位上具有 8gig RAM。这个查询肯定不会花半个小时
【问题讨论】:
-
是这三个独立的索引还是一个与那些列?我会创建一个包含 DATE 和 SECURITY 的索引。 (我对 Postgres 的了解还不够,无法回答这个问题。)
-
可以添加表定义吗?
-
@deltanovember: explain.depesz.com/s/VqC
-
很少需要
TIME上的单个索引。你可以安全地放下它。然后也删除DATE上的索引并在(DATE, TIME)上添加一个索引,这更有可能在其他查询中也有用。 -
这可能需要一段时间,但对表运行 VACUUM ANALYZE 以更新表统计信息。
标签: sql postgresql