duana1986 2010-05-11 17:50
浏览 24
已采纳

如何有效地执行这个cron作业?

I have a table with 200 rows. I'm running a cron job every 10 minutes to perform some kind of insert/update operation on the table. The operation needs to be performed only on 5 rows at a time every time the cron job runs. So in first 10 mins records 1-5 are updated, records 5-10 in the 20th minute and so on.

When the cron job runs for the 20th time, all the records in the table would have been updated exactly once. This is what is to be achieved at least. And the next cron job should repeat the process again.

The problem: is that, every time a cron job runs, the insert/update operation should be performed on N rows (not just 5 rows). So, if N is 100, all records would've been updated by just 2 cron jobs. And the next cron job would repeat the process again.

Here's an example:

This is the table I currently have (200 records). Every time a cron job executes, it needs to pick N records (which I set as a variable in PHP) and update the time_md5 field with the current time's MD5 value.

+---------+-------------------------------------+ 
|      id | time_md5                            | 
+---------+-------------------------------------+ 
|      10 | 971324428e62dd6832a2778582559977    |  
|      72 | 1bd58291594543a8cc239d99843a846c    |  
|       3 | 9300278bc5f114a290f6ed917ee93736    |  
|      40 | 915bf1c5a1f13404add6612ec452e644    |  
|     599 | 799671e31d5350ff405c8016a38c74eb    |  
|      56 | 56302bb119f1d03db3c9093caf98c735    |  
|     798 | 47889aa559636b5512436776afd6ba56    | 
|       8 | 85fdc72d3b51f0b8b356eceac710df14    |     
|      .. | .......                             |     
|      .. | .......                             |     
|      .. | .......                             |     
|      .. | .......                             |     
|     340 | 9217eab5adcc47b365b2e00bbdcc011a    |  <-- 200th record   
+---------+-------------------------------------+ 

So, the first record(id 10) should not be updated more than once, till all 200 records are updated once - the process should start over once all the records are updated once.

I have some idea on how this could be achieved, but I'm sure there are more efficient ways of doing it.

Any suggestions?

  • 写回答

2条回答 默认 最新

  • duancha1065 2010-05-11 17:56
    关注

    You could use a Red/Black system (like for cluster management).

    Basically, all your rows start out as black. When you run your cron, it will mark the rows it updated as "Red". Once all the rows are red, you switch, and now start turning all the red rows to be black. You keep this alternation going, and it should allow you to effectively mark rows so that you do not update them twice. (You could store whatever color goal you want in a file or something so that it is shared between crons)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看