[英]Joomla: instantiate ContentModelArticle really slow
我有一個約50M大小,約1萬條記錄的json文件。 我正在使用以下代碼以編程方式將其插入Joomla,因為所有相關表會同時更新,例如#_assets,但它運行得很好,但是代碼的運行速度非常慢:現在過去了大約40個小時,但是只處理了大約4k篇文章。 我可以以某種方式加快流程嗎?
我確實知道,如果我確實將#_content
, #_tags
等插入,將會更快,但是我嘗試避免這種瑣事。
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_BASE . DS . 'administrator' . DS . 'components' . DS . 'com_content');
$mainframe = JFactory::getApplication('site');
require_once (JPATH_ADMINISTRATOR.'/components/com_content/models/article.php');
$string = file_get_contents("items.json");
$json_str = json_decode($string, true);
foreach($json_str as $row){
$new_article = new ContentModelArticle();
$data = array(
'title' => $row['title'][0],
'alias' => $row['alias'],
'introtext' => $row['content'],
'state' => 1,
'catid' => 8, /* don't hard code here! */
'created' => $row['pdate'][0],
'created_by' => 798,
'created_by_alias' => $row['poster'][0],
'publish_up' => $row['pdate'][0],
'urls' => $row['urls'],
'access' => 1,
'metadata' => array(
'tags' => $row['tags'],
'robots' => "",
'author' => implode(" ", $row['poster']),
'rights' => "",
'xreference' => "",
),
);
$new_article->save($data);
}
如果您在Joomla中將其作為擴展運行,建議您打開調試功能,並查看Joomla! Debug Console
中的Profile Information
Joomla! Debug Console
Joomla! Debug Console
,查看所有時間都花在了哪里。
您不是,所以其余的都是扣除額(因此可能有問題)。
您正在為每個插入加載ContentModelArticle
,這意味着它不僅創建和銷毀每行的模型類,而且銷毀每行的表類。
如果這些都是插入物,那么您不需要模型的所有開銷(例如,檢查文章是否首先存在)。
如果數據全部有效,那么您也不需要內容模型中的數據准備( prepareTable()
)或內容表中的check()
。
如果不需要一次加載JTable
,則為每篇文章調用在數組中傳遞的表對象save()
。 這將運行bind()
, check()
, store()
-更新資產跟蹤,並完成插入。
因此,您可以嘗試類似的方法,我認為它會更快,但不確定多少。
$contentTable = JTable::getInstance('Content', 'JTable', array());
foreach($json_str as $row){
$data = array(
'title' => $row['title'][0],
'alias' => $row['alias'],
'introtext' => $row['content'],
'state' => 1,
'catid' => 8, /* don't hard code here! */
'created' => $row['pdate'][0],
'created_by' => 798,
'created_by_alias' => $row['poster'][0],
'publish_up' => $row['pdate'][0],
'urls' => $row['urls'],
'access' => 1,
'metadata' => array(
'tags' => $row['tags'],
'robots' => "",
'author' => implode(" ", $row['poster']),
'rights' => "",
'xreference' => "",
),
$contentTable->save($data);
}
* 免責聲明: 這不是經過測試的代碼,只是超出我的腦海,進入瀏覽器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.