Custo de Execução de Queries
- Oracle –
(versão 2015)
Carlos Pampulim Caldeira
http://www.di.uevora.pt/~ccaldeira
http://www.linkedin.com/in/carlospampulimcaldeira
Administração de Bases de Dados
O que fazer?
1. SQL> DESC plan_table;
2. Correr o script utlxplan.sql
Plan_table
Explain plan
EXPLAIN PLAN SET STATEMENT_ID =
'DEPARTAMENTO' FOR
SELECT * FROM "Departamento";
Plan set succeeded.
Script para ler plan_table
UNDEFINE v_statement_id;
SELECT
id ||
DECODE(id, 0, '', LPAD(' ', 2*(level - 1))) || ' ' ||
operation || ' ' ||
options || ' ' ||
object_name || ' ' ||
object_type || ' ' ||
DECODE(cost, NULL, '', 'Cost = ' || position)
AS “Plano de Execução”
FROM plan_table
CONNECT BY PRIOR id = parent_id
AND statement_id = '&&v_statement_id'
START WITH id = 0
AND statement_id = '&v_statement_id';
Ordem das operações
A ordem das operações é a seguinte:
• A mais chegada à direita é a primeira a ser
executada, seguida pelas operações-pai;
• Nas operações com mesma posição, a
primeira a ser executada é a de topo, seguida
pelas operações-pai.
Ordem das operações
0 SELECT STATEMENT Cost = 6
1 MERGE JOIN Cost = 1
2 TABLE ACCESS BY INDEX ROWID TIPOS TABLE Cost = 1
3 INDEX FULL SCAN TIPOS_CP INDEX (UNIQUE) Cost = 1
4 SORT JOIN Cost = 2
5 TABLE ACCESS FULL PRODUTOS TABLE Cost = 1
Ordem de execução: 3, 2, 5, 4, 1 e 0.
Comparação de planos de execução
0 SELECT STATEMENT Cost = 4
………
versus
0 SELECT STATEMENT Cost = 5
………
Sumário
• O tuning é o mecanismo que permite
aumentar a performance do SQL;
• O optimizador relacional é um subsistema de
AI que gera planos de execução.

Custo Execução Queries | Oracle | 2015

  • 1.
    Custo de Execuçãode Queries - Oracle – (versão 2015) Carlos Pampulim Caldeira http://www.di.uevora.pt/~ccaldeira http://www.linkedin.com/in/carlospampulimcaldeira Administração de Bases de Dados
  • 2.
    O que fazer? 1.SQL> DESC plan_table; 2. Correr o script utlxplan.sql
  • 3.
  • 4.
    Explain plan EXPLAIN PLANSET STATEMENT_ID = 'DEPARTAMENTO' FOR SELECT * FROM "Departamento"; Plan set succeeded.
  • 5.
    Script para lerplan_table UNDEFINE v_statement_id; SELECT id || DECODE(id, 0, '', LPAD(' ', 2*(level - 1))) || ' ' || operation || ' ' || options || ' ' || object_name || ' ' || object_type || ' ' || DECODE(cost, NULL, '', 'Cost = ' || position) AS “Plano de Execução” FROM plan_table CONNECT BY PRIOR id = parent_id AND statement_id = '&&v_statement_id' START WITH id = 0 AND statement_id = '&v_statement_id';
  • 6.
    Ordem das operações Aordem das operações é a seguinte: • A mais chegada à direita é a primeira a ser executada, seguida pelas operações-pai; • Nas operações com mesma posição, a primeira a ser executada é a de topo, seguida pelas operações-pai.
  • 7.
    Ordem das operações 0SELECT STATEMENT Cost = 6 1 MERGE JOIN Cost = 1 2 TABLE ACCESS BY INDEX ROWID TIPOS TABLE Cost = 1 3 INDEX FULL SCAN TIPOS_CP INDEX (UNIQUE) Cost = 1 4 SORT JOIN Cost = 2 5 TABLE ACCESS FULL PRODUTOS TABLE Cost = 1 Ordem de execução: 3, 2, 5, 4, 1 e 0.
  • 8.
    Comparação de planosde execução 0 SELECT STATEMENT Cost = 4 ……… versus 0 SELECT STATEMENT Cost = 5 ………
  • 9.
    Sumário • O tuningé o mecanismo que permite aumentar a performance do SQL; • O optimizador relacional é um subsistema de AI que gera planos de execução.

Notas do Editor

  • #8 Operation ID Description 3 Full scan of the index product_types_pk (which is a unique index) to obtain the addresses of the rows in the product_types table. The addresses are in the form of ROWID values, which are passed to operation 2. 2 Access the rows in the product_types table using the list of ROWID values passed from operation 3. The rows are passed to operation 1. 5 Access the rows in the products table. The rows are passed to operation 4. 4 Sort the rows passed from operation 5. The sorted rows are passed to operation 1. 1 Merge the rows passed from operations 2 and 5. The merged rows are passed to operation 0. 0 Return the rows from operation 1 to the user. The total cost of the query is 6 work units.
  • #9 Work units = Unidades de trabalho