I have table called page
which represents every single page in my website.
page_id | page_subject | page_path
----------------------------------
1 | Foo | /Foo
2 | Bar | /Bar
I also have table called group
:
group_id | group_name
---------------------
1 | Users
2 | Admins
Goal: how to define access to every page for single group?
I did create another table - page_group
:
page_group | group_id
---------------------
1 | 1
1 | 2
Table contains groups that have access to page (page_group
).
I wrote function:
CREATE FUNCTION `PAGE_ACCESS`(`pageId` INT, `groupId` MEDIUMINT)
BEGIN
RETURN IF((SELECT COUNT(*) FROM `page_group` WHERE page_id = pageId AND group_id = groupId) > TRUE, FALSE);
END;
Now I can do something like that:
SELECT *
FROM page
WHERE <conditions> AND PAGE_ACCESS(page_id, <group_id>)
...which returns pages that I should have access to.
Works fine but seems to be slow with with rows > 100,000 How would you do that? Would you do that in a different way?