Pages

Thursday, April 9, 2015

Apex vs. Oracle: More Procedural Processing, Less Declarative Processing

One of the first things I read when learning Oracle PL/SQL was Donald Bales's quote, "It's almost always best to let SQL simply do its job! You can't imagine how much PL/SQL code I've seen that can be replaced by one SQL statement."

SalesForce programming, on the other hand, requires being extremely judicious with your use of SOQL queries against the database.

In SalesForce, unlike in Oracle, you have no idea what's indexed and what's not, and you don't get to ask the DBA or "EXPLAIN PLAN", so you have no idea what queries run quickly and what queries run inefficiently.

So SalesForce limits the number of queries you can run from Apex code.

"Bulkifying" Apex code, as I understand it so far, basically means "Don't put SELECT or INSERT/UPDATE/DELETE/UNDELETE inside any sort of loop."

  • For SELECT statements: Dump the entirety of a SELECT statement's results into an in-the-Apex-code collection-typed variable. Post-process that data imperatively with Apex code
    • To put it another way, if in Oracle you might define a Cursor and then Open, Loop, & Close it a few times throughout the course of your program because you know it won't really be a performance drag to do so and will keep the code easy to read...don't do that in SalesForce. Open it once, dump its contents into a PL/SQL variable, close the cursor, and never touch the cursor again - work with your PL/SQL variable's contents instead.
  • For INSERT/UPDATE/DELETE/UNDELETE operations: Imperatively build an in-the-Apex-code collection-typed variable. Perform just 1 DML operation on that collection-typed variable.

No comments:

Post a Comment