Optimize query which determines if mode is used