Skip to Main Content
  • Questions
  • Mismatch in Execute and Fetch of execution plan of SQL

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Girish.

Asked: November 16, 2016 - 5:36 am UTC

Last updated: November 17, 2016 - 2:38 am UTC

Version: 12.1.0.2.0

Viewed 1000+ times

You Asked

Hi,

I have below trace information for a SQL in tkprof trace file

call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 93 0.07 0.07 0 0 0 0
Fetch 187 267.75 268.20 0 40955055 0 93
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 280 267.82 268.28 0 40955055 0 93

Here there are 93 execution and 93 rows returned.And fetch is almost double that is 187, where is that extra one fetch is taking place.
Why fetch is two times compared to execute

Thanks,
Girish

and Connor said...

Simplest possible cause is code using "old style" cursor processing against a table with one row (or a primary key lookup) but we are still fetching until you get an "not found", eg

SQL> set serverout on
SQL> declare
  2    cursor C is select * from dual;
  3    x varchar2(1);
  4    fetch_count int := 0;
  5  begin
  6    open C;
  7    loop
  8      fetch_count := fetch_count + 1;
  9      fetch C into x;
 10      exit when c%notfound;
 11    end loop;
 12    close C;
 13    dbms_output.put_line('fetches = '||fetch_count );
 14  end;
 15  /
fetches = 2

PL/SQL procedure successfully completed.


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Girish Jahagirdar, November 16, 2016 - 9:06 am UTC

That means new way of using cursor queries is to use bulk collects ?
Connor McDonald
November 17, 2016 - 2:38 am UTC

If you know you are doing a single row lookup, then use:

select ..
into l_local_var
from ...

If you cycling through a rows, then either use:

select ...
bulk collect into l_array_var

or

for i in ( select ... from ... )
loop

end loop;

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library