should you index a many to many table such as user_role where it has 2 fields (userId and RoleId) ?
and what kind of index would be best?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends. If there are a lot of user/role combinations (tens of thousands or more), and you are querying this information a lot, or need it to be particularly performant, then yes.
If there are not very many records, or you don’t care if it’s very performant, or don’t use it very often, then most likely no.
For the usage part, SQL Server can help you decide if this is used enough to warrant an index. See here for some guidance.
Also, as Martin pointed out, if you have defined user/role as a primary key on this table, you already have an index, the clustered index, for that combination of columns.