You are on page 1of 4

Ing.

Gisela Velazco Tuning de Consultas SQL

Problema 1  USO INCORRECTO DE LA TABLA DRIVING, O AUSENCIA DE LA MISMA.

Situación de error:

Error: PA_MORA_TARDIA_CORPO.P_ASIGNACION_AUTOMATICA –
insertando en TMP_BILL_FIRSTS_CORPO –

ORA-12801: error signaled in parallel query server P047


ORA-01652: unable to extend temp segment by 128 in tablespace TEMPORAL_GR

Consulta sql que genera el error:

Insert /*+ append nologging */


Into Tmp_Bill_Firsts_Corpo
(Tbc_Acc_Id, Tbc_Doc_Document_Date, Tbc_Doc_Balance, Tbc_Module)

Select Tac_Acc_Id, Doc_Document_Date, Doc_Balance, 'MTC'


From (Select /*+ORDERED use_hash(x)*/
Tac_Acc_Id,
Doc_Document_Date,
Nvl(Doc_Balance, 0) Doc_Balance,
Row_Number() Over(Partition By Doc_Acc_Id
Order By Doc_Document_Date) Nrofila
From Tmp_Accounts_Ext_Corpo,
(Select /*+no_merge full(doc1) parallel(doc1,4)*/
Doc_Acc_Id, Doc_Balance, Doc_Document_Date
From Documents Doc1
Where Exists (Select /*+hash_sj */ 1
From Document_Types Dt
Where Doc_Dct_Id = Dct_Id
And Dct_Dct_Id = 'FC')) x
Where x.Doc_Acc_Id = Tac_Acc_Id
And Tac_Module = 'MTC')
Where Nrofila = 1;

Teniendo en cuenta la consulta SELECT (Fuente para el INSERT), se observa que hace una UNION de
una tabla (TMP_ACCOUNTS_EXT_CORPO) y de una subconsulta (SELECT a DOCUMENTS) quedándose
únicamente con las filas que satisfacen la condición x.Doc_Acc_Id = Tac_Acc_Id.

Sin embargo, en la subconsulta a DOCUMENTS, se observa un escaneo completo de la misma,


donde la única restricción es que exista el tipo de documento en DOCUMENT_TYPES.

1
Ing. Gisela Velazco Tuning de Consultas SQL

Echemos un vistazo a los tamaños de las tablas involucradas:

TA
TAMAÑO
BLE SEGMENT_NAME
_MB
OWNER
STL DOCUMENTS 71721
DOCUMENT_TYPES ,15625
TMP_ACCOUNTS_EXT_CO
1
RPO

¡¡¡71721 Mb de DOCUMENTS frente a 1 Mb de TMP_ACCOUNTS_EXT_CORPO!!!

En este punto del análisis, debemos entender la importancia de elegir correctamente a la tabla
DRIVING, la tabla que conduce a la consulta, y que por esta particularidad, debe ser pequeña.

Más aun, teniendo en cuenta la consulta de este ejemplo, donde se recorren casi 71Gb, para luego
quedarnos únicamente con los registros que coinciden en ACC_ID con la tabla TMP.

2
Ing. Gisela Velazco Tuning de Consultas SQL

Sugerencia de tuning
La sugerencia que se presentó, sin modificar demasiado la estructura de la consulta, fue la
siguiente:

INSERT /*+ append nologging */


INTO tmp_bill_firsts_corpo
(tbc_acc_id, tbc_doc_document_date, tbc_doc_balance, tbc_module)

Select Tac_Acc_Id, Doc_Document_Date, Doc_Balance, 'MTC'


From (Select /*+ORDERED use_hash(x)*/
Tac_Acc_Id,
Doc_Document_Date,
Nvl(Doc_Balance, 0) Doc_Balance,
Row_Number() Over(Partition By Doc_Acc_Id
Order By Doc_Document_Date) Nrofila
From Tmp_Accounts_Ext_Corpo,
(Select /*+no_merge full(t) use_nl(t,doc1)
index(doc1,DOC_ACC_I1)*/
Doc_Acc_Id, Doc_Balance, Doc_Document_Date
From Tmp_Accounts_Ext_Corpo t, Documents Doc1
Where Doc1.Doc_Acc_Id = t.Tac_Acc_Id
And t.Tac_Module = 'MTC'
And Exists (Select /*+ hash_sj*/ 1
From Document_Types Dt
Where Doc1.Doc_Dct_Id = Dct_Id
And Dct_Dct_Id = 'FC')) x
Where x.Doc_Acc_Id = Tac_Acc_Id
And Tac_Module = 'MTC')
Where Nrofila = 1;

Cantidad de registros: 44
Tiempo en segundos: 7

Observamos que el cambio fue introducir a la tabla TMP_ACCOUNTS_EXT_CORPO como tabla


DRIVING dentro de la subconsulta a DOCUMENTS, respetando la condición por ACC_ID, y
aprovechando que la tabla DOCUMENTS posee un índice por dicho campo.

La consulta principal, no sufrió cambios, pero ahora debe procesar menos registros como resultado
de la subconsulta.

El tiempo de ejecución, fue drásticamente mejorado.

Y por la cantidad de registros del resultado, notarán el poco sentido de recorrer completamente la
tabla DOCUMENTS.

3
Ing. Gisela Velazco Tuning de Consultas SQL

Anexo: PLANES DE EJECUCIÓN

Consulta SQL original:

PLAN DE EJECUCION
Plan hash value: 1480061565
----------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | PQ Distrib |
----------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT REMOTE | | 44 | 1848 | 136K (21)| 00:18:15 | |
| 1 | PX COORDINATOR | | | | | | |
| 2 | PX SEND QC (RANDOM) | :TQ10003 | 44 | 1848 | 136K (21)| 00:18:15 | QC (RAND) |
|* 3 | VIEW | | 44 | 1848 | 136K (21)| 00:18:15 | |
|* 4 | WINDOW SORT PUSHED RANK | | 44 | 1760 | 136K (21)| 00:18:15 | |
| 5 | PX RECEIVE | | 44 | 1760 | 136K (21)| 00:18:15 | |
| 6 | PX SEND HASH | :TQ10002 | 44 | 1760 | 136K (21)| 00:18:15 | HASH |
|* 7 | WINDOW CHILD PUSHED RANK| | 44 | 1760 | 136K (21)| 00:18:15 | |
|* 8 | HASH JOIN | | 44 | 1760 | 136K (21)| 00:18:15 | |
| 9 | BUFFER SORT | | | | | | |
| 10 | PX RECEIVE | | 12 | 132 | 2 (0)| 00:00:01 | |
| 11 | PX SEND BROADCAST | :TQ10000 | 12 | 132 | 2 (0)| 00:00:01 | BROADCAST |
|* 12 | TABLE ACCESS FULL | TMP_ACCOUNTS_EXT_CORPO | 12 | 132 | 2 (0)| 00:00:01 | |
| 13 | VIEW | | 6840K| 189M| 136K (21)| 00:18:15 | |
|* 14 | HASH JOIN SEMI | | 6840K| 176M| 136K (21)| 00:18:15 | |
| 15 | PX BLOCK ITERATOR | | 430M| 8631M| 79808 (24)| 00:10:39 | |
| 16 | TABLE ACCESS FULL | DOCUMENTS | 430M| 8631M| 79808 (24)| 00:10:39 | |
| 17 | BUFFER SORT | | | | | | |
| 18 | PX RECEIVE | | 1 | 6 | 2 (0)| 00:00:01 | |
| 19 | PX SEND BROADCAST | :TQ10001 | 1 | 6 | 2 (0)| 00:00:01 | BROADCAST |
|* 20 | TABLE ACCESS FULL| DOCUMENT_TYPES | 1 | 6 | 2 (0)| 00:00:01 | |
----------------------------------------------------------------------------------------------------------------------

Consulta SQL con tuning:

PLAN DE EJECUCION
Plan hash value: 1079702637
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Inst |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT REMOTE | | 12 | 504 | 399 (2)| 00:00:04 | |
|* 1 | VIEW | | 12 | 504 | 399 (2)| 00:00:04 | |
|* 2 | WINDOW SORT PUSHED RANK | | 12 | 480 | 399 (2)| 00:00:04 | |
|* 3 | HASH JOIN | | 12 | 480 | 398 (1)| 00:00:04 | |
|* 4 | TABLE ACCESS FULL | TMP_ACCOUNTS_EXT_CORPO | 12 | 132 | 2 (0)| 00:00:01 | PROD |
| 5 | VIEW | | 44 | 1276 | 395 (1)| 00:00:04 | |
|* 6 | HASH JOIN SEMI | | 44 | 1672 | 395 (1)| 00:00:04 | |
| 7 | TABLE ACCESS BY INDEX ROWID| DOCUMENTS | 229 | 4809 | 33 (4)| 00:00:01 | PROD |
| 8 | NESTED LOOPS | | 2802 | 89664 | 393 (1)| 00:00:04 | |
|* 9 | TABLE ACCESS FULL | TMP_ACCOUNTS_EXT_CORPO | 12 | 132 | 2 (0)| 00:00:01 | PROD |
|* 10 | INDEX RANGE SCAN | DOC_ACC_I1 | 229 | | 1 (0)| 00:00:01 | PROD |
|* 11 | TABLE ACCESS FULL | DOCUMENT_TYPES | 1 | 6 | 2 (0)| 00:00:01 | PROD |
--------------------------------------------------------------------------------------------------------------------