if you have to fight with large tables only the practice will let you know the best way to deal with "deadlocks"..especially in a dynamic environment. A good way for me it could be to wrap the query into a possible retry loop while checking the status of the query like here