Как ускорить запрос?

-6

select
dt.total_paket,dt.total_paket_selesai,dt.total_pagu,dt.pagu_selesai,dt.penawaran,dt.selisih,dt.selisih_persen,
thn.tahun as tahun_lelang
from (select TO_CHAR((current_date - interval '1 year' * a),'YYYY') AS tahun FROM generate_series(0,4,1) AS s(a)) thn
LEFT JOIN
(select
count(semualelang) as total_paket,
count(selesailelang) as total_paket_selesai,
sum(semualelang.pkt_pagu) as total_pagu,
sum(selesailelang.pagu_selesai) as pagu_selesai,
sum(selesailelang.penawaran) as penawaran,
sum(selesailelang.pagu_selesai) - sum(selesailelang.penawaran) as selisih,
round((sum(selesailelang.pagu_selesai) - sum(selesailelang.penawaran)) / sum(selesailelang.pagu_selesai) * 100::numeric) AS selisih_persen,
date_part('YEAR', semualelang.lls_tgl_setuju) as tahun_lelang
from(
select agen.agc_nama,sk.stk_id,sk.stk_nama,ls.lls_id,ls.pkt_id,sd.sbd_id,sd.sbd_ket,a.ang_tahun,p.pkt_nama,p.pkt_pagu,p.pkt_hps,p.kgr_id,ls.mtd_pemilihan,ls.lls_tgl_setuju from lelang_seleksi ls
left join paket p on ls.pkt_id=p.pkt_id
left join (select pa.pkt_id,pa.ang_id from paket_anggaran pa inner join (select pkt_id,max(auditupdate) as auditupdate from paket_anggaran group by pkt_id) pa1 on pa1.pkt_id=pa.pkt_id and pa1.auditupdate=pa.auditupdate) pa on pa.pkt_id=p.pkt_id
left join satuan_kerja sk on p.stk_id=sk.stk_id
left join agency agen on agen.agc_id=sk.agc_id
left join anggaran a on pa.ang_id=a.ang_id
left join sumber_dana sd on a.sbd_id=sd.sbd_id
where ls.lls_status=1) semualelang
left join (
select e.lls_id,n.psr_id,pemenang.pagu_selesai,pemenang.nev_harga,min(n.nev_harga) as penawaran,pemenang.eva_jenis from evaluasi e inner join nilai_evaluasi n on e.eva_id=n.eva_id
inner join (select n.psr_id,e.eva_id,ls.lls_id, p.pkt_pagu as pagu_selesai, n.nev_harga,e.eva_jenis from lelang_seleksi ls
inner join paket p on ls.pkt_id=p.pkt_id and ls.lls_status=1
inner join (select max(e.eva_versi) as mev, e.lls_id from evaluasi e inner join lelang_seleksi ls on e.lls_id=ls.lls_id where ls.lls_status=1 group by e.lls_id) me on ls.lls_id=me.lls_id
inner join evaluasi e on me.lls_id=e.lls_id AND e.eva_jenis = 4::numeric and e.eva_versi=me.mev
/*inner join workflow w on e.eva_wf_id = w.wf_id AND w.wf_state = 5::numeric */
inner join nilai_evaluasi n on e.eva_id = n.eva_id AND n.nev_lulus = 1::numeric
left join (select ls.lls_id,
CASE
WHEN (select count(*) from jadwal j where now() BETWEEN j.dtj_tglawal and j.dtj_tglakhir and j.lls_id=ls.lls_id) > 0 THEN '0'::numeric
ELSE '1'::numeric
END
AS s_selesai
from lelang_seleksi ls order by ls.lls_id desc) tahapan on tahapan.lls_id=ls.lls_id
where ls.lls_status=1 and tahapan.s_selesai=1) pemenang on e.lls_id=pemenang.lls_id and n.psr_id=pemenang.psr_id
where e.lls_id=pemenang.lls_id and n.psr_id=pemenang.psr_id
group by e.lls_id,n.psr_id,pemenang.pagu_selesai,pemenang.nev_harga,pemenang.eva_jenis
)selesailelang on semualelang.lls_id=selesailelang.lls_id
group by date_part('YEAR', semualelang.lls_tgl_setuju)) dt
on cast(thn.tahun as double precision)=dt.tahun_lelang
ORDER BY thn.tahun ASC

спросил(а) 2015-10-06T06:41:00+03:00 4 года, 2 месяца назад
1
1 ответ
97

вы можете ускорить скорость запроса, как это, с представлением,

обобщите весь свой запрос, чтобы получить некоторый шаблон создать представление базы данных переписать все ваши запросы

т.е. а) сделать это ->

SELECT agen.agc_nama, sk.stk_id, sk.stk_nama, ls.lls_id, ls.pkt_id, sd.sbd_id, sd.sbd_ket, a.ang_tahun, p.pkt_nama, p.pkt_pagu, p.pkt_hps, p.kgr_id, ls.mtd_pemilihan, ls.lls_tgl_setuju FROM lelang_seleksi ls LEFT JOIN paket p ON ls.pkt_id = p.pkt_id LEFT JOIN (SELECT pa.pkt_id, pa.ang_id FROM paket_anggaran pa INNER JOIN (SELECT pkt_id, MAX (auditupdate) AS auditupdate FROM paket_anggaran GROUP BY pkt_id) pa1 ON pa1.pkt_id = pa.pkt_id И pa1.auditupdate = pa.auditupdate) pa ON pa.pkt_id = p.pkt_id LEFT JOIN satuan_kerja sk ON p.stk_id = sk.stk_id LEFT JOIN agency agen ON agen.agc_id = sk.agc_id LEFT JOIN anggaran a ON pa.ang_id = a.ang_id LEFT JOIN sumber_dana sd ON a.sbd_id = sd.sbd_id WHERE ls.lls_status = 1

б) тогда сделайте вид с этим -

SELECT e.lls_id,n.psr_id,pemenang.pagu_selesai,pemenang.nev_harga,MIN(n.nev_harga) AS penawaran,pemenang.eva_jenis FROM evaluasi e INNER JOIN nilai_evaluasi n ON e.eva_id=n.eva_id 
INNER JOIN (SELECT n.psr_id,e.eva_id,ls.lls_id, p.pkt_pagu AS pagu_selesai, n.nev_harga,e.eva_jenis FROM lelang_seleksi ls
INNER JOIN paket p ON ls.pkt_id=p.pkt_id AND ls.lls_status=1
INNER JOIN (SELECT MAX(e.eva_versi) AS mev, e.lls_id FROM evaluasi e INNER JOIN lelang_seleksi ls ON e.lls_id=ls.lls_id WHERE ls.lls_status=1 GROUP BY e.lls_id) me ON ls.lls_id=me.lls_id
INNER JOIN evaluasi e ON me.lls_id=e.lls_id AND e.eva_jenis = 4::NUMERIC AND e.eva_versi=me.mev
/*inner join workflow w on e.eva_wf_id = w.wf_id AND w.wf_state = 5::numeric */
INNER JOIN nilai_evaluasi n ON e.eva_id = n.eva_id AND n.nev_lulus = 1::NUMERIC
LEFT JOIN (SELECT ls.lls_id,
CASE
WHEN (SELECT COUNT(*) FROM jadwal j WHERE NOW() BETWEEN j.dtj_tglawal AND j.dtj_tglakhir AND j.lls_id=ls.lls_id) > 0 THEN '0'::NUMERIC
ELSE '1'::NUMERIC
END
AS s_selesai
FROM lelang_seleksi ls ORDER BY ls.lls_id DESC) tahapan ON tahapan.lls_id=ls.lls_id
WHERE ls.lls_status=1 AND tahapan.s_selesai=1) pemenang ON e.lls_id=pemenang.lls_id AND n.psr_id=pemenang.psr_id
WHERE e.lls_id=pemenang.lls_id AND n.psr_id=pemenang.psr_id
GROUP BY e.lls_id,n.psr_id,pemenang.pagu_selesai,pemenang.nev_harga,pemenang.eva_jenis

c) затем сделайте вид с этим -

SELECT
COUNT(semualelang) AS total_paket,
COUNT(selesailelang) AS total_paket_selesai,
SUM(semualelang.pkt_pagu) AS total_pagu,
SUM(selesailelang.pagu_selesai) AS pagu_selesai,
SUM(selesailelang.penawaran) AS penawaran,
SUM(selesailelang.pagu_selesai) - SUM(selesailelang.penawaran) AS selisih,
ROUND((SUM(selesailelang.pagu_selesai) - SUM(selesailelang.penawaran)) / SUM(selesailelang.pagu_selesai) * 100::NUMERIC) AS selisih_persen,
date_part('YEAR', semualelang.lls_tgl_setuju) AS tahun_lelang
FROM() semualelang
LEFT JOIN ()selesailelang ON semualelang.lls_id=selesailelang.lls_id
GROUP BY date_part('YEAR', semualelang.lls_tgl_setuju)

на самом деле этот shouuld выглядит следующим образом:

SELECT
COUNT(semualelang) AS total_paket,
COUNT(selesailelang) AS total_paket_selesai,
SUM(semualelang.pkt_pagu) AS total_pagu,
SUM(selesailelang.pagu_selesai) AS pagu_selesai,
SUM(selesailelang.penawaran) AS penawaran,
SUM(selesailelang.pagu_selesai) - SUM(selesailelang.penawaran) AS selisih,
ROUND((SUM(selesailelang.pagu_selesai) - SUM(selesailelang.penawaran)) / SUM(selesailelang.pagu_selesai) * 100::NUMERIC) AS selisih_persen,
date_part('YEAR', semualelang.lls_tgl_setuju) AS tahun_lelang
FROM semualelang_view
LEFT JOIN selesailelang_view ON semualelang_view.lls_id=selesailelang_view.lls_id
GROUP BY date_part('YEAR', semualelang_view.lls_tgl_setuju)

ЭТО НЕ ПОДДЕРЖИВАЕТСЯ, ЧТОБЫ ПРАВИЛЬНЫЙ КОД - ВЫ ИЩЕТЕ СОБСТВЕННОЕ СОТРУДНИЧЕСТВО, ЭТО ПРОСТО ПОЛУЧИТЬ КАК ВЫ МОЖЕТЕ ДОСТИГНУТЬ ВАШУ ЦЕЛЬ.

ответил(а) 2015-10-06T07:26:00+03:00 4 года, 2 месяца назад
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема