Skip to main content

PL/SQL Programming Joke #3: Need to make my code compile faster!


As my 25th winter in Chicago approaches (and after the 2016 elections),, I attempt to cheer myself up with jokes. Programmer jokes.

Jokes that largely have to do with being too lazy to verify assumptions or prove claims before making decisions that turn out to be really bad decision. 

Here's the most recent "joke" I heard from a developer, via an email back in July:
We just encountered a PL/SQL performance problem after migrating a database from 10g to 11g and I tought it might interest you. I isolated the problem using  the 10046 traces and DBMS_PROFILER. So I was able to reproduce the problem with a very simple PL/SQL testcase, but I cannot explain it.
He then pasted in 439 lines of code and output. So this is the "very simple" testcase? It might be, but still I wrote back:

"It may be simple, but it's long and I'd appreciate it if you would summarize for me what you believe you have discovered."

This way, at least, I didn't have to feel the least bit obligated to tangle with his problem until he replied.

But his response was even better and more entertaining than I'd hoped:
I just found out the cause of the strange behavior...Somebody had set the PLSQL_OPTIMIZE_LEVEL to 1 instead of 2 at the database level so that a massive recompilation of all packages would go faster.
When the optimization was set back to 2, the performance problem he noticed earlier disappeared. Hurray!

So now let's go back and parse that paragraph:

"Somebody had set" the optimization level - I sure hope that somebody else knows exactly who that original somebody is. You'd like to think that there aren't too many people with the authority to change the default optimization level on a database instance.

"…so that a massive recompilation of all packages would go faster."

Ah, that's just too delicious! Again, an element of truth that goes a loooong way towards mucking up application performance.

It is true that the higher you set the PL/SQL optimization level, it takes longer for your code to compile, because the compiler is doing more work analyzing and applying transformations to your code to improve runtime performance.

Hey, let's shrink that paragraph down to its essence:
Spend more time compiling your code so that code runs faster for your users.
Sounds like an excellent tradeoff. We wait a little longer for code to compile (and would we even notice the difference? Unlikely.) and in return our users are happier.

Not for that DBA, though. Maybe he had a hot lunch date or had signed up for one of Tom Kyte's amazingly popular webinars and trainings and that was about to begin. At which point he might learn about how important it is to keep that optimization at least at 2. 

Whatever the case, all he knew was that he needed to get that code complied fast.

I just hope it wasn't on a production instance.

Know Any Good "Jokes"?

I bet you've run into all sorts of hilariously truthy stories of developers or DBAs doing the most absurd things for the only slightly less absurd reasons. Please share them with me, either via comment below or by sending an email to steven dot feuerstein at oracle.com. 

In the meantime, don't forget to set the optimization level to 1. Or maybe 0. That way your code will compile faster. 

Giving you less time for that extra cup of coffee. Which is probably a good thing.

That was a joke, OK? Optimization level at 2 or higher, please! 


Originally published in ODTUG's Confessions of a Quick and Dirty Programmer series

Comments

Popular posts from this blog

Get rid of mutating table trigger errors with the compound trigger

When something mutates, it is changing. Something that is changing is hard to analyze and to quantify. A mutating table error (ORA-04091) occurs when a row-level trigger tries to examine or change a table that is already undergoing change (via an INSERT, UPDATE, or DELETE statement). In particular, this error occurs when a row-level trigger attempts to read or write the table from which the trigger was fired. Fortunately, the same restriction does not apply in statement-level triggers.

In this post, I demonstrate the kind of scenario that will result in an ORA-04091 errors. I then show the "traditional" solution, using a collection defined in a package. Then I demonstrate how to use the compound trigger, added in Oracle Database 11g Release1,  to solve the problem much more simply.

All the code shown in this example may be found in this LiveSQL script.
How to Get a Mutating Table ErrorI need to implement this rule on my employees table:
Your new salary cannot be more than 25x th…

How to Pick the Limit for BULK COLLECT

This question rolled into my In Box today:
In the case of using the LIMIT clause of BULK COLLECT, how do we decide what value to use for the limit? First I give the quick answer, then I provide support for that answer

Quick Answer
Start with 100. That's the default (and only) setting for cursor FOR loop optimizations. It offers a sweet spot of improved performance over row-by-row and not-too-much PGA memory consumption.Test to see if that's fast enough (likely will be for many cases).If not, try higher values until you reach the performance level you need - and you are not consuming too much PGA memory. Don't hard-code the limit value: make it a parameter to your subprogram or a constant in a package specification.Don't put anything in the collection you don't need. [from Giulio Dottorini]Remember: each session that runs this code will use that amount of memory.Background

When you use BULK COLLECT, you retrieve more than row with each fetch, reducing context switchi…

Quick Guide to User-Defined Types in Oracle PL/SQL

A Twitter follower recently asked for more information on user-defined types in the PL/SQL language, and I figured the best way to answer is to offer up this blog post.

PL/SQL is a strongly-typed language. Before you can work with a variable or constant, it must be declared with a type (yes, PL/SQL also supports lots of implicit conversions from one type to another, but still, everything must be declared with a type).

PL/SQL offers a wide array of pre-defined data types, both in the language natively (such as VARCHAR2, PLS_INTEGER, BOOLEAN, etc.) and in a variety of supplied packages (e.g., the NUMBER_TABLE collection type in the DBMS_SQL package).

Data types in PL/SQL can be scalars, such as strings and numbers, or composite (consisting of one or more scalars), such as record types, collection types and object types.

You can't really declare your own "user-defined" scalars, though you can define subtypes from those scalars, which can be very helpful from the perspective…