使用explain plan for
语句可以查看执行计划。在plsql developer
工具中,可以直接使用explain plan window
查看SQL
语句的执行计划。
SQL
语句为:
select a.doc_id, a.content, b.title
from cms_doc_body a, cms_doc_single_attr b
where a.doc_id = b.doc_id
and b.title like 'abc%'
and a.content_type = 'text/plain'
得到的执行计划为:
SELECT STATEMENT, GOAL = ALL_ROWS
NESTED LOOPS
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
TABLE ACCESS BY INDEX ROWID
INDEX UNIQUE SCAN
Method for looking up a single key value via a unique index. always returns a single value, You must supply AT LEAST the leading column of the index to access data via the index.
index range scan
--
索引局部扫描
Index range scan is a method for accessing a range values of a particular column. AT LEAST the leading column of the index must be supplied to access data via the index. Can be used for range operations (e.g. > < <> >= <= between) .
index full scan
--
索引全局扫描
Full index scans are only available in the CBO as otherwise we are unable to determine whether a full scan would be a good idea or not. We choose an index Full Scan when we have statistics that indicate that it is going to be more efficient than a Full table scan and a sort. For example we may do a Full index scan when we do an unbounded scan of an index and want the data to be ordered in the index order.
index fast full scan
--
索引快速全局扫描,不带order by
情况下常发生
Scans all the block in the index, Rows are not returned in sorted order, Introduced in 7.3 and requires V733_PLANS_ENABLED=TRUE and CBO, may be hinted using INDEX_FFS hint, uses multiblock i/o, can be executed in parallel, can be used to access second column of concatenated indexes. This is because we are selecting all of the index.
index skip scan
--
索引跳跃扫描,where
条件列是非索引的前导列情况下常发生
Index skip scan finds rows even if the column is not the leading column of a concatenated index. It skips the first column(s) during the search.
3.Rowid
物理ID
扫描
This is the quickest access method available.Oracle retrieves the specified block and extracts the rows it is interested in. --Rowid
扫描是最快的访问数据方式
关于表连接方式
1.
嵌套循环(Nested
Loops
简称NL
)
两表嵌套循环连接的SQL
如下:
select /*+use_nl(a b) leading(b)*/
a.doc_id, a.content, b.title
from cms_doc_body a, cms_doc_single_attr b
where a.doc_id = b.doc_id
and b.title like 'abc%'
and a.content_type = 'text/plain'
嵌套循环连接实现机制(
伪代码)
如下:
declare
begin
for outer_table in (select doc_id, title
from cms_doc_single_attr
where title like 'abc%') loop
for inner_table in (select doc_id, content
from cms_doc_body
where doc_id = outer_table.doc_id
and content_type = 'text/plain') loop
以什么样的方式来访问数据,是全表扫描(Full Table Scan
),索引范围扫描(Index Range Scan
)还是全索引快速扫描(INDEX Fast Full Scan
:INDEX_FFS
);
对于表关联查询,它负责确定表之间以一种什么方式来关联,比如HASH_JOHN
还是NESTED LOOPS