How Oracle Decides To Share

Oracle uses a complex algorithm to decide whether a cursor that is about to be executed may reuse an already compiled version from the shared pool. Here is a simplified presentation of that algorithm:

  1. Calculate the sum of the ASCII values of all characters in the cursor (excluding bind variables). For example, the total ASCII value of the following cursor is 2556:

    SELECT order_date FROM orders

    This is calculated as ASCII(S) + ASCII(E) + ASCII(L)...or 83 + 69 + 76 and so on.

  2. Apply a hash algorithm to the ASCII total.

  3. Peruse the shared pool for a cursor with the same hash value.

  4. If one is found, it may be reused.

Tip

Note that I say “may” be reused. Most of the time, a matching ASCII hash is enough to allow reuse, but not always, as I’ll explain later in this section.

In item 1, I state that the ASCII value of every character is used. Thus, something as low level as mixing uppercase and lowercase has to be considered when planning for cursor reuse. Consider these two cursors run directly in SQL*Plus.

    SQL> SELECT order_date
      2    FROM orders
      3   WHERE order_number = 11;

    ORDER_DAT
    ---------
    03-MAY-05

    SQL> SELECT order_date
      2    FROM orders
      3   WHERE order_numbeR = 11;

    ORDER_DAT
    ---------
    03-MAY-05

The human eye can see that both of these examples do exactly the same thing—select the order date for order number 11. The only difference is that an uppercase R was used in the second one. That’s just enough for Oracle to consider them different, though, so two cursors wind up in ...

Get Oracle PL/SQL for DBAs now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.