我的表test_table
中的示例数据:
date symbol value created_time
2010-01-09 symbol1 101 3847474847
2010-01-10 symbol1 102 3847474847
2010-01-10 symbol1 102.5 3847475500
2010-01-10 symbol2 204 3847474847
2010-01-11 symbol1 109 3847474847
2010-01-12 symbol1 105 3847474847
2010-01-12 symbol2 206 3847474847
以下是我目前在一个表上使用的查询,该表具有大约3k个唯一符号和大约1,100万行。它仍然需要一段时间(超过80%的查询时间花费在最后的子查询扫描上(我认为是查询结束时的WHERE IN子句。有没有办法加快这部分的速度(想象一下)我在WHERE IN查找中有很多符号。
select date, symbol, value, created_time
from (select *,
rank() over (partition by date, symbol order by created_time desc) as rownum
from test_table) x
where rownum = 1 and symbol in ('symbol1', 'symbol2', 'symbol5', ...)
如果有帮助,下面是EXPLAIN ANALYZE输出。
QUERY PLAN
Subquery Scan on x (cost=281573.35..282473.76 rows=129 width=37) (actual time=2874.389..3037.008 rows=32393 loops=1)
Filter: (x.rownum = 1)
Rows Removed by Filter: 183
-> WindowAgg (cost=281573.35..282152.19 rows=25726 width=37) (actual time=2874.363..2980.848 rows=32576 loops=1)
-> Sort (cost=281573.35..281637.67 rows=25726 width=37) (actual time=2874.340..2901.443 rows=32576 loops=1)
Sort Key: "test_table".date, "test_table".symbol, "test_table".created_time DESC
Sort Method: quicksort Memory: 3314kB
-> Seq Scan on "test_table" (cost=0.00..279688.80 rows=25726 width=37) (actual time=118.338..2693.767 rows=32576 loops=1)
Filter: (symbol = ANY ('{symbol5,symbol8,symbol15,symbol98,symbol43,symbol908}'::text[]))
Rows Removed by Filter: 10649132
Planning time: 0.999 ms
Execution time: 3064.496 ms
答案 0 :(得分:2)
请尝试使用distinct on
:
select distinct on (symbol, date) date, symbol, value, created_time
from test_table
where symbol in ('symbol1', 'symbol2', 'symbol5', ...)
order by symbol, date, created_time desc;
对于此查询,您需要test_table(symbol, date, created_time desc)
上的索引。