Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionOn my site when some user create profile he also choose his school name. Schools have the table in database. The main page in applicatioin should display some data about users school. And there are a few links that display something based on school ID.
Now I have two options:
- When user log in I get SchoolID based on userID and save it in session?
- Whenever I need to open something that request schoolID I send call to database to get schoolID based on userID?
- Something else.
Performance and security are my proprities becaouse thera are a lot users.
The performance gain by caching the schoolId is so minor that number 2 is your best option. Getting the schoolId based on userId would be a very quick lookup. Number 1 has the potential bug that you forget to update session when the user edits their school. It also assumes that schoolId would only be set within the session. The day you break that assumption by importing updates or the admin updates, the session will not match the data and all sorts of problems may ensue.
Of course, getting the school data should be one command: select School.* from School inner join User on User.SchoolId = School.SchoolId where User.UserId = @UserId
精彩评论