1

one of the tab is broken in the Application (when hit that tab it keeps on rotating and calling the particular service and getting 404 error after sometime). we created indexes for few columns in Application DB to see if that improves performance. Creating indexes didn't make much difference.

so We planned to rewrite the sql query to improve the performance and fix it.

select distinct 
    editedUser.*
from
    users editedUser,
    relationship_ref editedUserRelationship,
    users approvingUser,
    user_role approvingUserRoles,
    role_permission approvingUserRolePermissions,
    account approvingUserAccount
where
    approvingUser.user_id = 175263
    and approvingUser.user_id = approvingUserRoles.user_id
    and approvingUserRoles.role_id = approvingUserRolePermissions.role_id
    and approvingUserRoles.user_role_status_id = 2
    and editedUserRelationship.relationship_id = 
        editedUser.submitted_relationship_id
    and (approvingUser.account_id = approvingUserAccount.account_id
         or approvingUser.account_id is null)
    and editedUser.review_status = 'R'
    and approvingUserRolePermissions.permission_id = 
        editedUserRelationship.view_pending_permission_id;

It is taking nearly 6 mins.So can one please suggest how to use the proper joins in this query. It has 36 columns and 30,000 records.

3
  • 1
    What makes you think the problem is with the joins? Commented Dec 24, 2018 at 16:11
  • if i use left or right join instead of cross join.It may improve performance Commented Dec 24, 2018 at 16:29
  • 1
    It really won't. Commented Dec 24, 2018 at 16:45

1 Answer 1

1

Rearranged in a style that is easier to read (to me at least), I get this:

select distinct ed.*
from   users ap
       join user_role ro
            on  ro.user_id = ap.user_id
       join account ac
            on  ac.account_id = ap.account_id or ap.account_id is null
       join role_permission rp
            on  rp.role_id = ro.role_id
       join relationship_ref re
            on  re.view_pending_permission_id = rp.permission_id
       join users ed
            on  ed.submitted_relationship_id = re.relationship_id
where  au.user_id = 175263
and    ro.user_role_status_id = 2
and    ed.review_status = 'R'

How many approving users have null account_id? For any of those, you retrieve all accounts in the system. Is that actually the business requirement? I'm not sure that it makes any sense. The query does not make any further use of that table, so perhaps you can remove the account join entirely.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.