限制1慢于限制100

时间:2017-12-12 11:21:34

标签: postgresql limit query-performance

我有一个SQL查询

SELECT * FROM sharescheduledjob WHERE sharescheduledjob.status = 'SCHEDULED' ORDER BY id ASC LIMIT 1

限制1非常慢

Limit  (cost=0.43..46.43 rows=1 width=8) (actual time=3490.958..3490.959 rows=1 loops=1)
  ->  Index Scan using sharescheduledjob_pkey on sharescheduledjob sharesched0_  (cost=0.43..171383.41 rows=3726 width=8) (actual time=3490.956..3490.956 rows=1 loops=1)
        Filter: ((status)::text = 'SCHEDULED'::text)
        Rows Removed by Filter: 6058511
Total runtime: 3490.985 ms

但是限制100非常快

Limit  (cost=248.04..248.29 rows=100 width=8) (actual time=12.968..12.994 rows=100 loops=1)
  ->  Sort  (cost=248.04..257.36 rows=3726 width=8) (actual time=12.966..12.978 rows=100 loops=1)
        Sort Key: id
        Sort Method: top-N heapsort  Memory: 29kB
        ->  Index Scan using sharescheduledjob_status on sharescheduledjob sharesched0_  (cost=0.43..105.64 rows=3726 width=8) (actual time=0.044..8.636 rows=9284 loops=1)
              Index Cond: ((status)::text = 'SCHEDULED'::text)
Total runtime: 13.042 ms

是否可以更改数据库设置以强制首先查找状态?

我已经尝试了

ANALYZE sharescheduledjob

但没有帮助

很难改变查询,因为它是由hqlnate从hql查询生成的

0 个答案:

没有答案