Improved query to tell local records from grid ones