• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

What is the xf_attachment_view table for?

truonglv

Formerly Nobita.Kun
#2
Logs the viewing of an attachment.
PHP:
/**
    * Logs the viewing of an attachment.
    *
    * @param integer $attachmentId
    */
    public function logAttachmentView($attachmentId)
    {
        $this->_getDb()->query('
            INSERT ' . (XenForo_Application::get('options')->enableInsertDelayed ? 'DELAYED' : '') . ' INTO xf_attachment_view
                (attachment_id)
            VALUES
                (?)
        ', $attachmentId);
    }
 

Chris D

XenForo developer
Staff member
#3
It's a MySQL memory table. Each time someone views an attachment it logs the attachment ID in there.

Later on, a Cron entry will add those to the view_count in the xf_attachment table.
 

emc2

Active member
#4
Oh I c.

I understood the 'Logs the viewing of an attachment.' but couldn't figure out why.. Very smart way to do it in regards to performance..
 

Rigel Kentaurus

Well-known member
#5
The technique has been around for ages, maybe as old as vBulletin 2
But then, part of the reason was an update locked the full MyISAM table. These days, InnoDB has row locking, so I am not really sure how terribly necessary this is anymore.
 

Chris D

XenForo developer
Staff member
#7
There's no point in it logging the user_id and date as the table is truncated via a cron periodically and the process that does that would only grab the number of times its been viewed and update the view_count column.

Alhough what you're suggesting would be more functional, you wouldn't be able to make that fit in the current process... You'd have to create your own, additional table and modify the log attachment view function to still write to the existing table, but then additionally write to your custom table the other details you want.
 

Luke F

Well-known member
#8
The technique has been around for ages, maybe as old as vBulletin 2
But then, part of the reason was an update locked the full MyISAM table. These days, InnoDB has row locking, so I am not really sure how terribly necessary this is anymore.
It's still a good bit more efficient to do it this way