sql limit 10 offset 0:
EXPLAIN (analyze,buffers) SELECT
jdry.c_bh AS rybh,
jcyq.c_bh,
jdry.c_xm,
jdry.c_jdrybm,
jcyq.c_jdsbm,
jcyq.c_bmbm,
jdry.c_fq,
jdry.c_fj
FROM
db_ggfw.T_JDRY jdry,
db_szgl.T_JCYQ jcyq
WHERE
jdry.c_jdrybm = jcyq.c_jdrybm
AND jcyq.c_sfyx <> '0'
AND jcyq.c_dqspjg IS NULL
AND jcyq.c_lx = '01'
AND jcyq.c_dqspzt = '01'
AND jcyq.c_jdsbm = '530104'
ORDER BY
dt_rssj DESC
LIMIT 10 OFFSET 0
Результаты в этом плане выполнения:
Limit (cost=0.84..2211.77 rows=10 width=124) (actual time=800.415..3228.545 rows=3 loops=1)
Buffers: shared hit=729261
-> Nested Loop (cost=0.84..126023.71 rows=570 width=124) (actual time=800.412..3228.534 rows=3 loops=1)
Buffers: shared hit=729261
-> Index Scan using i_T_JDRY_dt_rssj on T_JDRY jdry (cost=0.42..21059.54 rows=150163 width=73) (actual time=0.022..282.385 rows=150068 loops=1)
Buffers: shared hit=147487
-> Index Scan using i_jcyq_rybh on T_JCYQ jcyq (cost=0.42..0.69 rows=1 width=68) (actual time=0.019..0.019 rows=0 loops=150068)
Index Cond: ((c_jdrybm)::text = (jdry.c_jdrybm)::text)
Filter: ((c_dqspjg IS NULL) AND ((c_sfyx)::text <> '0'::text) AND ((c_lx)::text = '01'::text) AND ((c_dqspzt)::text = '01'::text) AND ((c_jdsbm)::text = '530104'::text))
Rows Removed by Filter: 1
Buffers: shared hit=581774
Planning time: 0.896 ms
Execution time: 3228.649 ms
limit 100 offset 0:
EXPLAIN (analyze,buffers) SELECT
jdry.c_bh AS rybh,
jcyq.c_bh,
jdry.c_xm,
jdry.c_jdrybm,
jcyq.c_jdsbm,
jcyq.c_bmbm,
jdry.c_fq,
jdry.c_fj
FROM
db_ggfw.T_JDRY jdry,
db_szgl.T_JCYQ jcyq
WHERE
jdry.c_jdrybm = jcyq.c_jdrybm
AND jcyq.c_sfyx <> '0'
AND jcyq.c_dqspjg IS NULL
AND jcyq.c_lx = '01'
AND jcyq.c_dqspzt = '01'
AND jcyq.c_jdsbm = '530104'
ORDER BY
dt_rssj DESC
**LIMIT 100 OFFSET 0**
Результаты в этомплан выполнения:
Limit (cost=12365.88..12366.13 rows=100 width=124) (actual time=63.142..63.144 rows=3 loops=1)
Buffers: shared hit=8810
-> Sort (cost=12365.88..12367.31 rows=570 width=124) (actual time=63.139..63.139 rows=3 loops=1)
Sort Key: jdry.dt_rssj DESC
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=8810
-> Gather (cost=1000.42..12344.10 rows=570 width=124) (actual time=20.764..63.104 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=8810
-> Nested Loop (cost=0.42..11287.10 rows=238 width=124) (actual time=43.906..57.953 rows=1 loops=3)
Buffers: shared hit=8524
-> Parallel Seq Scan on T_JCYQ jcyq (cost=0.00..9597.27 rows=238 width=68) (actual time=43.869..57.862 rows=1 loops=3)
Filter: ((c_dqspjg IS NULL) AND ((c_sfyx)::text <> '0'::text) AND ((c_lx)::text = '01'::text) AND ((c_dqspzt)::text = '01'::text) AND ((c_jdsbm)::text = '530104'::text))
Rows Removed by Filter: 43409
Buffers: shared hit=8512
-> Index Scan using i_jdry_rybm_unique on T_JDRY jdry (cost=0.42..7.09 rows=1 width=73) (actual time=0.061..0.063 rows=1 loops=3)
Index Cond: ((c_jdrybm)::text = (jcyq.c_jdrybm)::text)
Buffers: shared hit=12
Planning time: 1.127 ms
Execution time: 66.655 ms
смещение 10 пределов 0: T_jdry ИСПОЛЬЗУЕТ индекс i_T_JDRY_dt_rssj.
смещение 100 пределов 0: t_jcyq seq_scan.
План выполнения отличается при использовании пределов 10 и100, а ограничение 10. очень медленное.
SQL также работает быстро, когда я снимаю ограничение.И когда я удаляю индекс i_T_JDRY_dt_rssj, он довольно быстрый.