Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:抽象PDO数据库。性能注意事项_Php_Performance_Design Patterns_Pdo - Fatal编程技术网

PHP:抽象PDO数据库。性能注意事项

PHP:抽象PDO数据库。性能注意事项,php,performance,design-patterns,pdo,Php,Performance,Design Patterns,Pdo,我正在PHP中的DB上构建一个抽象层 我使用PDO,一切都在纯命令式脚本中完美运行 现在,我想用MVC将这组代码(一个web服务API)转换为OOP方法,并尝试将DB接口抽象为具有类似(在race DB环境中)getRaceList()的调用;或getRookieAthlets();等,并将其呈现在任何其他地方 如果我在要调用的persistence类中执行类似的操作,它将非常有效 class Persistantance{ //other methods /** * return

我正在PHP中的DB上构建一个抽象层

我使用PDO,一切都在纯命令式脚本中完美运行

现在,我想用MVC将这组代码(一个web服务API)转换为OOP方法,并尝试将DB接口抽象为具有类似(在race DB环境中)getRaceList()的调用;或getRookieAthlets();等,并将其呈现在任何其他地方

如果我在要调用的persistence类中执行类似的操作,它将非常有效

class Persistantance{

   //other methods

/**
 * return all races
 **/
public function getRaces(){

    $result = null;
    try {   
        $db = $this->connect(); //return a PDO object
        $query = "SELECT races.title, races.year, races.id FROM races;";
        //here use prepared statement statements if needed
        $result = $db->query($query);
        $races = array();
        foreach ($result as $record) {
            //here insert $record in $races
        }
        $result->closeCursor();

        $db = null;
    }catch(PDOException $e){
        // Print PDOException message
        echo $e->getMessage();
    }
    return $races;
}
}

class View{
    public function viewRaces(){
        //...
        $races = $pers->getRaces();
        foreach($races as $race)
            printRecord($race);
        //...
    }
}
它可以工作,但就像$stmnt->fetchAll()一样,它非常占用内存,因为它将所有内容都放在内存中。Db相当大,但由于不稳定,我不太可能得到非常大的阵列,但我想知道一个模式尽可能与大小无关。 另一方面,如果我构建一个迭代器,我会使持久性接口复杂化很多(我想分发接口):


此外,如果不使多迭代器方法的接口更加复杂,就不可能并行执行更多查询。

听起来有点像过早优化。如果这种方法有效,就坚持下去。一旦你发现它带来了问题,你可以改进它。在任何情况下,运行一个分析器来了解它是如何运行的。是的,我知道这对于事物的实际状态来说是一种过度的杀伤力,我可能会坚持下去,事实上,这个问题是为了展望未来。
//from a view class
perist->a_query()
repeat untill null
    perist->fetch_last_query_next()
perist->close_conn()