复杂的数据库查询与数据和存储过程的计划存储

发布于 2024-11-04 10:41:59 字数 1318 浏览 0 评论 0原文

我正在使用 MySQL 构建一个系统,允许管理和创建任务。任务可以是

  • 由固定日期重复任务完成的一次性任务
  • ,从固定日期开始并每 N 天重复一次

简化的模式如下所示

Tasks
========================

| id |   name     | date     | repeats |
|  1 |  Backup X  | 2011-... | 0       |
|  2 |  Backup Y  | 2011-... | 1       |

现在,假设我想对此数据集运行查询

SELECT $N - mod(datediff(NOW(), Task.date), $N) AS datediff, Task.name 
  FROM tasks Task
  ORDER BY datediff, Task.date;

: N 是任务的重复间隔。

正如您所看到的,我必须按 Task.datedatediff 进行排序。我真正需要的是能够对一个字段进行排序,并且 datediff 取决于任务是否重复。

在伪代码中,以下查询更合适:

SELECT
  IF(Task.repeats = 1)
    // Get the number of days until next repeat on a repeating event
    $N - mod(datediff(NOW(), Task.date), $N) AS datediff
  ELSE
    // Get number of days until non-repeating task expires
    datediff(NOW(), Task.date) AS datediff
  • 存储过程是一种选择,但比在应用程序代码中更难管理。
  • 视图是另一种选择。但是 MySQL 视图的效率不是很高。

因此,我正在考虑设置一个 CRON 作业,每隔 5 分钟左右用更新的数据填充一个表。然后我就可以查询这个表的相关字段。

我认为当任务变得更加复杂时,这种方法会更好地扩展:

例如,用户可以使用 crontab 语法为每个任务指定一个非常精确的重复间隔,这需要在应用程序代码中进行大量处理才能计算出所有任务的 datediff。

我很欣赏你对此的想法,因为我最近倾向于避免复杂的查询和存储过程。也许这太过分了?

I'm building a system using MySQL that allows tasks to be managed and created. Tasks can be either

  • a one-off task to be completed by a fixed date
  • repeating tasks, starting on a fixed date and repeating every N days

A simplified schema looks like

Tasks
========================

| id |   name     | date     | repeats |
|  1 |  Backup X  | 2011-... | 0       |
|  2 |  Backup Y  | 2011-... | 1       |

Now, let's say I want to run a query on this data set:

SELECT $N - mod(datediff(NOW(), Task.date), $N) AS datediff, Task.name 
  FROM tasks Task
  ORDER BY datediff, Task.date;

where N is the repeat interval for a task.

As you can see, I have to sort by either Task.date or datediff. What I really need is to be able to sort on one field and for datediff to be dependent on whether or not a task repeats.

In psuedocode, the following query would be more appropriate:

SELECT
  IF(Task.repeats = 1)
    // Get the number of days until next repeat on a repeating event
    $N - mod(datediff(NOW(), Task.date), $N) AS datediff
  ELSE
    // Get number of days until non-repeating task expires
    datediff(NOW(), Task.date) AS datediff
  • Stored procedures are one option, but are more difficult to manage then in application code.
  • Views are another option. But MySQL views are not very efficient.

So, I am thinking of setting up a CRON job that populates a table with updated data every 5 minutes or so. Then I will be able to query this table on the relevant fields.

I think this method would scale better when tasks become more complicated:

e.g. users could give each task a very precise repeat interval using crontab syntax which would require quite a lot of processing in application code to work out the datediff on all tasks.

I appreciate your thoughts on this since i've had a tendency lately to avoid complex queries and stored procedures. Perhaps this is overkill?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-11-11 10:41:59

在我看来,您应该使用 MySQL Events

http:// dev.mysql.com/tech-resources/articles/mysql-events.html

sounds to me like you should store/run these with MySQL Events

http://dev.mysql.com/tech-resources/articles/mysql-events.html

猫烠⑼条掵仅有一顆心 2024-11-11 10:41:59

在这种情况下,我可能由于没有足够地研究 MySQL 的控制流函数而使可行的选项变得过于复杂。

我现在要采用的一个解决方案是

SELECT IF(Task.repeat, repeats_expression, non_repeats_expression) as date_diff; 

I probably over complicated the viable options in this case by not having looked into MySQL's control flow functions nearly enough.

One solution i'm goin with at the minute is

SELECT IF(Task.repeat, repeats_expression, non_repeats_expression) as date_diff; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文