{"id":140,"date":"2009-05-22T20:31:52","date_gmt":"2009-05-23T01:31:52","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=140"},"modified":"2011-07-06T10:18:04","modified_gmt":"2011-07-06T15:18:04","slug":"how-many-rows-does-plsql-array-fetch-by-default","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2009\/05\/22\/how-many-rows-does-plsql-array-fetch-by-default\/","title":{"rendered":"How many rows does PL\/SQL array fetch by default?"},"content":{"rendered":"<p>I was curious as to how many rows PL\/SQL will fetch by default without a BULK COLLECT clause.  For those of you that may not know, fetching multiple rows at a time can <b>substantially<\/b> reduce the run times of programs that deal with a large number of rows.<\/p>\n<p>We set up the obligatory test table&#8230;<\/p>\n<pre lang=\"sql\" line=\"1\">\r\nSQL> create table t0522 as select rownum r from dba_objects;\r\n\r\nTable created.\r\n<\/pre>\n<p>&#8230;then enable trace on our session&#8230;<\/p>\n<pre lang=\"sql\" line=\"1\">\r\nSQL> exec dbms_monitor.session_trace_enable(binds=>true,waits=>true);\r\n\r\nPL\/SQL procedure successfully completed.\r\n<\/pre>\n<p>&#8230;and then retrieve all rows&#8230;<\/p>\n<pre lang=\"plsql\" line=\"1\">\r\nSQL> begin\r\n  2\u00a0\u00a0\u00a0 for cur in (select * from t0522) loop\r\n  3\u00a0\u00a0\u00a0\u00a0\u00a0 null;\r\n  4\u00a0\u00a0\u00a0 end loop;\r\n  5\u00a0 end;\r\n  6\u00a0 \/\r\n\r\nPL\/SQL procedure successfully completed.\r\n\r\nSQL>\r\n<\/pre>\n<p>&#8230;and after we run our trace file through tkprof, we see that we fetch 100 rows at a time by default.  We can determine this by dividing the total number of rows we fetched, 51911, by the number of fetches it took to retrieve them, 520.<\/p>\n<pre lang=\"text\" line=\"1\">\r\nSELECT *\r\nFROM\r\nT0522\r\n\r\ncall\u00a0\u00a0\u00a0\u00a0 count\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cpu\u00a0\u00a0\u00a0 elapsed\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 disk\u00a0\u00a0\u00a0\u00a0\u00a0 query\u00a0\u00a0\u00a0 current\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 rows\r\n------- ------\u00a0 -------- ---------- ---------- ---------- ----------\u00a0 ----------\r\nParse\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0 0.01\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.03\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\r\nExecute\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0 0.00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\r\nFetch\u00a0\u00a0\u00a0\u00a0\u00a0 520\u00a0\u00a0\u00a0\u00a0\u00a0 0.30\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.31\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 603\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 51911\r\n------- ------\u00a0 -------- ---------- ---------- ---------- ----------\u00a0 ----------\r\ntotal\u00a0\u00a0\u00a0\u00a0\u00a0 522\u00a0\u00a0\u00a0\u00a0\u00a0 0.32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.35\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 604\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 51911\r\n<\/pre>\n<p>As such, if you are using BULK COLLECT in your code, it really isn&#8217;t benefiting you any more than a regular cursor loop unless you increase your array size to something (substantially) more than 100.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was curious as to how many rows PL\/SQL will fetch by default without a BULK COLLECT clause. For those of you that may not know, fetching multiple rows at a time can substantially reduce the run times of programs&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2009\/05\/22\/how-many-rows-does-plsql-array-fetch-by-default\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,22],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/140"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":20,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":1364,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/140\/revisions\/1364"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}