janslu
Active member
I'm working on a bridge that will connect Xenforo and my main site. Thanks to wonderful recources available here I have managed to retrieve the list of latest threads, but I also want to display a list of latest posts, limited to specific forums. This would be an easy thing to do, but I'm running a very large board (15 million posts) and I need to take care of performance. And here's my question: what is the best way to query the database for the last 5 posts coming from nodes x,y,z from the performance point of view? Should I employ xenforo models as much as possible, or query the database directly?
This is the function I'm using to query for threads:
But it seems that for posts I could go another way, querying the model for latest updated threads and than I could employ PostModel to query for last post of each threaid retrieved from XenForo_Model_Thread. Is one of these ways inherently better than the other?
This is the function I'm using to query for threads:
Code:
$nodeModel = XenForo_Model::create('XenForo_Model_Node'); // Retrieve the default viewable forums
if ($nodeids == '') {
$nodeids = $nodeModel->getViewableNodeList();
$nodeids = implode(',', array_keys($nodeModel->getViewableNodeList()));
}
//unset($latestNodes[X], $latestNodes[Y]); // X, Y are the IDs of publicly viewable forums but I don't want to retrieve threads from them
$latestthreads = array();
// Here I use XF's own $db connector (Zend_Db), but you can do it another away
$db = XenForo_Application::getDb();
$query = '
SELECT xt.thread_id, xt.title, xt.reply_count, xt.post_date, xt.node_id, xn.title as forumtitle, xt.username
FROM xf_thread as xt
LEFT JOIN xf_node as xn on xt.node_id = xn.node_id
WHERE xt.node_id IN (' . $nodeids . ')
AND xt.discussion_state = "visible"
AND xt.discussion_open = 1
AND xt.sticky = 0
ORDER BY xt.post_date DESC
LIMIT '.$limit.';
';
$threads = $db->query($query);
while ($thread = $threads->fetch()) {
//error_log("Thread: ".print_r($thread,1));
$latestthreads[$thread['thread_id']] = array(
'id' => $thread['thread_id'],
'title' => $thread['title'],
'node' => $thread['node_id'],
'username' => $thread['username'],
'forumtitle' => $thread['forumtitle'],
'forumurl' => XenForo_Link::buildPublicLink('canonical:forums',array('node_id' => $thread['node_id'], 'title' => $thread['forumtitle'])),
'url' => XenForo_Link::buildPublicLink('canonical:threads', $thread),
'replycount' => $thread['reply_count'],
'dateline' => $thread['post_date']
);
}
return $latestthreads;
But it seems that for posts I could go another way, querying the model for latest updated threads and than I could employ PostModel to query for last post of each threaid retrieved from XenForo_Model_Thread. Is one of these ways inherently better than the other?
Code:
$threadModel = XenForo_Model::create('XenForo_Model_Thread');
if ($nodeids == '') {
$nodeids = $nodeModel->getViewableNodeList();
//$nodeids = implode(',', array_keys($nodeModel->getViewableNodeList()));
}
$latestposts = array();
//error_log($nodeids);
$threads = $threadModel->getThreads(
array(
'node_id' => $nodeids,
'deleted' => false,
'moderated' => false,
'reply_count' =>array('>',0)
),
array(
'limit' => $limit,
'order' => 'last_post_date',
'orderDirection' => 'desc',
'join' => 0
)
);