I have 2 tables that manages the time spent on doing various things:
@times(id, time_in_minutes)
@times_intervals(id, times_id, time_in_minutes, start, end)
Then the @times might relate to different things:
@tasks(id, description)
@products(id, description, serial_number, year)
What is the best practice in order to reuse the same @times and @times_intervals for @task and @products?
I would think about:
@times(+task_id, +product_id)
// add task_id and product_id to the original @times table
But if I do so, when I’d join the @times table with @task and @products table would be slower as should choice between the 2 (task_id or product_id). When task_id is not null join on the @tasks and viceversa.
(I’m using MySQL6)
Thanks a lot
I would drop the time_in_minutes column from the times table. This information is redundant if it is just the sum of the detail and is a premature optimization.
I would add a product_time table containing product_id, times_id and a task_time table containing task_id, time_id
Then to get the total time with a product:
Typically to make this perform, you would have a non-clustered covering index for times_intervals with columns times_id and time_in_minutes – note that the times table is simply a data-less header table at this point and the only purpose it to group the times_intervals and it’s only necessary because you have this very similar arrangement for tasks.
If there were not two (or more) entities using the times_intervals, you might simply put product_id in the times_intervals and treat it as your header/master id.