{"id":303,"date":"2018-01-15T16:09:11","date_gmt":"2018-01-15T16:09:11","guid":{"rendered":"https:\/\/www.spotonoracle.com\/?p=303"},"modified":"2018-01-15T16:09:11","modified_gmt":"2018-01-15T16:09:11","slug":"play-fetch-get-exactly-one-row-at-a-time","status":"publish","type":"post","link":"https:\/\/www.spotonoracle.com\/?p=303","title":{"rendered":"Play fetch &#8211; get exactly one row at a time"},"content":{"rendered":"<p>Every once in a while I want SQL*Plus to fetch one row at a time, so I set ARRAYSIZE to 1.<br \/>\nBut:<\/p>\n<pre class=\"brush: sql; collapse: false; highlight: [14,17]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nSQL&gt; set autotrace traceonly statistics\r\nSQL&gt; set arraysize 1\r\nSQL&gt; select * from user1.cf1 where rownum &lt;= 100;\r\n\r\nStatistics\r\n----------------------------------------------------------\r\n          1  recursive calls\r\n          0  db block gets\r\n        102  consistent gets\r\n          0  physical reads\r\n          0  redo size\r\n     258194  bytes sent via SQL*Net to client\r\n       1091  bytes received via SQL*Net from client\r\n         51  SQL*Net roundtrips to\/from client\r\n          0  sorts (memory)\r\n          0  sorts (disk)\r\n        100  rows processed\r\n<\/pre>\n<p>We only did 51 network round trips to fetch 100 rows. This is because SQL*Plus is using OCI and the default for prefetch (OCI_ATTR_PREFETCH_ROWS) is set to 1. So we end up with 2 rows per fetch call (note that with prefetch 1, the first round trip already returns 1 row).<\/p>\n<p>Now, I really, really want to fetch just one row at a time. Starting with Oracle 12.1 you can create an OCI configuration file to specify various settings, one of them being prefetch. See <a href=\"https:\/\/docs.oracle.com\/database\/121\/LNOCI\/oci10new.htm#LNOCI73052\" rel=\"noopener\" target=\"_blank\">documentation<\/a> for more details.<\/p>\n<p>vi ${TNS_ADMIN}\/oraaccess.xml<\/p>\n<pre class=\"brush: xml; collapse: false; highlight: [5]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot;?&gt;\r\n &lt;oraaccess xmlns=&quot;http:\/\/xmlns.oracle.com\/oci\/oraaccess&quot; xmlns:oci=&quot;http:\/\/xmlns.oracle.com\/oci\/oraaccess&quot; schemaLocation=&quot;http:\/\/xmlns.oracle.com\/oci\/oraaccess http:\/\/xmlns.oracle.com\/oci\/oraaccess.xsd&quot;&gt;\r\n  &lt;default_parameters&gt;\r\n    &lt;prefetch&gt;\r\n      &lt;rows&gt;0&lt;\/rows&gt;\r\n    &lt;\/prefetch&gt;\r\n  &lt;\/default_parameters&gt;\r\n&lt;\/oraaccess&gt;\r\n<\/pre>\n<p>With this configuration in place we should see 100+ network round trips.<\/p>\n<pre class=\"brush: sql; collapse: false; highlight: [14,17]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nSQL&gt; set autotrace traceonly statistics\r\nSQL&gt; set arraysize 1\r\nSQL&gt; select * from user1.cf1 where rownum &lt;= 100;\r\n\r\nStatistics\r\n----------------------------------------------------------\r\n          0  recursive calls\r\n          0  db block gets\r\n        102  consistent gets\r\n          0  physical reads\r\n          0  redo size\r\n     267988  bytes sent via SQL*Net to client\r\n       1652  bytes received via SQL*Net from client\r\n        102  SQL*Net roundtrips to\/from client\r\n          0  sorts (memory)\r\n          0  sorts (disk)\r\n        100  rows processed\r\n<\/pre>\n<p>Go play fetch \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every once in a while I want SQL*Plus to fetch one row at a time, so I set ARRAYSIZE to 1. But: SQL&gt; set autotrace traceonly statistics SQL&gt; set arraysize 1 SQL&gt; select * from user1.cf1 where rownum &lt;= 100; Statistics &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- 1 recursive calls 0 db block gets 102 consistent gets 0 physical reads [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303"}],"version-history":[{"count":8,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spotonoracle.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}