I'm not sure the substring would help much. It only make sense to do it after the trim, but I don't think it would speed up the startsWith queries.