I have a database. In one table, each row needs to contain a开发者_JAVA百科 list of users associated with that row. What's the best way to do so? The options I have are:
1) Have one column "users_id", which is an index into a "users" table that has multiple entries per id, one for each user. Downside is I need an extra join, upside is it's flexible. There will be few users, but many, many rows, so is the extra join really worth it?
2) Have all user ids be powers of two. The "users" field is a bit-wise OR of these. Seems simpler, and I don't need more than 32 users, but are joins on bit-wise ANDs fast / doable?
3) Just make a separate user for any combination of users. Kind of like 2) except less intelligent.
Do it the right way. The bitwise field will prevent any useful indexing on that column.
You should have an association table, that contains a row for each combination that you have. It's the database's job to provide good query performance, it will do that with well-structured tables. Don't sweat an extra join here or there, but instead normalize the data correctly and discretely.
You didn't provide much information about the purpose of the data. I usually do something like this:
Table: User_RowEntry_Assignment
UserId: int
RowEntryId: uniqueidentifier
This is a good pattern because it makes it possible to query the data without querying the user assignment, when the user assignment is irrelevant. For example, if you want to search for all of the RowEntry
items with a set of specifications, and the associated user is not important, then you don't need to get the user info. Also, you can have multiple user-row assignments without any complication.
精彩评论