postgresql中地球距离和ll到地球的计数非常慢

postgresql中地球距离和ll到地球的计数非常慢,postgresql,gis,Postgresql,Gis,我有一个非常小的“事件”表,只有10703条记录。 以下查询大约需要600毫秒的时间: SELECT count(id) FROM events WHERE event_date > now() AND earth_distance((select position from zips where zip='94121'), ll_to_earth(venue_lat, venue_lon))<16090; 但这并没有改变任何事情。我还有关于事件日期的索引。 下面是解

我有一个非常小的“事件”表,只有10703条记录。 以下查询大约需要600毫秒的时间:

SELECT count(id) 
FROM events 
WHERE event_date > now() 
    AND earth_distance((select position from zips where zip='94121'), ll_to_earth(venue_lat, venue_lon))<16090;
但这并没有改变任何事情。我还有关于事件日期的索引。 下面是解释和分析:

Aggregate  (cost=5400.48..5400.49 rows=1 width=8) (actual time=615.479..615.479 rows=1 loops=1)    InitPlan 1 (returns $0)
     ->  Index Scan using zips_zip_idx on zips  (cost=0.00..8.27 rows=1 width=56) (actual time=0.051..0.056 rows=1 loops=1)
           Index Cond: ((zip)::text = '94121'::text)    ->  Bitmap Heap Scan on events  (cost=144.41..5386.03 rows=2468 width=8) (actual time=16.065..599.613 rows=3347 loops=1)
         Recheck Cond: (event_date > now())
         Filter: (sec_to_gc(cube_distance(($0)::cube, (ll_to_earth((venue_lat)::double precision, (venue_lon)::double precision))::cube)) < 16090::double precision)
         ->  Bitmap Index Scan on events_date_idx  (cost=0.00..143.79 rows=7405 width=0) (actual time=13.523..13.523 rows=7614 loops=1)
               Index Cond: (event_date > now())  Total runtime: 615.663 ms (10 rows)
Aggregate(成本=5400.48..5400.49行=1宽度=8)(实际时间=615.479..615.479行=1循环=1)初始计划1(返回$0)
->在拉链上使用zips_zip_idx进行索引扫描(成本=0.00..8.27行=1宽度=56)(实际时间=0.051..0.056行=1圈=1)
索引条件:((zip)::text='94121'::text)->事件上的位图堆扫描(成本=144.41..5386.03行=2468宽度=8)(实际时间=16.065..599.613行=3347循环=1)
重新检查条件:(事件日期>现在()
过滤器:(秒到秒gc(立方体距离($0)::立方体,(ll到地球((场地高度)::双精度,(场地高度)::双精度))::立方体))<16090::双精度)
->事件\日期\ idx上的位图索引扫描(成本=0.00..143.79行=7405宽度=0)(实际时间=13.523..13.523行=7614循环=1)
索引条件:(event_date>now())总运行时间:615.663毫秒(10行)

还有什么可以加快速度的?

通过添加earth_coord字段并对每行的ll_to_earth进行预计算,可以将速度降低到80ms。我想知道是否有更好的方法或其他我能做的事情。使用“事件日期”上的索引进行测试?@bma是的,我有一个关于事件日期的索引哦,是的,很抱歉,我没有在解释分析输出中看到它。你们有一张臃肿的桌子吗?如果您能在短时间内阻止表中的其他事务,请尝试使用events\u date\u idx发出
集群事件。CLUSTER命令将删除bloat并按照引用索引的顺序重写表。集群完成后,运行
分析事件
Aggregate  (cost=5400.48..5400.49 rows=1 width=8) (actual time=615.479..615.479 rows=1 loops=1)    InitPlan 1 (returns $0)
     ->  Index Scan using zips_zip_idx on zips  (cost=0.00..8.27 rows=1 width=56) (actual time=0.051..0.056 rows=1 loops=1)
           Index Cond: ((zip)::text = '94121'::text)    ->  Bitmap Heap Scan on events  (cost=144.41..5386.03 rows=2468 width=8) (actual time=16.065..599.613 rows=3347 loops=1)
         Recheck Cond: (event_date > now())
         Filter: (sec_to_gc(cube_distance(($0)::cube, (ll_to_earth((venue_lat)::double precision, (venue_lon)::double precision))::cube)) < 16090::double precision)
         ->  Bitmap Index Scan on events_date_idx  (cost=0.00..143.79 rows=7405 width=0) (actual time=13.523..13.523 rows=7614 loops=1)
               Index Cond: (event_date > now())  Total runtime: 615.663 ms (10 rows)