Subject How I can speed up this query
Author Luigi Siciliano
Hallo,

I have this query

SELECT
  ID,
  DENOMINAZIONE,
  SCADENZA,
  PARTITA,
  NUMERO_DOCUMENTO,
  DATA_DOCUMENTO,
  IMPORTO,
  IMPORTO - PAGATO AS RESIDUO,
  PAGATO,
  PAGAMENTO,
  SALDARE
from
(
SELECT
  C.ID,
  C.DENOMINAZIONE,
  PNS.SCADENZA,
  PNS.PARTITA,
  PNT.NUMERO_DOCUMENTO,
  PNT.DATA_DOCUMENTO,
  PNS.IMPORTO,
  COALESCE((SELECT
                         SUM(PNC2.AVERE - PNC2.DARE)
                       from
                         PN_CORPO PNC2
                       WHERE
                         PNC2.PARTITA = PNS.PARTITA
                         AND PNC2.SCADENZA = PNS.SCADENZA
                         AND PNC2.SCADENZA IS NOT NULL
                      ), 0) AS PAGATO,
  0.00 AS PAGAMENTO,
  '0' AS SALDARE
from
  PN_SCADENZE PNS,
  PN_CORPO PNC,
  CLIENTI C,
  PN_TESTA PNT
where
  PNS.PN_TESTA_ID = PNC.PN_TESTA_ID
  AND C.ID = PNC.CLIENTE_ID
  AND PNT.ID = PNS.PN_TESTA_ID
)

From Statistics of Flamerobin:

Prepare time: 0.014s
Field #01: . Alias:ID Type:SMALLINT
Field #02: . Alias:DENOMINAZIONE Type:STRING(60)
Field #03: . Alias:SCADENZA Type:DATE
Field #04: . Alias:PARTITA Type:INTEGER
Field #05: . Alias:NUMERO_DOCUMENTO Type:STRING(20)
Field #06: . Alias:DATA_DOCUMENTO Type:DATE
Field #07: . Alias:IMPORTO Type:NUMERIC(18,4)
Field #08: . Alias:RESIDUO Type:NUMERIC(18,4)
Field #09: . Alias:PAGATO Type:NUMERIC(18,4)
Field #10: . Alias:PAGAMENTO Type:NUMERIC(18,2)
Field #11: . Alias:SALDARE Type:STRING(1)
PLAN (PNC2 NATURAL)
PLAN JOIN (PNC NATURAL, PNT INDEX (PN_TESTA_PK), C INDEX (PK_CLIENTI),
PNS INDEX (PN_SCADENZE_FK))

Executing...
Done.
4684158 fetches, 0 marks, 0 reads, 0 writes.
0 inserts, 0 updates, 0 deletes, 3011 index, 2313903 seq.
Delta memory: 70560 bytes.
Total execution time: 1.608s
Script execution finished

How I can speed up it?
The time 1.608s if referred at only 300 rows fetched as defaults of
flamerobin. In production I have 1157 rows affected (they grows rapidly)
in near 6.20s.

Thanks

--

Luigi Siciliano
--------------------------