I have a SQL script that has been causing issues with deadlocks, and I’m wondering if there is some sort of special lock that is applied when doing a bitwise operation.
The offending query is this:
UPDATE pos.prices SET active = 1 WHERE NOT (Attributes & 1 = 1)
with column types active BIT and Attributes TINYINT.
The reason is that tons of other scripts with updates not including a bitwise AND (&) execute with no issues. This is the only update with this operation and it is deadlocking.
The query it is deadlocking against is a large, periodic select with multiple joins that is grabbing a SCHEMA lock.
Edit: Would this query avoid the table scan?
UPDATE pos.prices SET active = 1 WHERE id in
(SELECT id FROM
(SELECT id, (Attributes & 1) as IsLocked FROM pos.prices) as t1
WHERE NOT IsLocked = 1)
Any operation the does a calculation on a column in the where clause prevents the usage of an index for that column. That leads to a table scan. While a table scan does not necessarily produce deadlocks, it can significantly increase their probability.