2016-11-09 50 views
1

我正在尋找加速查詢(PostgreSql 9.5),但我無法更改它,因爲我無法修改的應用程序執行它。無法修改PostgreSQL查詢,如何加快速度

所以,我捕捉到的查詢在PostgreSQL日誌,那就是:

SELECT Count(*) 
FROM (SELECT ti.idturnosistemaexterno, 
       ti.historiaclinica_hp, 
       p.patientname, 
       CASE 
       WHEN (ti.creationdate :: VARCHAR IS NOT NULL) THEN 
       Date_trunc('SECOND', ti.creationdate) :: VARCHAR 
       ELSE 'NO EXISTE' :: VARCHAR 
       END AS creationdate, 
       CASE 
       WHEN (st.idstudy :: VARCHAR IS NOT NULL) THEN 'SI' :: VARCHAR 
       ELSE 'NO' :: VARCHAR 
       END AS idstudy, 
       st.institutionname, 
       CASE 
       WHEN (st.created_time :: VARCHAR IS NOT NULL) THEN 
       Date_trunc('SECOND', st.created_time) :: VARCHAR 
       ELSE 'NO EXISTE' :: VARCHAR 
       END AS created_time, 
       ti.enviado, 
       st.accessionnumber, 
       st.modality 
     FROM study st 
       right join turnointegracion ti 
         ON st.accessionnumber = ti.idturnosistemaexterno 
       left join patient p 
         ON st.idpatient = p.idpatient 
     ORDER BY ti.creationdate DESC) AS foo; 

解釋分析輸出是這樣的:

                 QUERY PLAN                  
-------------------------------------------------------------------------------------------------------------------------------------------------------- 
Aggregate (cost=231136.16..231136.17 rows=1 width=0) (actual time=32765.883..32765.883 rows=1 loops=1) 
    -> Sort (cost=230150.04..230314.39 rows=65741 width=8) (actual time=32754.992..32761.780 rows=64751 loops=1) 
     Sort Key: ti.creationdate DESC 
     Sort Method: external merge Disk: 1648kB 
     -> Hash Right Join (cost=219856.39..224889.28 rows=65741 width=8) (actual time=26653.007..32714.961 rows=64751 loops=1) 
       Hash Cond: ((st.accessionnumber)::text = (ti.idturnosistemaexterno)::text) 
       -> Seq Scan on study st (cost=0.00..4086.26 rows=77126 width=12) (actual time=12.983..6032.251 rows=77106 loops=1) 
       -> Hash (cost=219048.95..219048.95 rows=64595 width=16) (actual time=26639.722..26639.722 rows=64601 loops=1) 
        Buckets: 65536 Batches: 1 Memory Usage: 3602kB 
        -> Seq Scan on turnointegracion ti (cost=0.00..219048.95 rows=64595 width=16) (actual time=17.259..26611.806 rows=64601 loops=1) 
Planning time: 25.519 ms 
Execution time: 32766.710 ms 
(12 rows) 

下面是相關的表定義:

表「public.turnointegracion」

  Column   |   Type    |        Modifiers        
---------------------------+-----------------------------+-------------------------------------------------------------------- 
idturnosistemaexterno  | character varying(50)  | 
historiaclinica_hp  | integer      | 
matriculaprofrealiza  | character varying(10)  | 
matriculaprofinforma  | character varying(10)  | 
obrasocial    | character varying(20)  | 
planobrasocial   | character varying(20)  | 
nroafiliado    | character varying(20)  | 
nroautorizacion   | character varying(20)  | 
matriculaprofprescribe | character varying(10)  | 
codigonomenclador   | character varying(10)  | 
cantidadcodigonomenclador | integer      | 
importeunitariohonorarios | money      | 
importeunitarioderechos | money      | 
nrodefacturacion   | character varying(15)  | 
informe     | bytea      | 
titulodelestudio   | character varying(250)  | 
fechaprescripcion   | timestamp without time zone | 
fechahora     | timestamp without time zone | 
enviado     | boolean      | not null default false 
enviadofechahora   | timestamp without time zone | 
procesado_hp    | timestamp without time zone | 
modalidad     | character varying(6)  | 
orden      | integer      | not null default nextval('turnointegracion_orden_seq'::regclass) 
idturno     | integer      | not null default nextval('seq_turnointegracion_idturno'::regclass) 
creationdate    | timestamp without time zone | default now() 
informetxt    | text      | 
informedisponible   | timestamp without time zone | 
informeprocesado   | timestamp without time zone | 
Indexes: 
    "turnointegracion_pkey" PRIMARY KEY, btree (idturno) 
    "idx_fechahora" btree (fechahora) 
    "idx_historiaclinicahp" btree (historiaclinica_hp) 
    "idx_idturnosistemaexterno" btree (idturnosistemaexterno) 
    "idx_informedisponible" btree (informedisponible) 
    "idx_turnointegracion_creationdate" btree (creationdate DESC) 
    "idx_turnointegracion_idturnosistext_text" btree ((idturnosistemaexterno::text)) 

表「public.study」

  Column   |   Type    |      Modifiers       
------------------------------+-----------------------------+--------------------------------------------------------- 
idstudy      | integer      | not null default nextval('study_idstudy_seq'::regclass) 
studydate     | date      | 
studytime     | time without time zone  | 
studyid      | character varying(20)  | 
studydescription    | character varying(255)  | 
modality      | character varying(2)  | 
modalityaetitle    | character varying(50)  | 
nameofphysiciansreadingstudy | character varying(255)  | 
accessionnumber    | character varying(20)  | 
performingphysiciansname  | character varying(255)  | 
referringphysiciansname  | character varying(255)  | 
studyinstanceuid    | character varying(255)  | 
status      | status_      | 
institutionname    | character varying(100)  | 
idpatient     | integer      | 
created_time     | timestamp without time zone | 
Indexes: 
    "study_pkey" PRIMARY KEY, btree (idstudy) 
    "study_studyinstanceuid_key" UNIQUE CONSTRAINT, btree (studyinstanceuid) 
    "idx_study_accession_text" btree ((accessionnumber::text)) 
    "idx_study_accessionnumber" btree (accessionnumber) 
    "idx_study_idstudy" btree (idstudy) 
Foreign-key constraints: 
    "study_idpatient_fkey" FOREIGN KEY (idpatient) REFERENCES patient(idpatient) 
Referenced by: 
    TABLE "series" CONSTRAINT "series_idstudy_fkey" FOREIGN KEY (idstudy) REFERENCES study(idstudy) 

正如你所看到的,我已經添加了對受影響的列的索引,但策劃者依然做順序掃描。有沒有辦法改善這一點?

+0

所以你可以修改數據庫,但不是查詢? –

+0

是的,這是一箇舊的可執行文件,我無法訪問源代碼。 – leonardorame

+0

你可以粘貼研究表的模式嗎? – McNets

回答

2

沒有WHERE條件,由於這個連接:

right join turnointegracion ti 
        ON st.accessionnumber = ti.idturnosistemaexterno 

你正在閱讀的所有記錄從turnointegracion,添加索引`creationdate」可以加速排序功能,但同樣所有記錄回。

通過creationdate過濾可以減少最後的時間。

+0

謝謝@mcNets這是正確的答案。 – leonardorame

+0

不用客氣。 – McNets