Execution plan

1)  Query recent executed SQL for user from V$SQL

SQL>col sql_text format a50 truncate

SQL>select /* recentsql */ sql_id, child_number, hash_value, address, executions, sql_text
  2  from v$sql
  3  where parsing_user_id = (
  4  select user id from all_users where username = 'testuser')
  5  and command_type in (2,3,6,7,189)
  6  and UPPER(sql_text) not like UPPER('%recentsql%');


SQL_ID        CHILD_NUMBER HASH_VALUE ADDRESS          EXECUTIONS SQL_TEXT
------------- ------------ ---------- ---------------- ---------- --------------------------------------------------
0af82jbk8n805            0 3834257413 000007FFBE35D878          2 select /* DataTypeTest */ * from regions2 where to
dyk4dprp70d74            0 3933222116 000007FFBE6FE450          8 SELECT DECODE('A','A','1','2') FROM DUAL
2kwv6k885cf9a            0  274086186 000007FFB5BE8340          1 SELECT /* OPT_DYN_SAMP */ /*+ ALL_ROWS IGNORE_WHER
atcxc7b9jsjtg                0 3541845807 000007FFB5BBD438          1 SELECT USERENV('SESSIONID') FROM DUAL
...


2) Query the plan for SQL statement  already executed and cached in the memory.

SQL> select / *+ gather_plan_statistics */ * from regions2; -- Tell the Oracle to collect statistics while executing the statement.

REGION_ID  REGION_NAME
---------- -------------------------
1          a
2          b

SQL> select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------

SQL_ID  96fchstu6ysrs, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ * from regions2

Plan hash value: 670750275

----------------------------------------------------------------------------------------
| Id  | Operation         | Name     | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
----------------------------------------------------------------------------------------
|   1 |  TABLE ACCESS FULL| REGIONS2 |      1 |      2 |      2 |00:00:00.01 |       8 |
----------------------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement


16 rows selected.


4) Statistics views/table: V$SQL_PLAN, V$SQL_PLAN_STATISTICS, V$SQL_PLAN_STATISTICS_ALL, PLAN_TABLE

5) Display functions for DBMS_XPLAN: DISPLAY, DISPLAY_CURSOR, DISPLAY_SQLSET, DISPLAY_SQL_PLAN_BASELINE

6) Identify SQL Statement for later plan retrival.

SQL> select /* MARK_STATEMENT */ * from regions2;

REGION_ID  REGION_NAME
---------- -------------------------
1          a
2          b

SQL> select sql_id, child_number, sql_text
  2  from v$sql
  3  where sql_text like '%MARK_STATEMENT%'
  4  /

SQL_ID          CHILD_NUMBER SQL_TEXT
-------------      ------------                   --------------------------------------------------
1znp4apfaxx8g          0                   select /* MARK_STATEMENT */ * from regions2
1nrj9fvvcqc4u            0                   select sql_id, child_number, sql_text from v$sql w

SQL> select * from table(dbms_xplan.display_cursor('1znp4apfaxx8g', 0, 'ALLSTATS LAST'));

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------

SQL_ID  1znp4apfaxx8g, child number 0
-------------------------------------
select /* MARK_STATEMENT */ * from regions2

Plan hash value: 670750275

-----------------------------------------------
| Id  | Operation         | Name     | E-Rows |
-----------------------------------------------
|   1 |  TABLE ACCESS FULL| REGIONS2 |      2 |
-----------------------------------------------

Note
-----
   - dynamic sampling used for this statement
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level


19 rows selected.


