ZF-5759: Parse once, execute many time the same SQL statement

Description

Hi,

This proposal was announced here: ZF-5758.

We have took a look on the SGA Trace/optimzer of TOAD, and saw that exactly the same query was parsed as many time as it was executed. This is not as optimized as our own framework (... that only support Oracle, this is a fact!), here is our simple proposal that caches allready prepared statements :


/**
 * Adapter Oracle
 *
 * @package Core
 * @subpackage Db
 */
class Cto_Core_Db_Adapter_Oracle extends Zend_Db_Adapter_Oracle
{

    /** @var array array of Zend_Db_Statement_Oracle */
    private $_preparedStmt = array();

    /**
     * Returns an SQL statement for preparation.
     *
     * @param string $sql The SQL statement with placeholders.
     * @return Zend_Db_Statement_Oracle
     */
    public function prepare($sql) {

        if (isset($this->_preparedStmt[md5($sql)])) {
            return $this->_preparedStmt[md5($sql)];
        }
        
        $stmt = parent::prepare($sql);
        $this->_preparedStmt[md5($sql)] = $stmt;
        return $stmt;
    }

}

To use it, we simply add a new parameter in the config.ini:

[DB]
adapter = oracle
params.dbname = SPMS11
params.username = xxx
params.password = xxx
params.adapterNamespace = Cto_Core_Db_Adapter
params.options.autoQuoteIdentifiers = false

Is there any reason why not putting it in the main code?

Regads, Wilfried LOCHE

Comments

This is a proposed optimization to reduce dramaticaly the number of parsed queries.

In fact it will only consume more memory for something that is just bad writing of the SQL query, and will just cache SQL query for the same HTTP request.

But Oracle by itself cache the query and is doing soft-parse when sufficient. If you have too many hard-parse it's because you are not using bind variables, or not writing the same query with the same syntax (for exemple "select * from emp" is different to "select * from EMP").