This is the query to count the rows only
SELECT COUNT(*)
FROM INTERFACES.EAI_DJA_PAGU A
LEFT JOIN INTERFACES.EAI_DJA_SATKER B ON B.KDSATKER = A.KDSATKER
LEFT JOIN INTERFACES.EAI_DJA_UNIT C ON C.KDUNIT = A.KDUNIT
LEFT JOIN INTERFACES.EAI_DJA_PROGRAM D ON D.KDPROGRAM = A.KDPROGRAM
LEFT JOIN INTERFACES.EAI_DJA_KEGIATAN E ON E.KDGIAT = A.KDGIAT
LEFT JOIN INTERFACES.EAI_DJA_OUTPUT F ON F.KDOUTPUT = A.KDOUTPUT
EAI_DJA_PAGU have 3867 rows. EAI_DJA_SATKER have 1000 rows. EAI_DJA_UNIT have 305 rows. EAI_DJA_PROGRAM have 596 rows. EAI_DJA_KEGIATAN have 3280 rows. EAI_DJA_OUTPUT have 5000 rows.
And the query took like 13 minutes and 30 seconds to finish and returns roughly 9 billion of rows.
I have already created indexes for all the joined column as well as the record id as it is auto created on table creation as the primary key. Here are all the indexes create query
CREATE INDEX INTERFACES.EAI_DJA_PAGU_KDSATKER_IDX ON INTERFACES.EAI_DJA_PAGU (KDSATKER);
CREATE INDEX INTERFACES.EAI_DJA_SATKER_KDSATKER_IDX ON INTERFACES.EAI_DJA_SATKER (KDSATKER);
CREATE INDEX INTERFACES.EAI_DJA_UNIT_KDUNIT_IDX ON INTERFACES.EAI_DJA_UNIT (KDUNIT);
CREATE INDEX INTERFACES.EAI_DJA_PROGRAM_KDPROGRAM_IDX ON INTERFACES.EAI_DJA_PROGRAM (KDPROGRAM);
CREATE INDEX INTERFACES.EAI_DJA_KEGIATAN_KDGIAT_IDX ON INTERFACES.EAI_DJA_KEGIATAN (KDGIAT);
CREATE INDEX INTERFACES.EAI_DJA_OUTPUT_KDOUTPUT_IDX ON INTERFACES.EAI_DJA_OUTPUT (KDOUTPUT);
I have also tried to run all these queries
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_PAGU');
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_SATKER');
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_UNIT');
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_PROGRAM');
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_KEGIATAN');
CALL DBMS_STATS.gather_table_stats('INTERFACES', 'EAI_DJA_OUTPUT');
But after all that, the query still took a lot of time to return the row counts. Is there any way out of this situation or optimization that we may do on this query maybe? Or the query just too much??
Here are the execution plan of the query
Predicate Information
------------------------------------------------
3 - access: ("C"."KDUNIT" = "A"."KDUNIT") (0.043)
5 - access: ("D"."KDPROGRAM" = "A"."KDPROGRAM") (0.009)
7 - access: ("E"."KDGIAT" = "A"."KDGIAT") (0.000)
9 - access: ("B"."KDSATKER" = "A"."KDSATKER") (0.001)
13 - access: ("F"."KDOUTPUT" = "A"."KDOUTPUT") (0.014)