optimize query searching for existing entries