r/SQL • • 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?

5 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/kagato87 MS SQL May 20 '26 edited May 20 '26

Not many people use indexed views.

They do have a significant write cost if you're constantly inserting and updating the data. Especially if some developer has written a row by row etl to insert the data...

However, it might not be the answer to your challenge. It's tough, and the only way to be sure is by doing... Batch maintained RLS table is an excellent suggestion and probably the best one. You migjt also be able to use intermediary temp tables in a stored procedure to handle the checks.

There's no easy solution or silver bullet here.

3

u/GrandOldFarty May 20 '26

 Especially if some developer has written a row by row etl to insert the data...

Ah yes I scream whenever I see the words DECLARE CURSOR 

2

u/kagato87 MS SQL May 20 '26

Oh it was even worse than that. It'd retrieve all new data into a server side app, then do a row by row insert. Not great on any front when you're ETLing a quarter mil records per day per instance...

So as bad as the cursor, except it also added massive network traffic...

1

u/jshine13371 May 20 '26

Not many people use indexed views.

I would say that's subjective. I'm sure the number of instances out there using an indexed view is not far off from the number of systems that use columnstore indexing. And obviously that's no indication of the quality of the feature, actually, rather an indication of the knowledge base around the feature by developers.

I use them quite frequently, personally. They're a great way to simplify a materialization workflow I would've had to write code for otherwise.