Using subquery to avoid locking