Query case optimization