r/SQL • u/sweetnsourgrapes • May 20 '26
SQL Server using a materialised view to track user-entity authorisation
I'm wondering if this is a used pattern, or something definitely not to do. Working with SQL Server.
We have a system where the rules about what a user can access are things like "if the user has presented a course within the last year then they have access to course materials within that same subject area."
So "rules-based" auth not role-based. Many queries are slow because of all the things they need to check to show a list of available courses, etc to any particular user.
Taking the above example, I was thinking of creating materialised views whose SQL create rows that link users to the entities they can access. So e.g. a user_coursematerial view, which contains user_id and coursematerial_id - the query for which joins users and course materials based on those rules as above.
(Edit: The idea being that the view maintains those connections itself as data changes - I assume that's what happens?)
Then every time I want to list all the course materials a user has access to, I just join the user to that view, and bam I have all the correct material_ids for that user, which is fast and the rules are all in one place.
Is this achievable and acceptable, or are there better approaches to complex permissions issues like this?
View all comments
Show parent comment
1
u/GrandOldFarty May 20 '26
Thanks for all this, that’s a very interesting perspective, particularly that they helped in a pre-columnstore world and mixed OLTP /OLAP (the transaction replication example makes a lot of sense).
I work in a very large company with a lot of legacy architecture so I am mainly struggling away in SQL Server 2014 Enterprise. I have been much happier with how MVs work in Oracle 12c.
If you know the answer, I’d be curious whether Indexed Views improved after 2014?