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

3

u/jshine13371 May 20 '26

If you know the answer, I’d be curious whether Indexed Views improved after 2014?

I vaguely recall them being mentioned in one of the updates Microsoft did, but nothing jumps out at me of anything super notable. It may have just been a bug fix.

But what I will say is that sucks you're stuck on 2014. Besides being a version way out of support now, 2014 also had significant query planner / engine changes that both improved a lot of execution plans and unfortunately regressed a lot of others which later got fixed in subsequent major versions of SQL Server. Also, many new syntactical features were introduced besides major improvements to performance tuning features like columnstore indexes and Query Store, after 2014 (e.g. 2016 was a pretty notable version for this).

On 2014, I'd actually recommend familiarizing yourself with a few specific query hints, particularly FORCE_LEGACY_CARDINALITY_ESTIMATION for potential tools of query tuning quick wins when a particular query regressed because of the engine changes in 2014. Some of your queries may just legitimately run better under the 2012 engine which that hint forces.

1

u/GrandOldFarty May 21 '26

Whaaa I actually have had some weird cardinality issues, eg query plans picking hash match over nested joins for 50k or fewer rows. I end up killing jobs that run for 20+ mins when they should have taken seconds.

My solution to date has been to put spurious filters on the query which seems to signal “this result set will be small, not worth hashing”. I will give the hint you suggested a whirl.

Thanks for the advice, much appreciated.

2

u/jshine13371 May 21 '26

Absolutely, cheers!

eg query plans picking hash match over nested joins for 50k or fewer rows

You can experiment with join hints here too, e.g. directly in the join INNER LOOP JOIN etc. Or at the end of the query as an option, e.g. OPTION (LOOP JOIN), which will then apply to all joins in the query. When there is a valid query plan that can use that type of physical join operator, it forces the engine to pick that plan instead. Otherwise, it'll error if there truly isn't a plan applicable, so definitely test.

But try not to be trigger happy with query hints, especially join hints. They're helpful in complex cases that you don't have the time or ability to debug otherwise, but they're more of a plan B as opposed to tuning the root problem. The legacy cardinality estimation hint I mentioned earlier is much safer to use as needed though (especially knowing you're on a significant version where the engine changes).