Rails和Postgresql中的每周小时分配问题 [英] Weekly hour allocation problem in Rails and Postgresql

查看:100
本文介绍了Rails和Postgresql中的每周小时分配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个具有特定日期范围的任务列表,并且该任务分为每周工作量(例如,从2018-12-31到2019-01-06的30小时...从

If I have a list of tasks with a certain date ranges, and the task is broken into weekly hour chunks of work (ie. 30 hours from 2018-12-31 to 2019-01-06 ... etc starting from Monday).

我想做的操作是


  1. 显示用户列表中所有任务的所有每周时间

  2. 将用户一周中所有任务的每周工作时间总计

  3. 修改任务的持续时间后,请创建/销毁每周的小时块。

存储这些每周记录会更有效吗?

Would it be more efficient to store these weekly records as


  1. 开始日期/结束日期/小时,

  2. 年/周数/小时

存储开始/结束日期可能会给表带来更大的灵活性,因为它有可能存储非每周的校准时间。

Storing start/end date probably give more flexibility to the table as it could potentially store non-weekly align hours.

存储周数意味着给定日期范围,创建周块就像查找开始日期的周数一样简单。吃和结束日期的星期数,并填充之间的星期数(不转换为日期范围)。只要一周号是1到53,验证更新一周的小时数也更容易。

Storing week number means given a date range, creating the weekly chunks is as simple as finding the week number of the start date and the week number of the end date, and populating the weeks in between (without converting to date ranges). Also easier validation for updating the hours for a week, as long as the week number is 1-53.

如果有人尝试过任何一个选项并可以提供任何指针,这将很令人惊奇

Wondering if anyone has tried out either option and can give any pointers on their preferred option.

推荐答案

我可能会选择 daterange 列。

这使您可以灵活地使用不同大小的块,并可以定义排除约束,以防止范围重叠。

That gives you the flexibility to have differently sized chunks and allows you to define an exclusion constraint to prevent overlapping ranges.

在给定的一周中仍然查找行使用包含运算符 @> 非常简单,例如其中the_column @> to_date('2019-24','iyyy-iw')查找包含2019年第24周的行。

Finding the row for a given week is still quite simple using the "contains" operator @>, e.g. where the_column @> to_date('2019-24', 'iyyy-iw') finds the row(s) that contain week number 24 in 2019.

表达式 to_date('2019-24','iyyy-iw')返回指定星期的第一天(星期一)。

The expression to_date('2019-24', 'iyyy-iw') returns the first day (Monday) of the specified week.

查找两周之间的所有行也可以完成,但是构建相应的日期范围看起来有点难看。您可以使用第一天和最后一天构建包含范围: daterange(to_date('2019-24','iyyy-iw'),to_date('2019-24','iyyy-iw' )+ 6,'[]')

Finding all rows that are between two weeks can also be done, however construction the corresponding date range looks a bit ugly. You can either construction an inclusive range with the first and last day: daterange(to_date('2019-24', 'iyyy-iw'), to_date('2019-24', 'iyyy-iw') + 6, '[]')

或者您也可以在下周的第一天创建一个具有专有上限的范围: daterange(to_date('2019-24','iyyy-iw'),to_date('2019-25','iyyy-iw'),'[)')

Or you can create a range with an exclusive upper range with the next week's first day: daterange(to_date('2019-24', 'iyyy-iw'), to_date('2019-25', 'iyyy-iw'), '[)')

虽然可以很有效地为范围建立索引,并且维护所需的GIST索引比在两个整数列上的B-Tree索引要贵一些。

While ranges can be indexed quite efficiently and , the required GIST indexes are a bit more expensive to maintain than a B-Tree index on two integer columns.

使用范围的另一个缺点(如果您真的不需要灵活性)是它们比两个整数列(14字节而不是8字节)占用更多的空间,或者甚至有两个smallint的4)。因此,如果您担心表的大小,那么使用年/周列的当前解决方案会更有效。

Another downside of using ranges (if you don't really need the flexibility) is that they take up more space than two integer columns (14 byte instead of 8, or even 4 with two smallint). So if the size of the table is of any concern, then your current solution with the year/week columns is more efficient.


存储周数意味着在给定日期范围的情况下,创建周块就像查找开始的周数一样简单日期和结束日期的星期数

如果您输入的是开始日期和结束日期,请以(而不是周数),那么我肯定会选择 daterange 列。如果开始日期和结束日期超过一周,那么您只能存储一行,而不是多行。

If your input is a start and end date to begin with (rather than a "week number"), then I would definitely go for a daterange column. If that start and end date cover more than one week, then you store only one row, rather than multiple rows.

这篇关于Rails和Postgresql中的每周小时分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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