PDA

View Full Version : Views - Inserts vs Updates


RobDog888
01-29-2008, 05:49 AM
What is the benefits of having some field that is a sum updated via a count table? For example the "views" table when instant updates are not on will add a single record and thread id for everytime the thread is viewed. Then the cron job runs and updated the sum of those grouped records to each associated thread's views field in the thread table.

Is this really worth the effort as everytime you are viewing a thread there is an INSERT happening to the views table so why not just add 1 to the thread tables views field with an UPDATE as its still a single db call. Plus with the cron job it will blast all the records on an hourly basis which may add a surge of resource useage.


So which would be better and why?

Sorry for the long post but couldnt make it shorter :D

Marco van Herwaarden
01-29-2008, 06:38 AM
- Inserting a row in a relative small table "costs" less then finding a key in a large table, retrieving the data, adding 1, and write that row back.
- You will only get a lock for updating on one of the most used tables "thread" once an hour, instead of for each pageview.
- No unique index on threadview to maintain for each update.

And probably more.

RobDog888
01-29-2008, 06:53 AM
<font color="darkgreen">Good points and more important on larger sites.

Thanks Marco

An additional question related to the topic....
How can you go about writting a cron job to do this type of mechanism for something similar to "views"?</font>

Marco van Herwaarden
01-29-2008, 06:57 AM
Please explain more.

RobDog888
01-29-2008, 01:25 PM
<font color="darkgreen">If I had a custom field in the thread table that I wanted to track "total_???" actions, I would create a separate table for adding "total_???" records. But I would need a cron job to execute each hour suming the entries in the "total_???" table for updating the "total_???" field in the threads table.</font>

Marco van Herwaarden
01-29-2008, 02:03 PM
Then i would just take the Scheduled task that updates the threadviews as a start point.

RobDog888
01-29-2008, 02:11 PM
<font color="darkgreen">Ah ok I see now. I can set up a scheduled task to point to my cron job php file.

Are these stored in the db or a file (scheduled tasks)? What do you do when you need to create a product with this ST?

Thanks for the replies </font>