### 回答1: 好的,下面我来写一个简单的 Execution Plan 的 demo: 假设我们有一个表 `users`,其中包含以下字段:`id`、`name`、`age`、`gender`。 我们现在想要查询年龄小于等于 30 岁的女性用户,按照年龄从小到大排序。 具体操作如下: 1. 创建索引 ```sql CREATE INDEX age_index ON users(age); CREATE INDEX gender_index ON users(gender); ``` 2. 编写 Execution Plan ```sql EXPLAIN SELECT id, name, age, gender FROM users WHERE age <= 30 AND gender = 'female' ORDER BY age ASC; ``` 3. 执行查询 ```sql SELECT id, name, age, gender FROM users WHERE age <= 30 AND gender = 'female' ORDER BY age ASC; ``` 4. 解释 Execution Plan 解释结果如下: ``` id select_type table partitions type possible_keys key key_len ref rows filtered Extra 1 SIMPLE users NULL range age_index,gender_index age_index 5 NULL 1 100.00 Using index condition; Using filesort ``` 其中,重要的字段解释如下: - `select_type`: 查询类型,这里是 SIMPLE,表示单表查询。 - `table`: 查询的表名。 - `type`: 访问类型,这里是 range,表示使用索引范围扫描。 - `possible_keys`: 可能使用的索引,这里是 age_index 和 gender_index。 - `key`: 实际使用的索引,这里是 age_index。 - `key_len`: 使用索引的长度,这里是 5,表示 age 字段需要占用 4 个字节,再加上一个字符用于比较操作。 - `ref`: 使用索引的列。 - `rows`: 预估扫描的行数。 - `filtered`: 过滤后的行数占比。 - `Extra`: 额外的信息,这里是 Using index condition 和 Using filesort。 通过解释结果可以看到,查询时使用了 age_index 索引,而不是 gender_index 索引。同时,查询时使用了索引条件,过滤掉了大部分不符合条件的行。最后,使用了 filesort 对结果进行排序。 ### 回答2: Execution Plan是用于优化和调优SQL语句性能的工具,它主要用于分析SQL查询语句在数据库中的执行计划。下面是一个使用Execution Plan的demo的简单示例: 假设我们有一个名为"employees"的员工表,表结构如下: ``` CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), age INT, department VARCHAR(50), salary DECIMAL(10, 2) ); ``` 我们需要查询"salary"字段大于等于5000的员工信息,并按照"salary"字段降序排列。 使用Execution Plan,我们可以先执行以下语句开启执行计划: ``` EXPLAIN ANALYZE SELECT * FROM employees WHERE salary >= 5000 ORDER BY salary DESC; ``` 执行完成后,我们可以获得查询的执行计划,执行计划将显示查询的执行步骤和所需的资源消耗,以及可能存在的性能瓶颈。例如,执行计划可能会显示是否使用了索引、是否进行了表扫描等信息。 接下来,我们可以根据执行计划进行优化查询语句。例如,我们可以查看是否使用了索引,如果没有使用索引,我们可以考虑创建合适的索引以提高查询性能。我们也可以查看是否存在表扫描等性能瓶颈,并考虑使用合适的优化方法解决这些问题。 在这个例子中,假设执行计划显示了"salary"字段上的索引没有被使用,那么我们可以创建一个适当的索引来加速查询: ``` CREATE INDEX idx_salary ON employees (salary DESC); ``` 然后,我们再次执行查询语句并开启执行计划,查看优化后的执行计划是否有所改善。 通过以上的方法,我们可以使用Execution Plan工具来优化和调优SQL语句性能,提高数据库查询效率。 ### 回答3: 使用Execution Plan编写一个demo可以帮助我们理解如何优化SQL查询。 首先,我们需要创建一个表来演示。假设我们有一个员工表,包含字段:员工ID、姓名、年龄和工资。 ``` CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50), Age INT, Salary DECIMAL(10, 2) ); ``` 接下来,我们可以插入一些示例数据。 ``` INSERT INTO Employee (EmployeeID, Name, Age, Salary) VALUES (1, '张三', 25, 5000.00), (2, '李四', 30, 6000.00), (3, '王五', 35, 7000.00), (4, '赵六', 40, 8000.00); ``` 现在,让我们来编写查询代码,并查看对应的执行计划。 ``` EXPLAIN SELECT * FROM Employee WHERE Age > 30; ``` 执行此查询,我们将会得到一个查询执行计划。该计划将告诉我们在执行查询时数据库要采取的步骤。 我们可以通过解读执行计划来优化查询。比如,我们可以注意到上述查询使用了全表扫描,这可能效率较低。为了优化这个查询,我们可以使用索引。 我们可以添加一个索引来提高年龄查询的性能。 ``` CREATE INDEX idx_Employee_Age ON Employee (Age); ``` 现在,我们再次执行相同的查询并查看执行计划。 ``` EXPLAIN SELECT * FROM Employee WHERE Age > 30; ``` 我们可以看到执行计划已经改变,它将使用索引来加快查询速度。 通过使用Execution Plan,我们可以了解查询如何执行,并根据需要进行优化。这有助于提高数据库的性能和查询效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值