简体   繁体   English

数据不一致,使用MySQL,PHP,Yii2

[英]Data not consistence, using MySQL, PHP, Yii2

I'm building a system which tracks records of views of an image. 我正在建立一个跟踪图像视图记录的系统。

The main tables in this project are the view table and max_view table. 该项目中的主要表是视图表和max_view表。 The view table keeps the date, time and number of views. 视图表保留视图的日期,时间和数量。 And the max_view table keep tracks of the maximum number that an image can be viewed. max_view表记录了可以查看图像的最大数量。

Every time when an image is being loaded. 每次加载图像时。 I will store the date and time into the view table. 我将日期和时间存储到视图表中。 And will minus 1 from total max views of the image in max_view table. 并且将从max_view表中图像的最大总观看次数中减去1。

Table example: 表示例:

View Table 查看表

id  | image_id |  datetime             |  username
1     1            2015-03-02 02:30:34     user1
2     1            2015-03-02 02:32:34     user2
3     2            2015-03-03 01:40:34     user1

Max_view Table Max_view表

id   |   max_view   | image_loc
1        1000         /image/image_1.jpg
2        500          /image/image_2.jpg

My SQL query in yii2 framework below: 我在yii2框架中的SQL查询如下:

Choose an image randomly: 随机选择图像:

$row = Yii::$app->db->createCommand("SELECT id FROM image_source ORDER BY RAND()")->queryOne();

Insert into view table: 插入视图表:

Yii::$app->db->createCommand()->insert('view', [
    'image_id' => $row['image_id'],
    'datetime' => $datetime,
    'username' => $user,
])->execute();

Update max view count: 更新最大观看次数:

Yii::$app->db->createCommand("UPDATE max_view SET max_view=max_view-1 WHERE id=$image_id")->execute();

My problem is, when there are large number of users view at the same time. 我的问题是,当同时有大量用户查看时。 The data is not consistence. 数据不一致。 The data inserted in view table tend to be more sometimes. 插入视图表中的数据有时会更多。 For example, if there are 10 people viewed the same image. 例如,如果有10个人观看了同一张图片。 The view table supposedly to have 10 records and max_view table will minus 10 from the total of max view of that image. 视图表应该具有10条记录,max_view表将从该图像的最大视图总数中减去10。 But somehow, it sometimes only minus 9 from the max_view. 但是不知何故,有时max_view只能减负9。

If I test it on my local machine, it works flawlessly. 如果我在本地计算机上对其进行测试,则它可以完美运行。 May I know how do I solved this? 我可以知道如何解决这个问题吗? I'm suspecting that the server is not fast enough to capture the max_view data. 我怀疑服务器的速度不足以捕获max_view数据。 How do I make it more consistent ? 我如何使其更加一致?

If you try to edit data from a SQL database at the same time from different hosts, that often does not work properly. 如果您尝试同时从其他主机编辑SQL数据库中的数据,则通常无法正常工作。 Instead of this just add a New entry into a table which just says "1" and the image ID and count the entrys every time until they reach 1000. 取而代之的是,在表中仅添加“ 1”和映像ID的New条目,并在每次达到1000之前对条目进行计数。

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

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