Optimizing query for datepicker