复杂的数据库查询与数据和存储过程的计划存储
我正在使用 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.date
或 datediff
进行排序。我真正需要的是能够对一个字段进行排序,并且 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您应该使用 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
在这种情况下,我可能由于没有足够地研究 MySQL 的控制流函数而使可行的选项变得过于复杂。
我现在要采用的一个解决方案是
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