Finally the time has come. I’m currently installing Oracle 11g on a test-box (thanks to my Admin for the box!). I’m very excited about the new features Oracle promise. Unfortunately the memory of the box isn’t huge enough to enable Automatic Memory Management but I’ll try this feature in the future! So the next few weeks I’ll run miscellaneous test on it to see what improvement Oracle 11g brings. Well, lets see…. Oh, and of course, you’ll find the tests in this blog!
After more and more reads about BULK COLLECT and FORALL and their performance improvements I decided to have a closer look on it by myself to see how powerful they really are. So I built a little test-case which inserts all entries from the all_object view into another table. The inserts happens on three different ways:
First way is a simple cursor over the view and a insert in a loop with FETCH into local variables. This way also shows how slow the opening of the cursor itself is.
The second way is a simple FOR – IN LOOP with the insert of the cursor variables.
And, of course, the third way is the way with bulking the rows and inserting them with FORALL so lets see.
So the other table looks like this (three columns are enough for this tests)
SQL> create table temp (owner varchar2(30), name varchar2(30), type varchar2(19)); Table created.
And the three different procedures looks like this
CREATE OR REPLACE PROCEDURE CURSOR_FOR_OPEN_QUERY IS l_sOwner VARCHAR2(30); l_sName VARCHAR2(30); l_sType VARCHAR2(19); CURSOR cur IS SELECT owner, object_name name, object_type type FROM all_objects; BEGIN dbms_output.put_line('Before CURSOR OPEN: ' || systimestamp); OPEN cur; dbms_output.put_line('Before LOOP: ' || systimestamp); LOOP FETCH cur INTO l_sOwner, l_sName, l_sType; IF cur%NOTFOUND THEN EXIT; END IF; INSERT INTO temp values (l_sOwner, l_sName, l_sType); END LOOP; CLOSE cur; dbms_output.put_line('After CURSOR CLOSE: ' || systimestamp); COMMIT; END; / CREATE OR REPLACE PROCEDURE CURSOR_FOR_QUERY IS BEGIN dbms_output.put_line('Before CURSOR: ' || systimestamp); FOR cur IN (SELECT owner, object_name name, object_type type FROM all_objects) LOOP INSERT INTO temp values (cur.owner, cur.name, cur.type); END LOOP; dbms_output.put_line('After CURSOR: ' || systimestamp); COMMIT; END; / CREATE OR REPLACE PROCEDURE BULK_COLLECT_QUERY IS TYPE sOwner IS TABLE OF VARCHAR2(30); TYPE sName IS TABLE OF VARCHAR2(30); TYPE sType IS TABLE OF VARCHAR2(19); l_sOwner sOwner; l_sName sName; l_sType sType; BEGIN dbms_output.put_line('Before Bulk Collect: ' || systimestamp); SELECT owner, object_name, object_type BULK COLLECT INTO l_sOwner, l_sName, l_sType FROM all_objects; dbms_output.put_line('After Bulk Collect: ' || systimestamp); -- FORALL indx IN l_sName.FIRST..l_sName.LAST INSERT INTO temp values (l_sOwner(indx), l_sName(indx), l_sType(indx)); -- dbms_output.put_line('After FORALL: ' || systimestamp); COMMIT; END; /
Ok, then I bounced the database to get no buffers, caching, etc. on it.
So the first execute
SQL> exec cursor_for_open_query Before CURSOR OPEN: 27-SEP-07 10.56.30.699401000 AM +02:00 Before LOOP: 27-SEP-07 10.56.30.922366000 AM +02:00 After CURSOR CLOSE: 27-SEP-07 10.57.07.699791000 AM +02:00
Only look at the seconds it took 37 seconds and nearly nothing for opening the cursor! But how much rows were inserted?
SQL> select count(*) from temp; COUNT(*) ---------- 49424
Truncate the table (truncate to free the extends!) and bounce the database again and now the second run
SQL> exec cursor_for_query Before CURSOR: 27-SEP-07 10.59.47.848249000 AM +02:00 After CURSOR: 27-SEP-07 11.00.09.072525000 AM +02:00
The whole loop took 22 seconds, well this looks already better. Well, also all rows inserted?
SQL> select count(*) from temp; COUNT(*) ---------- 49424
But now (after truncate and bouncing) the bulk collect run
SQL> exec bulk_collect_query Before Bulk Collect: 27-SEP-07 11.01.33.553224000 AM +02:00 After Bulk Collect: 27-SEP-07 11.01.41.874054000 AM +02:00 After FORALL: 27-SEP-07 11.01.42.065753000 AM +02:00
Look at this, for bulking all the lines into the collection took just 8 seconds (for 49 424 rows) and the inserts just 1 second! Unbelievable, together we did everything in 9 seconds where the other ways took over 20 seconds!
Well now lets try to first execute the bulk load then truncate the table again but not bouncing the database so that the buffers and caches a still filled
SQL> exec bulk_collect_query Before Bulk Collect: 27-SEP-07 11.02.31.257498000 AM +02:00 After Bulk Collect: 27-SEP-07 11.02.41.614205000 AM +02:00 After FORALL: 27-SEP-07 11.02.41.818092000 AM +02:00 PL/SQL procedure successfully completed. SQL> select count(*) from temp; COUNT(*) ---------- 49423 SQL> truncate table temp; Table truncated. SQL> exec cursor_for_query Before CURSOR: 27-SEP-07 11.04.04.960254000 AM +02:00 After CURSOR: 27-SEP-07 11.04.25.749038000 AM +02:00
Ok so now we need 10 seconds for the run with the bulk but we sill need 21 seconds for the cursor! So not really a improvement with the cache and so on. Ok final test on a big system with over 268 thousand rows
Before Bulk Collect: 27-SEP-07 11.24.17.034732000 AM +02:00 After Bulk Collect: 27-SEP-07 188.8.131.52020000 AM +02:00 After FORALL: 27-SEP-07 184.108.40.206826000 AM +02:00 PL/SQL procedure successfully completed. COUNT(*) ---------- 267985 Table truncated. Before CURSOR: 27-SEP-07 220.127.116.119354000 AM +02:00 After CURSOR: 27-SEP-07 11.25.02.244549000 AM +02:00 PL/SQL procedure successfully completed. COUNT(*) ---------- 268056
And again, bulking took 8 seconds and the inserts just 1 second! But the run with the cursor took 33 seconds!
So this was just a short test but it definitely shows that BULK COLLECT and FORALL are much faster than cursors within the FOR loop! Only disadvantage of FORALL as you maybe already guess if you looked at the code: You can just perform one DML statement, there is no “FORALL END” clause! But anyway also bulking is a very high-performance functionality of Oracle! So if you have to run throw data collections then use BULK COLLECT!
Today I started with an Oracle Administration course and just finished the “Database Architecture” part. Well, not really new stuff for me. They told me think like: What’s the SGA, PGA (they didn’t say anything about the UGA), background, server and user processes, what’s the diffrence between database, instance and server and a little overview of which work the processes perform. All in all a course which go to less in detail of the components for me. All the stuff they told me I already know and with know I mean: I know it in much more detail! But to be fair, I’m sure that it’s not a bad course for people who starting to be a DBA. And that was just the first session of the course, so we”ll see if the others are more interesting for me!
Today following question came up: Would the JDBC driver of Oracle regonize it, when you set the setMaxRows and perform a SELECT using the FOR UPDATE clause. Example: You’ve a table with 40 rows. Now you call setMaxRows(20) so that you only would get the first 20 rows and perform the select without any where clause (I know, not really a pretty solution: Instead of using rownum, you would select all rows and just stop fetching at the 21th row, but however). Would you now lock all 40 rows or just 20. Well, the answer is (I’ve expected it): You get a row lock on the entry table, not only the first 20 lines. With rownum in the where clause it is now problem. You just lock the selected 20 rows and the other 20 are available for other transactions. So developers: Use the rownum pseudo column instead of the setMaxRows function specially if you perform a select… for update!
But the nicest thing on this little test: I wrote my first Java code since 4 years! 🙂
Oh damn, I just get through a presentation of Steven Feuerstein and found out, that PL/SQL supports object-oriented programming. I’m sure, now you think: Why does this stupid guy mean “oh damn…”. Because this “feature” is available since 8i and got strong on 9i. And now they already released 11g! So I didn’t know this possibility several years ago! Well but now I know it and I find it pretty cool, because PL/SQL gets more and more powerful (there are also supported functions for file I/O, HTTP, XML and many many more). Also external or java stored procedures were a big step forward. I just can recur me: Pretty cool!