Determining when to execute SQL statements statically or dynamically in embedded SQL applications
Consideration | Likely Best Choice |
---|---|
Uniformity of data being queried or operated
upon by the SQL statement
|
|
Quantity of range predicates within the query
|
|
Likelihood of repeated SQL statement execution
|
|
Nature of Query
|
|
Types of SQL statements (DML/DDL/DCL)
|
|
Frequency with which the RUNSTATS command
is issued
|
|
SQL statements are always compiled before they are run.
The difference is that dynamic SQL statements are compiled at runtime, so the application might be slower due to the increased resource use associated with compiling each of the dynamic statements at application runtime versus during a single initial compilation stage as is the case with static SQL.
In a mixed environment, the choice between static and dynamic SQL must also factor in the frequency in which packages are invalidated. If the DDL does invalidate packages, dynamic SQL is more efficient as only those queries issued are recompiled when they are next used. Others are not recompiled. For static SQL, the entire package is rebound once it has been invalidated.
There are times when it does not matter whether you use static SQL or dynamic SQL. For example it might be the case within an application that contains mostly references to SQL statements to be issued dynamically that there might be one statement that might more suitably be issued as static SQL. In such a case, to be consistent in your coding, it might make sense to issue that one statement dynamically too. Note that the considerations in the previous table are listed roughly in order of importance.
Do not assume that a static version of an SQL statement is always faster than the equivalent dynamic statement. In some cases, static SQL is faster because of the resource use required to prepare the dynamic statement. In other cases, the same statement prepared dynamically issues faster, because the optimizer can make use of current database statistics, rather than the database statistics available at an earlier bind time. Note that if your transaction takes less than a couple of seconds to complete, static SQL will generally be faster. To choose which method to use, you should prototype both forms of binding.
- Static SQL statements containing no host variables This is an unlikely situation which you may see only for:
- Initialization code
- Simple SQL statements
Simple SQL statements without host variables perform well from a performance perspective in that there is no runtime performance increase, and the Db2® optimizer capabilities can be fully realized.
- Static SQL containing host variables
Static SQL statements which make use of host variables are considered as the traditional style of Db2 applications. The static SQL statement avoids the runtime resource usage associated with the PREPARE and catalog locks acquired during statement compilation. Unfortunately, the full power of the optimizer cannot be used because the optimizer does not know the entire SQL statement. A particular problem exists with highly non-uniform data distributions.
- Dynamic SQL containing no parameter markers
This is typical of interfaces such as the CLP, which is often used for executing on-demand queries. From the CLP, SQL statements can only be issued dynamically.
- Dynamic SQL containing parameter markers
The key benefit of dynamic SQL statements is that the presence of parameter markers allows the cost of the statement preparation to be amortized over the repeated executions of the statement, typically a select, or insert. This amortization is true for all repetitive dynamic SQL applications. Unfortunately, just like static SQL with host variables, parts of the Db2 optimizer will not work because complete information is unavailable.