kitensei
kitensei

Reputation: 2530

Zend_Db performances

After doing some research on differents libraries/framework to implement in a pretty big application (350 tables db, several millions entries on some tables) I found out that Zend_Db do pretty easily what I want to do: Adapter management for quick switch between databases.

The problem is that performances are really low, here's an example ($db is a basic adapter, the time is computed on the select/fetch only) :

SQL QUERY (the query used for testing, the table contains ~200 elements)

SELECT * FROM element WHERE id=2'

Basic PDO - 0.6392s

$db = new PDO('mysql:dbname=etab_191;host=127.0.0.1', 'root');
for ($i=0; $i<2000; $i++) {
    $stmt = $db->query($sql);
    $p = $stmt->fetch(PDO::FETCH_OBJ);
    $stmt->closeCursor();
}

Current Application database manager - 0.7401s (simple abstraction layer over mysqli core functions)

$db = GestionConnexionBDD::getInstance('default', 1)->gestionBDD;
for ($i=0; $i<2000; $i++) {
    $res = $db->query($sql);
    $p = $db->fetchObject($res);
    $db->freeResult($res);
}

Zend_Db manual query - 1.0647s (Mv_Core_Db_Manager is an abstraction layer based on Zend, returning a list of Zend_Db_Adapter_Abstract)

    $db = Mv_Core_Db_Manager::getInstance()->getAdapter('default', 1);
for ($i=0; $i<2000; $i++) {
    $stmt = $db->query($sql);
    $p = $stmt->fetch();
    $stmt->closeCursor();
}

Zend_Db_Table_Abstract query - 3.6702s (tweaked with Zend_Db_Table_Abstract::setDefaultMetadataCache($cache))

$elmt = new Element();
for ($i=0; $i<2000; $i++) {
    $elmt->find(2);
}

Querying on a loop kills zend performances. I know it's not the best thing to do, but the application is already developed and i'd love to change less code possible.

Some ideas ? Am i doing something wrong ?

Upvotes: 1

Views: 992

Answers (3)

Nicolay77
Nicolay77

Reputation: 2154

Zend_DB_Abstract will query table metadata on each PHP request.

This means doing a DB DESCRIBE TABLE, that can be very slow on some databases.

To avoid this, you can cache such metadata, and this will improve query performance:

    /////////////////////////////
    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core',
        'File',
        array('lifetime' => 86400, 'automatic_serialization' => true ),
        array('cache_dir' => $config->cacheDir));

    // Next, set the cache to be used with all table objects
    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);

Upvotes: 2

cypherabe
cypherabe

Reputation: 2603

A few pointer:

  • using Table / Row classes will always be slightly slower because of the added overhead of the objectification (if that is even a word ;) )
  • at my workplace we just had similar problems using doctrine (1.2), in the next few weeks we'll be experimenting with APC (sadly, the images on that article are gone)
  • I thought Zend_Db had a query cache mechanism, but I can't find informations about it in the reference guide

Upvotes: 1

coding Bott
coding Bott

Reputation: 4357

Abstraction has a price.

Zend is a php framework, which is of curse much slower than native extensions like pdo. Zend_DB / Zend_Db_Table creates lot of instances of classes at runtime. maybe you run you app much fast with a bytecodecache like apc or that build-in in zend-server.

maybe HipHop is also a solution for you.

Upvotes: 1

Related Questions