dongzi5062 2012-05-10 11:55
浏览 56
已采纳

Zend_Db表演

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 ?

  • 写回答

3条回答 默认 最新

  • duangu1878 2014-10-21 15:04
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