digitalpoint
Well-known member
Every hour a cron job kicks off that spins through every thread viewed at least once in the last hour to update it's view counter.
For us, it makes our DB server queries per second chart look like this:
The spikes are the counters being updated (1 query per thread viewed in the last hour). When you have 30,000+ unique threads being viewed every hour, it starts to be a little silly with 30,000+ SQL write queries going on.
You can make it much more efficient by JOINing against a temporary table and updating based on the JOIN and rewriting the XenForo_Model_Thread::updateThreadViews() method to use a query like this:
I just tested it and rather than doing 30,000 SQL UPDATEs every hour, it was able to update all threads that needed it with a single query that took 0.06 seconds to run (against a DB with ~2.6M threads).
For us, it makes our DB server queries per second chart look like this:
The spikes are the counters being updated (1 query per thread viewed in the last hour). When you have 30,000+ unique threads being viewed every hour, it starts to be a little silly with 30,000+ SQL write queries going on.
You can make it much more efficient by JOINing against a temporary table and updating based on the JOIN and rewriting the XenForo_Model_Thread::updateThreadViews() method to use a query like this:
Code:
UPDATE xf_thread
INNER JOIN (
SELECT thread_id, COUNT(*) AS total
FROM xf_thread_view
GROUP BY thread_id
) AS xf_tv ON (xf_tv.thread_id = xf_thread.thread_id)
SET xf_thread.view_count = xf_thread.view_count + xf_tv.total
I just tested it and rather than doing 30,000 SQL UPDATEs every hour, it was able to update all threads that needed it with a single query that took 0.06 seconds to run (against a DB with ~2.6M threads).