Skip to Main Content
  • Questions
  • Adaptive execution plans inside Stored Procedure

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Micah.

Asked: April 10, 2017 - 11:23 pm UTC

Last updated: November 27, 2018 - 9:27 am UTC

Version: 12.1.0.2.0

Viewed 1000+ times

You Asked

I have a stored procedure that using a global temp table called ITEMS with delete on commit (session based stats). Inside the stored procedure, sometimes 1 row gets added to ITEMS, sometimes 200,000 rows to that table. ITEMS is then used in a few other queries/updates in the stored procedure.

I add just 1 row to ITEMS, run stats, *then* compile the stored procedure. Then when executing the stored proc, the proc will insert into ITEMS. If it has a only adds a few items to ITEMS, it's relatively fast. If it has thousands of items, then it's excessively slow.

Of course, if I add 200,000 rows to ITEMS, run stats, *then* compile the stored procedure. Then the results are flipped.

I thought about using EXECUTE IMMEDIATE to solve this, but I don't like the idea for a lot of reasons including that it's not easy to edit and I don't get compile time errors.

Is there any other way to make the SQLs in the stored procedure run with adaptive execution plans?

and Connor said...

I'd consider an alternative strategy...since it would appear to be the case than you *know* the number of rows being inserted, why not let the optimizer know, ie, something like (pseudo-code)

procedure sp is
begin
  
  insert into gtt values (1);
 dbms_stats.set_table_stats('','GTT',numrows=>1,numblks=>1,no_invalidate=>false);

  insert into gtt select ....;
  x := sql%rowcount;
  dbms_stats.set_table_stats('','GTT',numrows=>x,numblks=>x/50,no_invalidate=>false);
  
end;
/

Rating

  (1 rating)

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

Comments

for historical executions

Rajeshwaran Jeyabal, November 27, 2018 - 7:40 am UTC

Team,

V$sql has a column called "is_resolved_adaptive_plan" to identifiy the sql that benefit from Adaptive plans.

Do we have any such columns available in dba_hist_sqlstat dictionary for historical executions?
Connor McDonald
November 27, 2018 - 9:27 am UTC

Not to my knowledge, but you could perhaps mine into dba_hist_sql_plan. The OTHER_XML column should have "<info type="adaptive_plan" note="y">" within it.

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