Mysql PHP Cron用于更新用户统计信息 [英] Mysql PHP Cron for updating user stats

查看:147
本文介绍了Mysql PHP Cron用于更新用户统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个php cron作业,它将每24小时运行一次,并在mysql数据库中更新用户静态。我遇到的问题是我不知道我应该如何保证相同的用户值不更新在一个循环,意味着没有其他的更新。我试图做一个while循环,但它并没有好。



所以基本上应该像这样工作

1. cron starts - >

2.更新每个用户在mysql中的统计信息数据库 - >

3. cron结束

解决方案

MySQL



您需要MySQL event 才能按时间运行。



我建议您根据您的条件编写更新语句,并将相同的过程链接到事件调度程序希望每天开始运行。



您可以执行以下操作:。



$ b

/
drop事件(如果存在)event_scheduling_sample;

创建事件(如果不存在)event_scheduling_sample
- 按计划每86400秒启动00:00:00
- 在时间戳(adddate(current_date,1) 00:00')
- 按计划每1天
- 开始current_timestamp
- 结束current_timestamp +间隔'5'天
- 按计划每1天
- 开始current_timestamp
- 结束时间戳(current_date,'23:59:59')

每1天一次
开始时间戳(current_date + '00:00:00')

注释'事件调度样例'
do
调用db_name.procedure_name_that_updates_the_user_records();
;
//
delimiter;

请参阅 MySQL:CREATE EVENT语法



事件计划程序的默认状态为DISABLED。

您需要通过以下任何语句启用它。

  SET GLOBAL event_scheduler = ON; 
SET @@ global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@ global.event_scheduler = 1;

事件计划程序打开时,事件调度程序线程列在 SHOW PROCESSLIST 作为守护进程,其状态如下所示:

  mysql> ; SHOW PROCESSLIST\G 
*************************** 1.行*********** ****************
Id:1
用户:root
主机:localhost
db:NULL
命令:查询
时间:0
状态:NULL
信息:show processlist
********************* **** 2.行***************************
Id:2
用户:event_scheduler
Host:localhost
db:NULL
命令:守护程序
时间:3
状态:等待下一次激活
信息:NULL
2行设置(0.00秒)

事件计划程序设置为ON后, p>

请参阅 MySQL事件计划程序配置


I'm trying to build a php cron job that will run every 24 hours and updated user statics in a mysql database. The problem I've run into is I'm not sure how I should insure that the same users values are not updated in a loop meaning none of the others are updated. I was trying to do it with a while loop however it hasn't gone well.

So basically it should work like this
1. cron starts ->
2. updates each users stats in mysql database ->
3. cron ends

解决方案

MySQL solution:

You require MySQL event to run on a time basis.

And I suggest you write the update statements based on your condition and link the same procedure to the event scheduler you want to run on every day start.

You can do it as following:.

Example:

delimiter //
drop event if exists event_scheduling_sample;

create event if not exists event_scheduling_sample
--  on schedule every 86400 second starts 00:00:00
--  at timestamp( adddate( current_date, 1 ),'00:00:00' )
--  on schedule every 1 day 
    --   starts current_timestamp 
    --   ends current_timestamp + interval '5' day
--  on schedule every 1 day 
    --   starts current_timestamp 
    --   ends timestamp( current_date,'23:59:59' )

  on schedule every 1 day 
     starts timestamp( current_date + 1, '00:00:00' )

  comment 'event scheduling sample'
  do
    call db_name.procedure_name_that_updates_the_user_records();
;
//
delimiter ;

Refer to: MySQL: CREATE EVENT Syntax

Default state of Event Scheduler is DISABLED.
You require to enable it by any of the following statements.

SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;

When the Event Scheduler is ON, the event scheduler thread is listed in the output of SHOW PROCESSLIST as a daemon process, and its state is represented as shown here:

mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
     Id: 1
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 2. row ***************************
     Id: 2
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 3
  State: Waiting for next activation
   Info: NULL
2 rows in set (0.00 sec)

Once the Event Scheduler is set ON, you would see it working.

Refer to : MySQL Event Scheduler Configuration

这篇关于Mysql PHP Cron用于更新用户统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