如何加快查询速度?

时间:2015-10-06 03:41:15

标签: mysql

from itertools import cycle
def get_pattern(n):
    for e,i in enumerate(cycle((1,2,4,8))):
        val = i*(10**(e//4))
        if val > n: return
        yield val

1 个答案:

答案 0 :(得分:0)

您可以使用view

加快查询速度
  1. 概括所有查询以获得某种模式
  2. 创建数据库视图
  3. 重写所有查询
  4. 即 - a)用这个来创建一个视图 - >

    选择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来自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         在哪里ls.lls_status = 1

  5. b)然后用这个 -

    制作一个视图
    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)
    

    这不是正确的代码 - 你必须找到自己的方式,这只是PSEDUO流程你如何能够实现你的目标。