简体   繁体   English

在数据库表中存储视图计数

[英]Storing view counts in database table

What is the appropriate and most efficient way to store a view count each time a database record is access? 每次访问数据库记录时,哪种最佳且最有效的方法来存储视图计数?

I have table ITEMS containing the following fields: id, item_name 我有包含以下字段的表ITEMSid, item_name

Each item has its own permalink: http://domain.com/item_name 每个项目都有自己的固定链接: http : //domain.com/item_name

I would like to be able to use this data and display a Views: 2,938 on the page. 我希望能够使用此数据并在页面上显示Views: 2,938 Which method is best? 哪种方法最好?


Method A 方法一

Create an additional field view_count in the ITEMS table and update it to increment the view count: ITEMS表中创建一个附加字段view_count并对其进行更新以增加视图计数:

view_count = view_count + 1;

Method B 方法B

Create a new table VIEWS containing the following fields: 创建一个包含以下字段的新表VIEWS

id, item_id, timestamp, ip

And add a new record to the VIEWS table each time a page is viewed. 每次查看页面时,将新记录添加到VIEWS表中。

Method C 方法C

Is there another method altogether? 完全有另一种方法吗?


I have seen Method A used in a variety of PHP forum software, however my University instructors have informed me that Method B is better because add() requires less strain than update() 我已经看到方法A在各种PHP论坛软件中都使用过,但是我的大学讲师告诉我方法B更好,因为add()update()所需的压力更少

It's somewhat true that an INSERT operation will often consume less resources than an UPDATE statement; 确实,INSERT操作通常比UPDATE语句消耗更少的资源。 but a well tuned UPDATE statement isn't necessarily a strain". 但是调整良好的UPDATE语句不一定会很麻烦。”

There are other considerations. 还有其他考虑。 If the only known and foreseen requirement is for a "total" page count, then a single row will consume much less storage than storing individual rows for each "view" event. 如果唯一已知和可预见的要求是“总”页数,则与为每个“视图”事件存储单独的行相比,单行的存储量要少得多。 Also, the queries to retrieve the view count will be much more efficient. 而且,检索视图计数的查询将更加有效。

(Where the "strain" is in the real world is in storage, not in terms of just disk space, but the number of tapes and the amount of clock time required for backups, time required for restore, etc.) (实际上,“应变”是在存储中,而不仅仅是磁盘空间,而是磁带的数量和备份所需的时钟时间,还原所需的时间等)

If you need to be able to report on views by hour, or by day, then having the more granular data will provide you the ability to do that, which you can't get from just a total. 如果您需要能够按小时或按天报告观看次数,则拥有更细粒度的数据将使您能够做到这一点,而这是您无法从总体上获得的。

You could actually do both. 您实际上可以两者兼而有之。

If I needed a "summary" count to put the page, I wouldn't want to have to run a query against a boatload of rows in the historical event record, just to come up with a new value of 2,939 the next time the page is viewed. 如果我需要一个“摘要”计数来放置页面,那么我就不必对历史事件记录中的大量行运行查询,而在下一次页面时只需输入新值2939被查看。 It would be much less of a "strain" on database resources to retrieve a single column value from a single row, than it would be to churn through thousands of rows of event data to aggregate it into a count, every time a page is viewed. 从单行中检索单列值对数据库资源的“压力”要小得多,而不是每次查看页面时都要翻阅数千行事件数据以将其聚合为一个计数。 。

If I also needed to be able to administratively report "views" by country, or by IP, by hour, by day or week or month, I would also store the individual event rows, to have them available for slice and dice analytics. 如果还需要能够按国家,IP或小时,天,周或月,按国家或IP管理地报告“视图”,则我还将存储单个事件行,以使它们可用于切片和骰子分析。

there is another way so you can get unique IP.. this will help you : http://codebase.eu/source/code-php/ip-counter/ 还有另一种方法可以使您获得唯一的IP。这将为您提供帮助: http : //codebase.eu/source/code-php/ip-counter/

but if you want to store it in database, let me know if this work for you: 但是如果您要将其存储在数据库中,请告诉我是否适合您:

CREATE TABLE `counter` (
`file` VARCHAR(200),
`time` VARCHAR(20),
PRIMARY KEY (`file`)
);

and create counter.php 并创建counter.php

<?php
//read the name of your page
$file=$_SERVER['PHP_SELF'];

//connect to database
mysql_connect("dbhost","dbuser","dbpass");
mysql_select_db("dbname");

//database check
$sql=mysql_fetch_array(mysql_query("SELECT * FROM `counter` WHERE `file` = '$file'"));
if ($sql)
{
//if there is data - it will add one
$add_counter=mysql_query("UPDATE `counter` SET `time` = `time` + 1 WHERE `file` = '$file' LIMIT 1");
}
else
{
//if empty - it will create one
$add_data=mysql_query("INSERT INTO `counter` (`file`,`time`) VALUES ('$file','1')");
} ?>

then put this to show your counter 然后把这个告诉你的柜台

<?php
//connect database
mysql_connect("dbhost","dbuser","dbpass");
mysql_select_db("dbname");

//show array from database
$sql=mysql_query("SELECT * FROM `counter` ORDER BY `file` ASC");
while ($data=mysql_fetch_array($sql))
{
echo ("This ".$data['file']." Get ".$data['time']." Views");
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM