Reputation: 699
So I have a database that has a structure something like this:
[user_table] user_id, name, active
"john", "John S. Smith", true
[group_table] group_id, description, active
"abc", "test group", "true"
"cba", "second group", "true"
"bca", "third group", "true"
So now I would like to hide "abc" group from "john". I create a new table called "hide_group" with field user_id and group_id. I insert appropriate values and everything is nice. But it is not very well optimized. What if I have 20 users and hundreds of groups? That would mean hundreds of new records. Is there a more optimized way to do this with relationships? I am using MyISAM engine, and if I recall, I can't use foreign key with it. I am reading mysql documentation but it is still a blur to me. Maybe someone could point me at the right direction?
edit, my table structure is something like this:
[group]
group_id, group_description
[group_hide]
group_id, user_id, hide
I want to select all groups that are under "group" table. Except the ones that are hidden inside the group_id table. How can I do this? Right now I can make it either only select all records inside the group_hide table (which is not what I need), or just all records inside group, ignoring all that is in group_hide. I would like it not to select groups that are supposed to be hidden, but everything else should be visible.
Upvotes: 0
Views: 57
Reputation: 7066
If you can categorize the users and their group visibility somehow, you might consider defining certain "roles". So the associations would be between a role (say "Admin" or "Default" or "Moderator") and the groups, and between roles and users.
E.g. you would create a table roles
with an ID and a name
, a table roles_users
with 2 columns role_id
and user_id
and another table hidden_categories_roles
with 2 columns hidden_category_id
and role_id
. Don't forget to put indexes on both columns in both tables to speed up queries.
If the users are so different that they can't be categorized, your approach is the correct one, and you should simply define indexes on both columns of your new table to speed up queries.
Upvotes: 1