gpt4 book ai didi

PHP 红 bean ORM 性能问题

转载 作者:可可西里 更新时间:2023-11-01 12:35:45 29 4
gpt4 key购买 nike

我正在用 php 创建一个 WebService,我的网站将使用它通过 Ajax 调用来查询信息。

起初我只是用内置的 php mysql 库以标准方式完成它,然后手动编写所有查询并在 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 已经存在,我就返回它,如果不存在,我就创建一个新的并返回它。

关于PHP 红 bean ORM 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675227/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com