首页 > 开发 > 综合 > 正文

为什么AUTOTRACE不会在执行计划中显示分区截断(partition pruning)?

2024-07-21 02:07:28
字体:
来源:转载
供稿:网友

the oracle (tm) users' co-operative faq
why does autotrace not show partition pruning in the explain plan ?
为什么autotrace不会在执行计划中显示分区截断(partition pruning)?
--------------------------------------------------------------------------------

author's name: norman dunbar
作者:norman dunbar
author's email: oracle (at) bountifulsolutions.co.uk
 date written:  25  march 2004
写作日期:2004年3月25日
oracle version(s): 9.2.0.3.0
oracle版本: 9.2.0.3.0
why is it that when i use autotrace in sql*plus, the explain plan never shows partition pruning taking place?
为什么当我在sql*plus中使用autotrace时,执行计划从不显示发生了分区截断呢?

 

--------------------------------------------------------------------------------

autotrace not showing partition pruning/elimination is bug 1426992, but, after investigation oracle has decided that this is not an optimiser bug, but a bug in sql*plus. you can, with a bit of knowledge of your data and a little experimentation, deduce that partition pruning is taking place from the output of autotrace, but there are much easier ways !
autotrace不显示分区截断/是错误(bug)1426992,但调查后oracle发现这不是优化器的错误,而是sql*plus的问题。对数据有所了解并经过一点试验后,你可以根据autotrace的输出推断出发生了分区截断,但有更简单的方法!
the following demonstration shows the failings in autotraceand demonstrates a couple of other methods of determining whether or not your partitions are being pruned - or not.
下面的演示显示了autotrace不能显示时用其他方法判断是否发生了分区截断。

--------------------------------------------------------------------------------

autotrace
autotrace
first of all, create a simple table range partitioned over 6 different partitions, and fill it with some test data extracted from all_objects.
首先创建一个简单的根据区间分为6个分区(range partitioned)的表,并从all_objects填充一些测试数据。

sql> create table tab_part (part_key number(1), some_text varchar2(500))
  2  partition by range (part_key) (
  3  partition part_1 values less than (2),
  4  partition part_2 values less than (3),
  5  partition part_3 values less than (4),
  6  partition part_4 values less than (5),
  7  partition part_5 values less than (6),
  8  partition part_6 values less than (maxvalue) );
table created.

sql> insert /*+ append */ into tab_part
  2  select mod(rownum, 10), object_name
  3  from all_objects;
24683 rows created.

sql> commit;
commit complete.
once the table has been filled, analyse it and see how the data has been spread over the various partitions. the first and last partitions have more data in them that the remaining four, hence the differing totals.
一旦表中填入数据,分析并查看数据如何在不同的分区分布。第一和最后的分区比其他四个分区有更多的数据。

sql> analyze table tab_part compute statistics;
table analyzed.

sql> select partition_name, num_rows
  2  from user_tab_partitions
  3  where table_name = 'tab_part'
  4  order by partition_name;
partition_name                   num_rows
------------------------------ ----------
part_1                               4937
part_2                               2469
part_3                               2469
part_4                               2468
part_5                               2468
part_6                               9872
6 rows selected.
now that we have a table to work with, we shall see what autotrace has to say about partition elimination. first, however, note how many logical reads a full scan of the entire table needs :
现在我们有了一个试验表,来看看autotrace是如何处理分区截断的。不过首先,注意全表扫描所需的逻辑读:

sql> set autotrace on
sql> select count(*) from tab_part;
  count(*)
----------
     24683

execution plan
----------------------------------------------------------
   0      select statement optimizer=choose (cost=42 card=1)
   1    0   sort (aggregate)
   2    1     partition range (all)
   3    2       table access (full) of 'tab_part' (cost=42 card=24683)

statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        135  consistent gets
          0  physical reads
          0  redo size
        381  bytes sent via sql*net to client
        499  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
to read 24,683 rows of data oracle had to perform 135 logical reads. keep this in mind and note that the autotrace output shows a full table scan - as we would expect on an unindexed table. the next count should only look in a single partition :
为了读取24,683行数据,oracle进行了135次逻辑读。记住这些,并注意autotrace输出显示了一个全表扫描——正如对于一个无索引表,我们所预期的。下面的count只应当搜索一个单独的分区:

