PHP Red Bean ORM性能问题

前端之家收集整理的这篇文章主要介绍了PHP Red Bean ORM性能问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在PHP中创建一个WebService,我的网站将用它来查询Ajax调用的信息.

起初我只是使用内置的PHP mysql lib以标准方式完成它,并手动编写所有查询并在MySQL Workbench中创建整个数据模型等等.这非常耗时,如果我不得不改变数据模型,之后一切都会变得非常复杂,所以我决定寻找一个PHP ORM,我发现RedBean在我看来是纯粹的魔法和快乐.

除了我在性能问题上非常困难.我的网站是一个网站,供用户创建自己的电视连续剧列表.我查询一个系列的外部源并将其插入我的数据库,如果它还没有,否则我从我自己的数据库ofc中获取它.

我从这个外部来源回来的xml列出了系列,季节,剧集等等,我把这一切都存储起来.

function InsertSerie($serie) {
    $serieBean = $this->CreateSerieBean($serie->Series);
    $genreBeans = $this->CreateGenreBeans($serie->Series->Genre);
    $actorBeans = $this->CreateActorBeans($serie->Series->Actors);
    $episodeBeans = array();
    foreach ($serie->Episode as $episode) {
        $episodeBean = $this->CreateEpisodeBean($episode);
        $seasonBean = $this->CreateSeasonBean($episode);
        $writerBeans = $this->CreateWriterBeans($episode->Writer);
        $guestBeans = $this->CreateActorBeans($episode->GuestStars);
        $directorBeans = $this->CreateDirectorBeans($episode->Director);
        R::associate($episodeBean,$seasonBean);
        foreach ($writerBeans as $bean) {
            R::associate($episodeBean,$bean);
        }
        foreach ($guestBeans as $bean) {
            R::associate($episodeBean,$bean);
        }
        foreach ($directorBeans as $bean) {
            R::associate($episodeBean,$bean);
        }
        $episodeBeans[] = $episodeBean;
    }
    foreach ($genreBeans as $bean) {
        R::associate($serieBean,$bean);
    }
    foreach ($actorBeans as $bean) {
        R::associate($serieBean,$bean);
    }
    foreach ($episodeBeans as $bean) {
        R::associate($serieBean,$bean);
    }
}

function CreateGenreBeans($genres) {
    if(empty($genres)) { return; }
    $genre = explode("|",$genres);
$genreBeans = array();
foreach ($genre as $g) {
    if($g != '') {
            $genreBeans[] = $this->CreateGenreBean($g);
        }
    }
    return $genreBeans;
}

function CreateGenreBean($genre) {
    $bean = R::dispense('genre');
    $bean->name = (string)$genre;
    return $bean;
}

function CreateDirectorBeans($directors) {
    if(empty($directors)) { return; }
    $director = explode("|",$directors);
$directorBeans = array();
foreach ($director as $d) {
    if($d != '') {
            $directorBeans[] = $this->CreateDirectorBean($d);
        }
    }
    return $directorBeans;
}

function CreateDirectorBean($director) {
    $bean = R::dispense('director');
    $bean->name = (string)$director;
    return $bean;
}

function CreateActorBeans($actors) {
    if(empty($actors)) { return; }
    $actor = explode("|",$actors);
$actorBeans = array();
foreach ($actor as $a) {
    if($a != '') {
            $actorBeans[] = $this->CreateActorBean($a);
        }
    }
    return $actorBeans;
}

function CreateActorBean($actor) {
    $bean = R::dispense('actor');
    $bean->name = (string)$actor;
    return $bean;
}

function CreateWriterBeans($writers) {
    if(empty($writers)) { return; }
    $writer = explode("|",$writers);
$writerBeans = array();
foreach ($writer as $w) {
    if($w != '') {
            $writerBeans[] = $this->CreateWriterBean($w);
        }
    }
    return $writerBeans;
}

function CreateWriterBean($writer) {
    $bean = R::dispense('writer');
    $bean->name = (string)$writer;
    return $bean;
}

function CreateSerieBean($serie) {  
    $bean = R::dispense('serie');
    $bean->serie_id = (string)$serie->id;
    $bean->airs_day_of_week = (string)$serie->Airs_DayOfWeek;
    $bean->airs_time = (string)$serie->Airs_Time;
    $bean->content_rating = (string)$serie->ContentRating;
    $bean->first_aired = (string)$serie->FirstAired;
    $bean->imdb_id = (string)$serie->IMDB_ID;
    $bean->language = (string)$serie->Language;
    $bean->network = (string)$serie->Network;
    $bean->overview = (string)$serie->Overview;
    $bean->rating = (string)$serie->Rating;
    $bean->rating_count = (string)$serie->RatingCount;
    $bean->run_time = (string)$serie->Runtime;
    $bean->serie_name = (string)$serie->SeriesName;
    $bean->status = (string)$serie->Status;
    $bean->last_updated = (string)$serie->lastupdated;
    $bean->thumbnail = (string)$serie->thumbnail;
    return $bean;
}

function CreateSeasonBean($episode) {
    $bean = R::dispense('season');
    $bean->season_id = (string)$episode->seasonid;
    $bean->season_number = (string)$episode->SeasonNumber;
    return $bean;
}

function CreateEpisodeBean($episode) {
    $bean = R::dispense('episode');
    $bean->episode_id = (string)$episode->id;
    $bean->episode_name = (string)$episode->EpisodeName;
    $bean->episode_number = (string)$episode->EpisodeNumber;
    $bean->first_aired = (string)$episode->FirstAired;
    $bean->imdb_id = (string)$episode->IMDB_ID;
    $bean->language = (string)$episode->Language;
    $bean->overview = (string)$episode->Overview;
    $bean->rating = (string)$episode->Rating;
    $bean->rating_count = (string)$episode->RatingCount;
    $bean->last_updated = (string)$episode->lastupdated;
    return $bean;
}

问题是插入一个系列需要大约5分钟,并且它也插入重复项,执行R :: freeze();也没有帮助表现.

问:我如何解决这个问题,我能做些什么来使redbean表现更好,我可以用自己的代码做些什么来使其更好地工作,或者我应该简单地使用不同的解决方案/方法框架等?

尝试共享列表,如建议但具有相同的结果.

function InsertSerie($serie) {
    $serieBean = $this->CreateSerieBean($serie->Series);
    ...
    foreach ($serie->Episode as $episode) {
        $episodeBean = $this->CreateEpisodeBean($serieBean,$episode);
        ...
        $this->CreateDirectorBeans($serieBean,$episode->Director);
        $serieBean->sharedEpisode[] = $episodeBean;
    }
    R::store($serieBean);
}

function CreateDirectorBeans($bean,$directors) {
    if(empty($directors)) { return; }
    $director = explode("|",$directors);
    foreach ($director as $d) {
        if($d != '') {
            $bean->sharedDirector[] = $this->CreateDirectorBean($d);
        }
    }
}

function CreateDirectorBean($director) {
    $bean = R::dispense('director');
    $bean->name = (string)$director;
    return $bean;
}
    ....
我终于找到了如何将执行时间从5分钟降低到大约11秒,仍然需要花费一些时间,但考虑到数据量和工作量,我认为它对硬件有利.

添加了这些代码

R::Begin();
R::associate($bean1,$bean2);
...
R::commit();

现在它收集所有工作并在一个大的事务中执行它,如工作单元模式.另外为了防止插入重复,我切换到使用

$bean = R::findOrDispense($type,$sql,$values);

然后我只返回bean,如果它已经存在,如果不是我创建一个新的并返回它.

原文链接:https://www.f2er.com/php/137342.html

猜你在找的PHP相关文章