Cost-aware rank join with random and sorted access