sql> select count(*) from tab_part where part_key = 7;
  count(*)
----------
      2468

execution plan
----------------------------------------------------------
   0      select statement optimizer=choose (cost=17 card=1 bytes=2)
   1    0   sort (aggregate)
   2    1     table access (full) of 'tab_part' (cost=17 card=2468 bytes=4936)
 
statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         49  consistent gets
          0  physical reads
          0  redo size
        380  bytes sent via sql*net to client
        499  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
this seems to have again carried out a full table scan, but as we already know that a real fts takes 135 logical reads, the fact that only 49 were required here should indicate that something is different. autotrace's output is not showing partition elimination. if you didn't know how many reads were required to full scan the table, you would be hard pressed to determine that partition elimination had taken place in this scan.
看起来又一次进行了全表扫描,但正如我们所知,一次真正的fts需要135次逻辑读,事实上这里需要的49次说明有所改变了。autotrace的输出没有显示分区截断。如果你不知道全表扫描需要的读(次数),你很难判断这里的搜索发生了分区截断。

event 10053
事件10053

there are other methods by which we can obtain a true picture of the plan used by the optimiser - a 10053 trace for example would show the details. i've never had to use a 10053 trace so i'm unfortunately not in a position to explain its use, i leave this as 'an exercise for the reader' as they say :o)
我们还有其他方法来获得优化器所用计划的全景——例如,一次10053跟踪将显示出详细信息。我从来都不是必须10053跟踪,所以很不幸我也不适合来解释它的用法,我把它留下来作为“读者的一个练习” :o)

sql_trace and tkprof
i have used sql_trace and tkprof though, so here's what shows up when sql_trace is set true.
sql_trace与tkprof
我用sql_trace与tkprof,下面是当sql_trace设为true时的输出。

sql> set autotrace off
sql> alter session set sql_trace = true;
session altered.
sql> alter session set tracefile_identifier = 'partition';
session altered.

sql> select count(*) from tab_part where part_key = 7;
  count(*)
----------
      2468

sql> alter session set sql_trace = false
session altered.
at this point, exit from sql*plus and locate the trace file in user_dump_dest which has 'partition' in it's name. this is the one you want to run through tkprof. the output from this is shown below :
此时,退出sql*plus并根据user_dump_dest来定位名字含有“partition”的跟踪文件。运行tkprof来解释这个文件,输出如下:

select count(*) from tab_part where part_key = 7

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
parse        1      0.00       0.00          0          0          0           0
execute      1      0.01       0.00          0          0          0           0
fetch        2      0.01       0.01          0         49          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.02       0.01          0         49          0           1
misses in library cache during parse: 0
optimizer goal: choose
parsing user id: 62 
rows     row source operation
-------  ---------------------------------------------------
      1  sort aggregate (cr=49 r=0 w=0 time=10353 us)
   2468   table access full tab_part partition: 6 6 (cr=49 r=0 w=0 time=6146 us)
the explain plan clearly shows that partition 6 was the start and stop partition in the scan. in addition, there were 49 logical reads performed to get at the count. this is identical to what we saw above with autotrace, except we get to see that partition pruning did actually take place.
执行计划清楚的显示了分区6是扫描的起始和结束分区。并且,进行了49次逻辑读。除了真实的看到分区截断的确发生了,这与我们在上面用autotrace的结果一致。

explain plan
执行计划
back in sql*plus, there is another method that can be used. the old faithful explain plan will show how partition pruning did take place.
回到sql*plus,还可以用另一种方法。古老而忠实的explain plan将显示分区截断是如何发生的。

sql> explain plan
  2  set statement_id = 'norman'
  3  for
  4  select count(*) from tab_part where part_key = 7;
