Skip to content

Conversation

@aidanharan
Copy link
Contributor

When you perform a query using locking and a left-join, the lock is used in both the FROM and LEFT OUTER JOIN portions of the query:

ActiveRecord: Person.lock(true).left_joins(:readers).load
SQL: SELECT [people].* FROM [people] WITH(UPDLOCK) LEFT OUTER JOIN [readers] WITH(UPDLOCK) ON [readers].[person_id] = [people].[id]

However, when you use the same query with a inner-join, the lock is not used in the INNER JOIN portion of the query:

ActiveRecord: Person.lock(true).joins(:readers).load
SQL: SELECT [people].* FROM [people] WITH(UPDLOCK) INNER JOIN [readers] ON [readers].[person_id] = [people].[id]

This change will apply the lock hint to the inner-joins.

I came across this issue as we are trying to use NOLOCK hints on queries containing joins.

ActiveRecord: Person.lock('WITH(NOLOCK)').joins(:readers).load
Before change SQL: SELECT [people].* FROM [people] WITH(NOLOCK) INNER JOIN [readers] ON [readers].[person_id] = [people].[id]
After change SQL: SELECT [people].* FROM [people] WITH(NOLOCK) INNER JOIN [readers] WITH(NOLOCK) ON [readers].[person_id] = [people].[id]

@metaskills
Copy link
Member

Love the work here, thanks!

@metaskills metaskills merged commit 3c0ca4c into rails-sqlserver:master Jan 23, 2018
@metaskills
Copy link
Member

Just pushed v5.1.6 to rubygems. Again, thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants