What you have can work, given a few constraints:
- You move between states in a linear fashion
- You have multiple states with multiple activities.
- You have only one consideration for what "color" the alert indicator should be.
- You have three colors and only three colors (no more and no fewer).
Based on what you mentioned as requirements, that solution fits most of the constraints, but not #2. There are a couple of things I would look at, though.
One thing I would question is whether you need that Activity table at all. If it really is a 1:1 correspondence between states and activities, you could wrap it up into one table without loss: your activity is just the action which pushes you to the next state. If, however, there are multiple activities per state, this can make sense. If you have an overarching status category (e.g., when the row is in "Vehicle Assigned" status, there are 5 separate activities which need to occur to get it to "Closed" status), then this general idea is alright.
So if you have one activity per state, the Status table would have the following attributes:
- Name
- Activity required to get to this state (if you, for example, have a web application and you want to populate the button text with a verb instead of a noun)
- The next status ID (assuming this is a linear workflow)
- GreenMax (the maximum number of minutes during which the row can be "green")
- YellowMax (the maximum number of minutes during which the row can be "yellow")
RedMax is implied: it's red whenever the difference between the current time and whichever time you want to use (DateOfRequirement, LastUpdate, etc.) is greater than YellowLimit.
Alternatively, if you have different categories for display such as "step 1 has red-yellow-green, but step 2 is just red-green and step 3 is red-pink-orange" (or if you believe that you will), I would move the colors out to their own table and create a Status-Color bridge table with status ID, color ID, and maximum time. That makes retrieval a bit more complicated, though.
Now, if you do have multiple activities per status, then I would turn Status into a parent-child relationship with Activity, instead of a many-to-many relationship like you have now with Rules. So Status would have Name, and Activity would look like:
- Name
- Status ID (to get the current status)
- The next Activity ID (assuming this is a linear workflow)
- GreenMax
- YellowMax
And on your main object, you would actually hold the activity ID instead of the status ID, because you can derive the status from the activity.
The other thing I would do is drop the Rules table. The only time that you would need a Rules table is if you have multiple statuses, multiple activities, and there are multiple relationships between these (for example, that both the New and Closed statuses both be able to perform the Assign Vehicle activity). Based on your example, this does not appear to be the case. It is possible that you actually do that many-to-many relationship between activities and statuses, if the relationship is not like your example. In that event, your Rules table looks okay, with the caveat that you don't need the Red limit.
Incidentally, I highly recommend that you put the actual workflow logic in a stored procedure, whether you keep the linear flow (like you have now) or move to a non-linear workflow model. You can use a combination of triggers and check constraints to force a workflow to operate in a particular manner, but it's a lot easier to understand if you just do it in a stored procedure: all of the code is in one place not in an easy-to-forget area like a trigger. Putting this in a stored procedure also abstracts it away from the application and business object layers, so they just need to call a method which says "go to the next workflow step" (and maybe pass in a detail to help you figure out which workflow step, in the event that this is not a linear process).
EDIT (post question update)
Based on your question update and comment, I'm okay with a three-table approach. I'm not positive that you need it, but this could simplify things at the application level.
At this point, what you have is a state machine, with a State (status) table and a Transition (activity) table. Your rule table then defines which transitions are valid for a particular state.
I would change the Rule table slightly, to look like this:
- Surrogate key
- Rule Name
- Starting State ID
- Transition (Activity ID)
- Ending State ID
- Green Limit
- Yellow Limit
There is one minor change: the position of the different keys. Your rule table now reads like a book: starting from one state, perform a transition and end at a different state (or the same one--you have an example in which the final state is the same as the initial state).
You would have a unique key constraint on starting state ID and activity ID, to guarantee that a particular transition from a particular state always ends in a single state.
On your vehicle table, I would put idRule instead of idStatus, to make it easy to tell which set of limits you need. You might also want a history table showing the vehicle ID, rule ID, and time added, so you can track what happened in the workflow.
This means that your "go to the next workflow step" stored procedure is actually pretty simple: given a vehicle ID and activity to perform, you can do a quick select against the Rule table to see if there is a valid transition from the vehicle's rule's state via the activity ID you get in the proc. If there is, perform that transition: update the vehicle table accordingly (setting idRule and LastUpdate); if not, return an error indicating that something must have gone wrong.
There is also no problem with having a table with two foreign keys back to another table. idCurrentState and idNextState can both be legitimate foreign keys back to State. You just need different names for the two foreign keys (e.g., FK_Rule_State_CurrentState and FK_Rule_State_NextState).
I partially agree with Aaron's comment - in the most general case for storing 21 unrelated pieces of information, you'd probably use 21 bit columns. As a general solution, it may well be your best solution. If you had multiple bitmask-ed varchar columns, that would translate to a row with possibly over a hundred bit flags. FYI, 21 bits get stored as 3 bytes when you don't define them as NULLable, removing the necessity for space in the NULL bitmap. Since you have multiple bitmask columns, you'd end up with every 8 bits mashed into a byte.
What SQL Server ends up doing with your multi-column queries is eventually a bunch of bitmasking routines (yes! SQL Server uses bitmasks, so they the concept per se can't be all bad!) but for average use cases, it makes life easier for you.
If we had more information about what types of queries you run, we may be able to better advise, because ultimately the use cases dictate the design.
If you persist with the COMPUTED column, I would persist and index it if you haven't already. It helps some queries, such as
exact matches
WHERE computedInt = POWER(2, 6) -- bit position 7
AND matching on 15th bit and OR matching on 2 other bits (10th and 7th)
WHERE computedInt >= Power(2,14) AND computedInt < Power(2,15)
AND computedInt & (Power(2,9) + Power(2,6)) > 0
But these are probably exotic samples and yet also real live in some cases. It's certainly not too much worse than 21 individual bit columns, for which yes your statements could be easier to write, but remember that SQL Server has mashed them for storage into 3 bytes and will be doing the bit-unmasking anyway! You would have thought if bit-masking were all bad (without exception) then SQL Server wouldn't be doing it, right?
EDIT
Re the scenario of
Four flags, HasHouse,HasCar,HasCat,HasDog, 0000 is has none, 1111 is has all.
it is more efficient and logically expedient to test all 4 bits at once and do a single integer based operation, e.g.
WHERE computedInt & (POWER(2,10)+POWER(2,5)+POWER(2,3)+POWER(2,1)) = 0 -- has none
WHERE computedInt & (POWER(2,10)+POWER(2,5)+POWER(2,3)+POWER(2,1)) > 0 -- has one or more
Hypothetically, if this were your most exercised query on the table, you might even group the four columns into another computed column and index it separately, making the bitmask unnecessary (just test the resultant int with =0
and >0
). You might even go further and just precompute the answer... horses for courses.
Best Answer
Denormalization to the 'Vehicle' table is still option, technically, but based on the responses in the comments I would probably sub-class it as well. So, you're at the options I mentioned in my last comment. There's no magic bullet. It's string concatenation for dynamic SQL or separate statements. One way or another you're going to have to change your app code.