explained.
sql> set lines 132
sql> set pages 10000
sql> col operation format a20
sql> col options format a15
sql> col object_name format a15
sql> col p_start format a15
sql> col p_stop format a15
sql> col level noprint
sql>  select level,lpad('  ', 2*level-1)||operation as operation,
  2   options,
  3   object_name,
  4   partition_start as p_start,
  5   partition_stop as p_stop,
  6   cardinality
  7   from plan_table
  8   where statement_id = 'norman'
  9   start with id=0
 10   connect by prior id=parent_id
 11   order by level
operation            options         object_name     p_start         p_stop          cardinality
-------------------- --------------- --------------- --------------- --------------- -----------
 select statement                                                                              1
   sort              aggregate                                                                 1
     table access    full            tab_part        6               6                      2468
once again, the plan clearly shows that partition pruning takes place. the problem is that autotrace doesn't show it at all. unless you really know how many blocks of data you have in a table and all of its partitions, you may find it difficult to determine whether or not you are seeing a 'true' plan when using partitioned tables and autotrace.
计划再次清楚的显示发生了分区截断。问题是autotrace不显示。除非真的知道表中有多少数据块与所有的分区,你会发现使用分区表和autotrace确实很难确定“真正”的计划。

note: do you ever suffer from the plan_table growing too big as developers fail to delete old rows from the table? alternatively, do you forget to delete rows from the table?
注意:有没有为plan_table增长太快而开发人员不从表中删除旧行而痛苦?或者,你是否忘记从表中删除记录?

take a copy of $oracle_home/rdbms/admin/utlxplan.sql and edit it.
复制一份$oracle_home/rdbms/admin/utlxplan.sql并编辑:
change this :
修改:

create table plan_table (
statement_id    varchar2(30), ...
filter_predicates varchar2(4000));
to this :
为:

create global temporary table plan_table (
statement_id    varchar2(30), ...
filter_predicates varchar2(4000))
on commit preserve rows;
now login to sql*plus as sys and :
现在以sys登陆sql*plus,并:

sql> @?/rdbms/admin/utlxplan_edited    /* or whatever your copy is called */
sql> grant all on plan_table to public;
sql> create public synonym plan_table for sys.plan_table;
now when developers or dbas use plan_table and logout their rows will be deleted. a self-tidying plan_table. of course, this is no good if you want to keep rows in plan_table between sessions.
现在当开发人员或dba们使用plan_table并登出时,他们的记录将被删除。一个自我清洁的plan_table。当然,如果你需要在会话间保留plan_table中的记录就不行了。

dbms_xplan
under oracle 9i (release 2 i think) there is a new pl/sql package which you can use to show explain plans. the above statement could have its plan shown using this command :
在oracle 9i(我想是release 2)中,有一个新的pl/sql包可以用于显示执行计划。上面的语句可以用如下指令显示计划:

sql> select * from table(dbms_xplan.display(statement_id=>'norman'));
or, if this was the only statement in my plan_table :
或者,如果这时我plan_table中唯一的语句:

sql> select * from table(dbms_xplan.display);
there is much more information shown with this new feature than with a 'normal' explain plan and you don't have to worry about all that formatting either.
其中比“正规”的执行计划显示了更多信息,并且你也不必为格式化操心。

summary
总结

in summary, autotrace doesn't show partition elimination in oracle up to versions 9i release 2. you should therefore be aware of this fact and use sql_trace or explain_plan to get at the true plan for the sql you are trying to tune/debug.
autotrace到oracle 9ir2为止不显示分区截断。所以你应当注意这个事实并使用sql_trace或explain_plan来获得你需要调整的sql的真正的计划。

--------------------------------------------------------------------------------

further reading:
进一步阅读:

note: 166118.1 partition pruning/elimination on metalink. you will need a support contract to access metalink.
注意:166118.1 partition pruning/elimination on metalink. 你需要一个支持合同来访问metalink。

bug: 1426992 sqlplus autotrace does not show correct explain plan for partition elimination. again on metalink.
bug: 1426992 sqlplus autotrace does not show correct explain plan for partition elimination. 还是在metalink。

--------------------------------------------------------------------------------
本文翻译自http://www.jlcomp.demon.co.uk/faq/autotrace_pruning.html  译者仅保留翻译版权

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表