{
  "date": "2017-01-05T15:52:46",
  "slug": "12cr2-no-cardinality-feedback-for-small-queries",
  "link": "https://www.dbi-services.com/blog/12cr2-no-cardinality-feedback-for-small-queries/",
  "title": {
    "rendered": "12cR2: no cardinality feedback for small queries"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nThis follows the comments on Clemens Bleile post about SQL Plan Directives in Oracle 12.2 where he discovered a new behavior that is not documented. The feature is good: bypass the cardinality feedback overhead when the query is fast anyway.<br />\n<!--more--></p>\n<h3>Feature</h3>\n<p>In Clemens Bleile post about <a href=\"http://dbi-services.com/blog/sql-plan-directives-in-12cr2-behavior-is-different-than-in-12cr1/\">SQL Plan Directives changes in 12<em>c</em>R2</a> he determined than the new feature coming in bug 23596611 was responsible for the bypass of automatic reoptimization (the 12<em>c</em> cardinality feedback) in some cases.</p>\n<p>The description gives the idea that the reason for the bypass of cardinality feedback is &#8216;small queries&#8217;:</p>\n<pre><code>SQL&gt; select BUGNO,SQL_FEATURE,DESCRIPTION,OPTIMIZER_FEATURE_ENABLE from v$system_fix_control where bugno=23596611;\n&nbsp;\n     BUGNO SQL_FEATURE                    DESCRIPTION                            OPTIMIZER_FEATURE_ENABLE\n---------- ------------------------------ -------------------------------------- -------------------------\n  23596611 QKSFM_STATS_FEEDBACK_23596611   No feedback for small queries         12.2.0.1</code></pre>\n<p>This is a new concept and in the traces we know there is nothing about &#8216;small queries&#8217;. Here is how to get more information.</p>\n<h3>Function</h3>\n<p>First, I&#8217;m looking for the &#8216;small queries&#8217; text in the oracle executable:</p>\n<pre><code>$ strings $ORACLE_HOME/bin/oracle | grep \"small query\"\nkkocfbCheckCardEst: No feedback for small query (BG %f, CPU %f (ms)\nkkocfbCompareExecStats: skipping cost comparision forsmall query (BG %f, CPU %f (ms)</code></pre>\n<p>Good. This looks like there is a way to trace some information about that bypass decision, and there&#8217;s a clue that &#8216;small queries&#8217; are evaluated on number of buffer gets (BG) and CPU time threshold. More than that, we have the name of the C function that traces this: kkocfbCheckCardEst</p>\n<p>kkocfb probably means:</p>\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\"><a href=\"https://twitter.com/johnnyq72?ref_src=twsrc%5Etfw\">@johnnyq72</a> it’s kernel kompile costing cardinality feedback probably. and it seems there is a trace function&#8230;</p>\n<p>&mdash; Frits Hoogland (@fritshoogland) <a href=\"https://twitter.com/fritshoogland/status/816763076800380929?ref_src=twsrc%5Etfw\">January 4, 2017</a></p></blockquote>\n<p><script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script></p>\n<p>and then I&#8217;m looking for a way to get this trace. I didn&#8217;t know which event can trace that, but others know:</p>\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\">are you talking about event 10507 level 512?</p>\n<p>&mdash; Mauro Pagano (@Mautro) <a href=\"https://twitter.com/Mautro/status/816772241635627008?ref_src=twsrc%5Etfw\">January 4, 2017</a></p></blockquote>\n<p><script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script></p>\n<p>However there is another way to get an idea of the events that are checked by a function.<br />\nThe idea is described here: <a href=\"http://yong321.freeshell.org/computer/OracleFuncCalls.html\">http://yong321.freeshell.org/computer/OracleFuncCalls.html</a></p>\n<h3>Event</h3>\n<p>We are not allowed to disassemble oracle code (this is in the licensing rules that you accept when you download oracle) so I&#8217;ll not do it here.<br />\nIf you think you have a special permission to do it, just run:</p>\n<pre><code>gdb oracle &lt;&lt;&lt;&quot;disas kkocfbCheckCardEst&quot;</code></pre>\n<p>I can&#8217;t show the dissassembled code, so here is how to get the list of the events that are checked by the kkocfbCheckCardEst function:</p>\n<pre><code>$ gdb oracle &lt;&lt;&lt;&quot;disas kkocfbCheckCardEst&quot; | awk --non-decimal-data &#039;/mov .*,%edi$/{gsub(/[$,]/,&quot; &quot;);a=$4}/EventRdbmsErr/{printf &quot;%dn&quot;, a}&#039; | sort -u\n10507</code></pre>\n<p>The information we have about this event does not describe all what can be traced:</p>\n<pre><code>$ oerr ora 10507\n10507, 00000, \"Trace bind equivalence logic\"\n// *Cause:\n// *Action:</code></pre>\n<p>From that, I don&#8217;t know which level to set the event, but in case of doubt and because levels are often bit flags, you can set it to the maximum 0xFFFFFFFF:</p>\n<pre><code>\nalter session set events '10507 trace name context forever, level 4294967295';\n</code></pre>\n<h3>trace when cardinality feedback is bypassed</h3>\n<p>When I run the query where execution is doing less than 100 buffer gets I get the following trace with event 10507 level 512:</p>\n<pre><code>\n****** Begin CFB Hints (APA) [sql_id=a71qw8t17qpqq] xsc=0x7fca901f0e30 ctx=0x9415d4f8 ******\nDumping Hints\n=============\n*********** End CFB Hints (APA) ***********\nkkocfbInitCardFdbkCompCtx [sql_id=a71qw8t17qpqq] monitor=y\nkkocfbCheckCardEst: No feedback for small query (BG 0.000000, CPU 0.000000 (ms)\n</code></pre>\n<p>The message is clear: no feedback for small query. The Buffer Get and CPU show 0 but on my test case, increasing the size of the table, I have seen that 100 buffer gets is the threshold:</p>\n<pre><code>\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          95 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          95 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          96 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          96 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          97 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          97 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          98 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          98 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          99 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: N                buffer_gets:          99 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         100 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         100 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         101 cpu_time:       1000\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         101 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         102 cpu_time:          0\nbq4fc1rdx97av is_reoptimizable: Y                buffer_gets:         102 cpu_time:       1000\n</code></pre>\n<h3>trace when query becomes reoptimizable</h3>\n<p>When the query reads more than 100 buffer gets, the cursor is marked as reoptimizable and here is the trace:</p>\n<pre><code>\n****** Begin CFB Hints (APA) [sql_id=a71qw8t17qpqq] xsc=0x7fed7dc2ca40 ctx=0x9418f4f8 ******\nDumping Hints\n=============\n*********** End CFB Hints (APA) ***********\nkkocfbInitCardFdbkCompCtx [sql_id=a71qw8t17qpqq] monitor=y\nReparsing due to card est...\n @=0x95426550 type=1 nodeid=1 monitor=Y halias=\"DEMO_TABLE\" loc=\"SEL$1\" act=500 min=0 est=63 next=(nil)\nkkocfbCheckCardEst [sql_id=a71qw8t17qpqq] reparse=y ecs=n efb=n ost=n fbs=n\n *********** Begin Dump Context (kkocfbCheckCardEst) [sql_id=a71qw8t17qpqq cpcnt=0] ***********\n @=0x95426550 type=1 nodeid=1 monitor=Y halias=\"DEMO_TABLE\" loc=\"SEL$1\" act=500 min=0 est=63 next=(nil)\n *********** End Dump Context ***********\n</code></pre>\n<p>act=500 is the actual number of rows (A-Rows) and est=63 is the estimated one (A-Rows) and this is how misestimate is detected.</p>\n<h3>trace when reoptimized query is run again</h3>\n<p>At that point we can see the feedback as OPT_ESTIMATE hints in v$sql_reoptimization_hints.</p>\n<p>When we run the same query another time, it is parsed again with those hits, fixing cardinality tho the A-Rows of previous execution.</p>\n<p>Here is the trace for this second execution.</p>\n<pre><code>\nkkoarCopyCtx: [sql_id=a71qw8t17qpqq] origin=CFB old=0x63904140 new=0x7fcda3716d40 copyCnt=1 copyClient=y\n**************************************************************\nkkocfbCopyBestEst: Best Stats\n  Exec count:         1\n  CR gets:            279\n  CU gets:            4\n  Disk Reads:         0\n  Disk Writes:        0\n  IO Read Requests:   0\n  IO Write Requests:  0\n  Bytes Read:         0\n  Bytes Written:      0\n  Bytes Exchanged with Storage:  0\n  Bytes Exchanged with Disk:  0\n  Bytes Simulated Read:  0\n  Bytes Simulated Returned:  0\n  Elapsed Time: 6998 (us)\n  CPU Time: 2000 (us)\n  User I/O Time: 0 (us)\n *********** Begin Dump Context (kkocfbCopyBestEst) **********\n *********** End Dump Context ***********\nkkocfbCopyCardCtx: oldCtx Dumping string mapping\n----------------------\nkkocfbCopyCardCtx: newCtx Dumping string mapping\n----------------------\n**************************************************************\nkkocfbCopyCardCtx: Best stats\n  Exec count:         1\n  CR gets:            279\n  CU gets:            4\n  Disk Reads:         0\n  Disk Writes:        0\n  IO Read Requests:   0\n  IO Write Requests:  0\n  Bytes Read:         0\n  Bytes Written:      0\n  Bytes Exchanged with Storage:  0\n  Bytes Exchanged with Disk:  0\n  Bytes Simulated Read:  0\n  Bytes Simulated Returned:  0\n  Elapsed Time: 6998 (us)\n  CPU Time: 2000 (us)\n  User I/O Time: 0 (us)\n **** Begin Dump Context (kkocfbCopyCardCtx - best est) ****\n ********************* End Dump Context ********************\n   *********** Begin Dump Context (Copy:old) [sql_id=a71qw8t17qpqq cpcnt=0] ***********\n   @=0x6446dc58 type=1 nodeid=1 monitor=Y halias=\"DEMO_TABLE\" loc=\"SEL$1\" act=500 min=0 est=63 next=(nil)\n   *********** End Dump Context ***********\n   *********** Begin Dump Context (Copy:new) [sql_id=a71qw8t17qpqq cpcnt=0] ***********\n   @=0x7fcda3716a78 type=1 nodeid=1 monitor=Y halias=\"DEMO_TABLE\" loc=\"SEL$1\" act=500 min=0 est=63 next=(nil)\n   *********** End Dump Context ***********\nkkoarReparse: xsc=0x7fcda3672b98 kxsAutoReoptCtx=0x7fcda3716d40\nkkocfbAddCardHints: Dumping string mapping\n----------------------\n******** Begin CFB Hints [sql_id=a71qw8t17qpqq]  xsc=0x7fcda3672b98 ********\nDumping Hints\n=============\n  atom_hint=(@=0x7fcda37831e8 err=0 resol=0 used=0 token=1018 org=6 lvl=3 txt=OPT_ESTIMATE (@\"SEL$1\" TABLE \"DEMO_TABLE\"@\"SEL$1\" ROWS=500.000000 ) )\n********** End CFB Hints **********\n</code></pre>\n<p>You can see the OPT_ESTIMATE hints here.</p>\n<pre><code>\n****** Begin CFB Hints (APA) [sql_id=a71qw8t17qpqq] xsc=0x7fcda3672b98 ctx=0x8a274118 ******\nDumping Hints\n=============\n  atom_hint=(@=0x7fcda37831e8 err=0 resol=1 used=0 token=1018 org=6 lvl=3 txt=OPT_ESTIMATE (@\"SEL$1\" TABLE \"DEMO_TABLE\"@\"SEL$1\" ROWS=500.000000 ) )\n  atom_hint=(@=0x7fcda3782d10 err=0 resol=1 used=0 token=1018 org=6 lvl=3 txt=OPT_ESTIMATE (@\"SEL$1\" TABLE \"DEMO_TABLE\"@\"SEL$1\" ROWS=500.000000 ) )\n*********** End CFB Hints (APA) ***********\nkkocfbInitCardFdbkCompCtx [sql_id=a71qw8t17qpqq] monitor=y\n**************************************************************\nkkocfbCopyBestEst: Best Stats\n  Exec count:         1\n  CR gets:            279\n  CU gets:            4\n  Disk Reads:         0\n  Disk Writes:        0\n  IO Read Requests:   0\n  IO Write Requests:  0\n  Bytes Read:         0\n  Bytes Written:      0\n  Bytes Exchanged with Storage:  0\n  Bytes Exchanged with Disk:  0\n  Bytes Simulated Read:  0\n  Bytes Simulated Returned:  0\n  Elapsed Time: 6998 (us)\n  CPU Time: 2000 (us)\n  User I/O Time: 0 (us)\n *********** Begin Dump Context (kkocfbCopyBestEst) **********\n *********** End Dump Context ***********\nkkocfbCheckCardEst [sql_id=a71qw8t17qpqq] reparse=n ecs=n efb=y ost=n fbs=n\n</code></pre>\n<h3>So what?</h3>\n<p>With those adaptive feature it is good to be able to trace the decisions in order to understand and reproduce the problems we encounter. Event 10507 is very useful. It&#8217;s the execution time counterpart for the event 10053 which explains compile time decision. And in latest versions, the optimizer is more and more present at execution time.</p>\n<p>We have always seen problems coming from cardinality feedback. Most of them are coming from bad statistics or a data model where there is not one optimal access plan. Then the CBO is always trying to find better and sometimes the best is the enemy of the good.<br />\nNow in 12.2 you have a very good way to avoid the cardinality feedback side effect: tune your queries and your data model so that the critical queries read less than 100 logical reads.</p>\n",
    "protected": false
  }
}
