Question

The idea is to show how many times people have visited a certain product page.

So far I've come up with two options.

  1. Add a new column called "views" in the products table(Mysql). This seems to be the most simple and fastest way to setup.

  2. Create a new table called "product_views". Have 3 columns for it. id, product id, views. From there you can use JOINS to update and call for view count.

So what I would like to know which option is better?

Was it helpful?

Solution

Depends on what you want to go for. I usually log page views on a table such as:

ID (int), PRODUCT_ID (int), IP_ADDRESS (varchar(15)), VIEW_TIME (datetime)

and that gives me the ability to count either full views or unique views by IP ADDRESS and just do a COUNT(*) on the table, and an added benefit of having a log of who visits the page. Obviously this will increase your space requirement needed for the DB.

If you're going to just create a product view column, then create a stored procedure that will take in the product id as a parameter and increase the value of the product_view column by 1 each time it's run.

OTHER TIPS

Ip address should be unsigned int(10), you convert the string to an number with INET_ATON. And similar reverse. Saves space and makes it much faster sort and filter on ip address. I have a table that looks like this:

CREATE TABLE statistics 
(
   id INT UNSIGNED NOT NULL AUTO_INCREMENT,
   productid INT(6) NOT NULL DEFAULT 0,
   ip INT(10) UNSIGNED NOT NULL DEFAULT 0,
   event char(1) NOT NULL DEFAULT 'V',
   intervall TIME NOT NULL DEFAULT 0,
   createddate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (id)
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top